Go Down

Topic: Code Won't Repeat (Read 406 times) previous topic - next topic

biggles2206

Mar 20, 2013, 08:20 am Last Edit: Mar 20, 2013, 08:28 am by Coding Badly Reason: 1
Quote

 /*
* serial_control.pde
* -------0
----------
* Turns the LED 13 ON and OFF depending on what is received
* through the serial port.
*
* http://spacetinkerer.blogspot.com
*/

#define LED 13
int LED2 = 12;

int input = 0;       // variable to keep the data from the serial port

void setup() {
 pinMode(LED,OUTPUT);    // declare the LED's pin as output
 
 Serial.begin(9600);        // connect to the serial port
}

void loop () {
 while(Serial.available()){
 input = Serial.read();      // read the serial port

 // if the input is '1' turn the LED ON, if '0' turn it OFF
 if (input == '1' ) {
    Serial.println("LEDS are Now On");
   digitalWrite(LED , HIGH);
   delay(500);
   digitalWrite(LED , LOW);
 
 }
else if(input == 0){
  Serial.print("Program Terminated.");
 
}

else{
 Serial.println("Sorry, that is not a valid input.");
Serial.println("You tried inputing:");
Serial.println(input);
Serial.println("Valid Commands are 0 and 1.");
}
 }
 
}



The code won't go on a loop in the if statement, why?
Code Add Aurduino Times Gaming Equals BOSS

AWOL

Quote
The code won't go on a loop in the if statement, why?

Because you've read everything in the serial input buffer?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Grumpy_Mike

Ready ge how to use this forum sticky post and use the correct code tags when posting code.

Code: [Select]
 else if(input == 0){
What about the quote marks?

UKHeliBob

You don't describe what happens when you run the program but here are some observations.

input is defined as an int.
Later in the code you check to see whether input is a char.  ('1')
Later you check to see whether it is zero. (0)
Both of them can't be right, can they ?

What line ending(s) have you got set in the Serial monitor ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up