Go Down

Topic: if loop (Read 3160 times) previous topic - next topic

welchsc

I am controlling a light with an Arduino UNO and a 4 channel 5v relay board.  The inserted sketch works well to turn on and off the light when I press the letter "a" in the serial monitor.  It also turns off when I press any other key.  What I am trying to do is when I type the letter "b" I want it to cycle on and off with a delay I specify.  It seems when I use another "if" statement it doesn't work properly.  I have also tried "else if".  I'm just not sure how to code it properly.  I would also like to be able to add programming to control other keys to make it blink at different rates based on the key typed into the serial monitor.  Any help would be greatly appreciated.
Thanks.

Code: [Select]
int light = 13;
#define a = HIGH;

void setup(){
  Serial.begin(9600);

  //Set all the pins we need to output pins

  pinMode(13, OUTPUT);
}
 
  void loop (){
  if (Serial.available()) {

    //read serial as a character
    char ser = Serial.read();
 
  if(ser == 'a') {
   
    digitalWrite (light, HIGH);
    delay (1000);
   
  }
  else{
   
    digitalWrite (light, LOW);
    delay (1000);
  }
  }

  }

AWOL

Post the code you say doesn't work.

welchsc

I thought I posted it.  I used the insert code link.  Do you want me to post it in the body of the msg directly?

luisilva

Post the code you say doesn't work.
It seems that this suggestion is very clever. If you don't supply us with the code that doesn't work how do you want that someone find the error in that code?

luisilva

I thought I posted it.  I used the insert code link.  Do you want me to post it in the body of the msg directly?
Maybe the problem is saying
(...)  The inserted sketch works well (...)

welchsc

sorry here it is.

int light = 13;
#define a = HIGH;

void setup(){
 Serial.begin(9600);

 //Set all the pins we need to output pins

 pinMode(13, OUTPUT);
}
 
 void loop (){
 if (Serial.available()) {

   //read serial as a character
   char ser = Serial.read();
 
 if(ser == 'a') {
   
   digitalWrite (light, HIGH);
   delay (1000);
   
 }
 else{
   
   digitalWrite (light, LOW);
   delay (1000);
 }
 }

 }

luisilva

What is this:
Quote
Code: [Select]
#define a = HIGH;
?

welchsc

sorry my brain isn't working.  Here is the code that isn't working.
Code: [Select]
int light = 13;
#define a = HIGH;

void setup(){
  Serial.begin(9600);

  //Set all the pins we need to output pins

  pinMode(13, OUTPUT);
}
  
  void loop (){
  if (Serial.available()) {

    //read serial as a character
    char ser = Serial.read();
  
  if(ser == 'a') {
    
    digitalWrite (light, HIGH);
    delay (1000);
  }
    
  else if(ser == 's') {
  
    digitalWrite (light, HIGH);
    delay (1000);
    digitalWrite (light, LOW);
    delay (1000);  
  }
  
  else{
    
    digitalWrite (light, LOW);
    delay (1000);
  }
  }
  }

Delta_G

Code: [Select]
#define a = HIGH;

What's that supposed to mean?
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

KenF

Code: [Select]
#define a = HIGH;

What's that supposed to mean?
or even
Code: [Select]

 if(ser == 'HIGH')
:)

luisilva

or even
Code: [Select]

 if(ser == 'HIGH')
:)
Nop! It means:
Code: [Select]

 if(ser == '= HIGH')


Don't?

UKHeliBob

Quote
It also turns off when I press any other key.
Which lines of code will be executed if the character received is not 'a' or 's' ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

welchsc

i removed #define a HIGH; as I'm noticing it's not needed.  I am stuck trying to make the letter "b" loop turning the light on and off until I type another key.

welchsc

Sorry I meant the letter "s" not "b"

luisilva

i removed #define a HIGH; as I'm noticing it's not needed.  I am stuck trying to make the letter "b" loop turning the light on and off until I type another key.
Is that what you want to do?
Try this:
Code: [Select]
int light = 13;

boolean flick = false;

void setup(){
  Serial.begin(9600);

  //Set all the pins we need to output pins

  pinMode(13, OUTPUT);
}

void loop (){
  if (Serial.available()) {

    //read serial as a character
    char ser = Serial.read();

    if(ser == 'a') {

      digitalWrite (light, HIGH);
      delay (1000);
      flick = false;
    }

    else if(ser == 's') {
      flick = true;

    }

    else{
      flick = false;
      digitalWrite (light, LOW);
      delay (1000);
    }
  }
 
  if (flick == true) {
    digitalWrite (light, HIGH);
    delay (1000);
    digitalWrite (light, LOW);
    delay (1000); 
  }   
}


Go Up