Pages: [1]   Go Down
Author Topic: Code Won't Repeat  (Read 360 times)
0 Members and 1 Guest are viewing this topic.
England
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: March 20, 2013, 02:28:30 am by Coding Badly » Logged

Code Add Aurduino Times Gaming Equals BOSS

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The code won't go on a loop in the if statement, why?
Because you've read everything in the serial input buffer?
Logged

"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.

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 604
Posts: 33421
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4089
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: