My problem is when I run code like this:
char directioN;
void setup() {
Serial.begin(9600)
}
void loop()
{
if (Serial.available())
{
directioN = Serial.read();
switch (directioN)
{
case 'F':
break;
case 'B':
break;
case 'R':
break;
case 'L':
break;
default :
break;
}
Serial.print("direction is:");
Serial.println(directioN);
}
}
My serial monitor add another input, but the second one is empty; this happened here with char both as with int.
But when i tried the same code in tinkercad it worked fine.
So do I really need a new ATMEGA328 or it can be done by software or something?
**I use an Arduino Uno
Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
-
what have you set in Serial Monitor as line ending to send?Juraj– Juraj ♦2019年01月31日 09:16:04 +00:00Commented Jan 31, 2019 at 9:16
1 Answer 1
So why you don't just ignore it?
if(directionN >= 32){ //check if char is readable ascii character
Serial.print("direction is:");
Serial.println(directioN);
}
-
How i ignore. it affect how my code run cause a specific input do a specific function so the following zero or null input which im talking about here cause the wanted function to endnour albrmky– nour albrmky2019年01月31日 19:11:15 +00:00Commented Jan 31, 2019 at 19:11
-
@nouralbrmky Like in the code i posted, null input is probably newline.Matej– Matej2019年01月31日 19:13:47 +00:00Commented Jan 31, 2019 at 19:13
-
It worked fine thank you .do u have any idea what may cause some thing like that to happen and i 'll be more thankfulnour albrmky– nour albrmky2019年01月31日 21:56:20 +00:00Commented Jan 31, 2019 at 21:56
-
@nouralbrmky It's probably caused by newline character which is sent after text. Newline decimal value in ascii table is smaller than 32 and that's why this code ignores it.Matej– Matej2019年01月31日 21:58:19 +00:00Commented Jan 31, 2019 at 21:58
lang-cpp