Arduino Forum

Using Arduino => Programming Questions => Topic started by: DrakeHands on Jul 31, 2016, 07:25 am

Title: Please Help on Serial Program Code
Post by: DrakeHands on Jul 31, 2016, 07:25 am
Hello all, very new to Arduino here.

Here I have a simple serial program which is intended to display "the lights are off" when 0 is input into the serial monitor, and "the lights are on" when 1 is input. However, the program simply jumps to the else case when i run this. No matter how much I tweak it.

My only guess is that it's something to do with bytes and the type of input the Serial Monitor accepts???

Code listed below. If you find any errors, I'd appreciate it enormously if you'd also go to the liberty to describe why the correct statement(s) needs to be implemented.

Code: [Select]
void setup() {
 Serial.begin(9600);
 pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
     while(Serial.available() > 0){
       int lightControl = Serial.read();
       if(lightControl == 0)
         {
         digitalWrite(13, LOW);
         Serial.println("Light is off fam");
         }
       else if(lightControl == 1)
         {
         digitalWrite(13, HIGH);
         Serial.println("THE LIGHTS ARE ON ");
         }
       else
         {
         Serial.println("stop writing random numbers pls");
         }
     }
 }


Moderator edit: [code] [/code] tags added.
Title: Re: Please Help on Serial Program Code
Post by: OldSteve on Jul 31, 2016, 07:34 am
You really should have read How to use this forum (http://forum.arduino.cc/index.php/topic,148850.0.html) before posting.

ie Your code and any error messages should always be placed between [code]code tags[/code]. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :)
Title: Re: Please Help on Serial Program Code
Post by: DrakeHands on Jul 31, 2016, 07:35 am
Hah, I'm so stupid to forget Serial only accepts ASCII chars.

Problem resolved!

Thanks to all who took the liberty to read this and not respond.

Title: Re: Please Help on Serial Program Code
Post by: Coding Badly on Jul 31, 2016, 07:41 am
Quote
I've been working over half an hour on this...
Your amount of cuss is incongruous to effort exerted.  Post censored.

Title: Re: Please Help on Serial Program Code
Post by: OldSteve on Jul 31, 2016, 08:39 am
Thanks to all who took the liberty to read this and not respond.
Sounds like a back-handed thank you.

I had actually typed a reply telling you what was wrong, including a 'fixed' version of your code, when you posted reply #2, so I aborted.
Title: Re: Please Help on Serial Program Code
Post by: Robin2 on Jul 31, 2016, 09:49 am
And, in any case, what is wrong with reading a post and not responding?

It's not as if the OP is paying for his dinner and being ignored by the waiter :)

...R