Arrays 如何将输入存储在数组中,然后在以后调用它?
我已经用以下方法创建了一个莫尔斯电码输入设备:Arrays 如何将输入存储在数组中,然后在以后调用它?,arrays,arduino,arduino-uno,Arrays,Arduino,Arduino Uno,我已经用以下方法创建了一个莫尔斯电码输入设备: *阿杜伊诺乌诺 *格罗夫基地高地 *Grove按钮 *格罗夫光传感器 *LCD RGB背光灯 我有它,所以一个字显示在LCD上(在本例中为“黄色”)。然后,该单词必须通过按钮作为莫尔斯电码输入。 输入时,这些字母将从摩尔斯电码翻译成英语,并在屏幕上显示为字母。 (光传感器用于重置LCD) 我的问题是。。。我如何将答案作为莫尔斯电码输入并存储,然后检查其正确性?因此,如果我女儿输入“Yellop”,它会显示一条消息,说“不正确”。她会重置并重试。 我
*阿杜伊诺乌诺
*格罗夫基地高地
*Grove按钮
*格罗夫光传感器
*LCD RGB背光灯
我有它,所以一个字显示在LCD上(在本例中为“黄色”)。然后,该单词必须通过按钮作为莫尔斯电码输入。 输入时,这些字母将从摩尔斯电码翻译成英语,并在屏幕上显示为字母。 (光传感器用于重置LCD) 我的问题是。。。我如何将答案作为莫尔斯电码输入并存储,然后检查其正确性?因此,如果我女儿输入“Yellop”,它会显示一条消息,说“不正确”。她会重置并重试。 我对这一切都是非常陌生的,希望能得到一些帮助,让这一切顺利进行。 现在,我假设我必须以某种方式将每个字母输入存储在某种数组中,然后检查其正确性。然而,我不知道该怎么做。 这是我的密码
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
#define DOT false
#define DASH true
#define MAX_CHAR_LENGTH 6
#define BUTTON A0
#define LIGHTSENSOR A1
#define pinLED 6
#define PULSE_THRESHOLD 400
#define LETTER_SEPARATION 600
#define WORD_SEPARATION 8000
const struct MorseTree *tree = NULL;
const int colorR = 0;
const int colorG = 0;
const int colorB = 100;
int value;
int button = -1;
int pulseThreshold = 0;
int letterSeparation = 0;
int wordSeparation = 0;
void initMorse(int lbutton, int lpulseThreshold, int lletterSeparation, int
lwordSeparation)
{
tree = generateMorseTree();
button = lbutton;
pulseThreshold = lpulseThreshold;
letterSeparation = lletterSeparation;
wordSeparation = lwordSeparation;
}
struct MorseTree
{
// '-' unless this is a leaf node
char character;
struct MorseTree *dotChild;
struct MorseTree *dashChild;
};
struct MorseData
{
char character;
bool code[MAX_CHAR_LENGTH];
byte codeLength;
};
void initMorseTree(struct MorseTree *tree)
{
tree -> character = '-';
tree -> dotChild = NULL;
tree -> dashChild = NULL;
}
struct MorseTree *newMorseTree()
{
struct MorseTree *tree = (struct MorseTree *) malloc(sizeof(struct
MorseTree));
initMorseTree(tree);
return tree;
}
void addTreeMember(struct MorseTree *tree, struct MorseData &data)
{
struct MorseTree *current = tree;
for (byte i = 0; i < data.codeLength; i++)
{
boolean currentSymbol = data.code[i];
if (currentSymbol == DOT)
{
if (current -> dotChild == NULL)
current -> dotChild = newMorseTree();
current = current -> dotChild;
}
else
{
if (current -> dashChild == NULL)
current -> dashChild = newMorseTree();
current = current -> dashChild;
}
}
// now current must be a leaf node
current -> character = data.character;
}
void addTreeMembers(struct MorseTree *tree, struct MorseData data[],
byte dataLength)
{
for (byte i = 0; i < dataLength; i++)
addTreeMember(tree, data[i]);
}
void addAlphabet(struct MorseTree *tree)
{
struct MorseData data[] = {
{'A', {DOT, DASH}, 2},
{'B', {DASH, DOT, DOT, DOT}, 4},
{'C', {DASH, DOT, DASH, DOT}, 4},
{'D', {DASH, DOT, DOT}, 3},
{'E', {DOT}, 1},
{'F', {DOT, DOT, DASH, DOT}, 4},
{'G', {DASH, DASH, DOT}, 3},
{'H', {DOT, DOT, DOT, DOT}, 4},
{'I', {DOT, DOT}, 2},
{'J', {DOT, DASH, DASH, DASH}, 4},
{'K', {DASH, DOT, DASH}, 3},
{'L', {DOT, DASH, DOT, DOT}, 4},
{'M', {DASH, DASH}, 2},
{'N', {DASH, DOT}, 2},
{'O', {DASH, DASH, DASH}, 3},
{'P', {DOT, DASH, DASH, DOT}, 4},
{'Q', {DASH, DASH, DOT, DASH}, 4},
{'R', {DOT, DASH, DOT}, 3},
{'S', {DOT, DOT, DOT}, 3},
{'T', {DASH}, 1},
{'U', {DOT, DOT, DASH}, 3},
{'V', {DOT, DOT, DOT, DASH}, 4},
{'W', {DOT, DASH, DASH}, 3},
{'X', {DASH, DOT, DOT, DASH}, 4},
{'Y', {DASH, DOT, DASH, DASH}, 4},
{'Z', {DASH, DASH, DOT, DOT}, 4},
};
addTreeMembers(tree, data, 26);
}
void addNumbers(struct MorseTree *tree)
{
struct MorseData data[] = {
{'1', {DOT, DASH, DASH, DASH, DASH}, 5},
{'2', {DOT, DOT, DASH, DASH, DASH}, 5},
{'3', {DOT, DOT, DOT, DASH, DASH}, 5},
{'4', {DOT, DOT, DOT, DOT, DASH}, 5},
{'5', {DOT, DOT, DOT, DOT, DOT}, 5},
{'6', {DASH, DOT, DOT, DOT, DOT}, 5},
{'7', {DASH, DASH, DOT, DOT, DOT}, 5},
{'8', {DASH, DASH, DASH, DOT, DOT}, 5},
{'9', {DASH, DASH, DASH, DASH, DOT}, 5},
{'0', {DASH, DASH, DASH, DASH, DASH}, 5},
};
addTreeMembers(tree, data, 10);
}
void addPunctuation(struct MorseTree *tree)
{
struct MorseData data[] = {
{'.', {DOT, DASH, DOT, DASH, DOT, DASH}, 6},
{',', {DASH, DASH, DOT, DOT, DASH, DASH}, 6},
{'?', {DOT, DOT, DASH, DASH, DOT, DOT}, 6},
};
addTreeMembers(tree, data, 3);
}
struct MorseTree *generateMorseTree()
{
struct MorseTree *tree = newMorseTree();
initMorseTree(tree);
addAlphabet(tree);
addNumbers(tree);
addPunctuation(tree);
return tree;
}
void waitFor(int pin, int state)
{
do
{
// spin until the pin reads the given state
while (digitalRead(pin) != state) { }
// delay, to verify the reading
delay(5);
// continue if the reading has changed
} while (digitalRead(pin) != state);
}
boolean getNextSymbol()
{
waitFor(button, HIGH);
unsigned long start = millis();
waitFor(button, LOW);
unsigned long end = millis();
unsigned long pulseLength = end - start;
// Serial.println(pulseLength);
if (pulseLength > pulseThreshold)
return DASH;
else
return DOT;
}
boolean shouldTimeOut()
{
unsigned long start = millis();
while (digitalRead(BUTTON) == LOW)
{
if (millis() - start > letterSeparation)
return true;
}
return false;
}
boolean shouldBeSpace()
{
unsigned long start = millis();
while (digitalRead(BUTTON) == LOW)
{
if (millis() - start > wordSeparation)
return true;
}
return false;
}
char getNextChar()
{
static boolean lastCharWasSpace = false;
const struct MorseTree *current = tree;
byte symbolCount = 0;
if (!lastCharWasSpace && shouldBeSpace())
{
lastCharWasSpace = true;
return ' ';
}
lastCharWasSpace = false;
while (true)
{
symbolCount++;
boolean currentSymbol = getNextSymbol();
// Serial.println(currentSymbol == DOT ? "DOT" : "DASH");
if (currentSymbol == DOT)
current = current -> dotChild;
else
current = current -> dashChild;
if (current == NULL)
return '-';
if (shouldTimeOut() || symbolCount >= MAX_CHAR_LENGTH)
return current -> character;
}
// should never get here
return '!';
}
void setup()
{
pinMode(BUTTON, INPUT);
pinMode(LIGHTSENSOR, INPUT);
value = analogRead(LIGHTSENSOR);
initMorse(BUTTON, PULSE_THRESHOLD, LETTER_SEPARATION, WORD_SEPARATION);
Serial.begin(9600);
while(! Serial);
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
lcd.print("Input 'yellow'");
}
void loop()
{
Serial.println(value);
value = analogRead(LIGHTSENSOR);
if(value<40)
{
lcd.clear();
}
static boolean firstLoop = true; //skips the first space between words.
char c = getNextChar();
if (firstLoop)
{
firstLoop = false;
if (c == ' ')
return;
}
lcd.print(c);
}
#包括
#包括“rgb_lcd.h”
rgb_液晶显示器;
#定义点假
#定义破折号为真
#定义最大字符长度6
#定义按钮A0
#定义光传感器A1
#定义销6
#定义脉冲_阈值400
#定义字母_分隔600
#定义单词_分隔8000
const struct MorseTree*tree=NULL;
常数int colorR=0;
常数int colorG=0;
常数int colorB=100;
int值;
int按钮=-1;
int pulseThreshold=0;
int-letterSeparation=0;
int-wordSeparation=0;
void initMorse(int lbutton,int lpulseThreshold,int lletterSeparation,int
Lwords(分离)
{
tree=generateMorseTree();
按钮=L按钮;
pulseThreshold=lpulseThreshold;
字母分隔=字母分隔;
wordSeparation=lwordSeparation;
}
结构MorseTree
{
//“-”除非这是叶节点
字符;
结构MorseTree*dotChild;
结构MorseTree*dashChild;
};
莫塞达结构
{
字符;
布尔码[最大字符长度];
字节码长;
};
void initMorseTree(结构MorseTree*树)
{
树->字符='-';
tree->dotChild=NULL;
树->dashChild=NULL;
}
struct MorseTree*newMorseTree()
{
struct MorseTree*树=(struct MorseTree*)malloc(sizeof(struct
莫塞特里);
initMorseTree(树);
回归树;
}
void addTreeMember(struct MorseTree*树,struct MorseData&数据)
{
struct MorseTree*current=树;
用于(字节i=0;i点子对象==NULL)
current->dotChild=newMorseTree();
当前=当前->点子对象;
}
其他的
{
如果(当前->dashChild==NULL)
当前->dashChild=newMorseTree();
当前=当前->子项;
}
}
//现在,当前节点必须是叶节点
当前->字符=data.character;
}
void addTreeMembers(struct MorseTree*树,struct MorseData[],
字节(数据长度)
{
用于(字节i=0;i脉冲阈值)
返回短跑;
其他的
返回点;
}
布尔值shouldTimeOut()
{
无符号长启动=毫秒();
while(数字读取(按钮)=低)
{
如果(毫秒()-开始>字母间隔)
返回true;
}
返回false;
}
布尔shouldBeSpace()
{
无符号长启动=毫秒();
while(数字读取(按钮)=低)
{
if(millis()-start>wordSeparation)
返回true;
}
返回false;
}
char getNextChar()
{