Go Down

Topic: Please Help on Serial Program Code (Read 679 times) previous topic - next topic

DrakeHands

Jul 31, 2016, 07:25 am Last Edit: Jul 31, 2016, 07:35 am by Coding Badly
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.

OldSteve

You really should have read How to use this forum 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. :)
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

DrakeHands

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.


Coding Badly

Quote
I've been working over half an hour on this...
Your amount of cuss is incongruous to effort exerted.  Post censored.


OldSteve

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.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up