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

  • what have you set in Serial Monitor as line ending to send? – Juraj Jan 31 at 9:16
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);
} 
  • 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 – nour albrmky Jan 31 at 19:11
  • @nouralbrmky Like in the code i posted, null input is probably newline. – Matej Jan 31 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 – nour albrmky Jan 31 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 Jan 31 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.