0

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
asked Jan 31, 2019 at 7:00
1
  • what have you set in Serial Monitor as line ending to send? Commented Jan 31, 2019 at 9:16

1 Answer 1

0

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);
} 
answered Jan 31, 2019 at 7:22
4
  • 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 end Commented Jan 31, 2019 at 19:11
  • @nouralbrmky Like in the code i posted, null input is probably newline. Commented 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 thankful Commented 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. Commented Jan 31, 2019 at 21:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.