Link to home
Start Free TrialLog in
Avatar of Andrea Edwards
Andrea Edwards

asked on

Serial Monitor - code that compares the incoming byte data to characters 1 and 0

Hi

I am a beginner to the arduino. To cut a long story short I have the code Serial.read which appears to read the 8 bit ascii value of a character.. This value is read into an int variable called Byte Received

Later in the code there is this
   
    if(ByteReceived == '1') // Single Quote! This is a character.
    {
      digitalWrite(led,HIGH);
      Serial.print(" LED ON ");
    }
    
    if(ByteReceived == '0')
    {
      digitalWrite(led,LOW);
      Serial.print(" LED OFF");
    }

Open in new window

   
 

I do not understand the if statements e,g,
if(ByteReceived == '1')

Open in new window


ByteReceived is an int and a value of 0 is null and a value of 1 is start of heading. Why are we comparing to 0 and 1 and why is it a character comparison ?

Furthermore I never get LED ON or LED OFF displayed but an led which is not pin 13 does flash .


Any help is much appreciated

Thanks
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

if(ByteReceived == '1')

Open in new window

Here, you compare ByteReceived with the character '1'.
And if you refer to an ASCII table, that character's numeric value is 49.
Same goes for the character '0', it's numeric value is 48 (and not 0, neither NULL).

See this article on wikipedia.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andrea Edwards
Andrea Edwards

ASKER

Thanks for explaning that the smaller operand is expanded to the same as the larger operand. I didnt see how that comparison could be possible. I have another problem with the code but I will ask another question on that
thank you