Case Statement

Hello to everyone.I have a question.I want to controll 2 leds and i want to run simultaneously.I use the Case statement but i am not happy with the result.I want my 2 leds to run simultaneously, It runs for 1 sec and then stop.My goal is to run over and over again.How to do that?thanks in advance

int const White_pin_right = 2; 
int const White_pin_left = 8;  




void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  
  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
      case 'X':
       digitalWrite(White_pin_right, HIGH);
       digitalWrite(White_pin_left, HIGH);
       delay(1000);
       digitalWrite(White_pin_right, LOW);
       digitalWrite(White_pin_left, LOW);
       delay(1000);
       Serial.println("Lights ON");
       break;
      case 'x':
       digitalWrite(White_pin_right, LOW);
       digitalWrite(White_pin_left, LOW);
       Serial.println("Lights OFF");
       break;
       default: 
        // if nothing matches, do nothing
        break;
     }
  }
}

Don't use delay - a one second delay is enough time for the serial input buffer to overflow many times, even at only 9600 bits per second.

You wait for serial. When you get it, and it is 'X' turn the lights on for one second. Then turn them off for one second and wait for more serial. If serial is an 'x', do nothing for one second since the lights are already off.

Just what do you want to do?

It seems weird to me to use an uppercase 'X' to turn things on and a lowercase 'x' to turn it off. Is there a reason for that choice? I'd change it to something like:

int incomingByte = toupper(Serial.read());   // No longer case sensitive
//...
switch (incomingByte) {
   case 'X':                    // turn on
      // case code...

   case 'O':                    // Turn off
      // case code...
}

Clearly, it's just a matter of choice...

Have a look at the demo Several Things at a Time to see how to manage timing using millis() rather than delay().

And the examples in Serial Input Basics illustrates how to receive serial data without blocking other actions or losing data.

...R

My goal is to controll the leds with smartphone(bluetooth). When press the button(case X on arduino)the led on. When press the button(case x on arduino)the led off When the leds are on i want to blink simultaneously.Can anyone help me please?

Once you receive 'X' the leds should blink until you receive 'x', right? You need to blink outside the check for serial input, set or clear a flag when you get an x.

int const White_pin_right = 2;
int const White_pin_left = 8; 

boolean blinkingOn = false;  // true => lights should be blinking

void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
    case 'X':
      blinkingOn = true;    // turn blinking on
      break;

    case 'x':
      blinkingOn = false;    // turn blinking off

      digitalWrite(White_pin_right, LOW);
      digitalWrite(White_pin_left, LOW);
      Serial.println("Lights OFF");
      break;
    default:
      // if nothing matches, do nothing
      break;
    }
  }

  // if blinking turned on
  if( blinkingOn )
  {
    digitalWrite(White_pin_right, HIGH);
    digitalWrite(White_pin_left, HIGH);
    delay(1000);
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    delay(1000);
    Serial.println("Lights ON");

  }
}

[quote author=Blue Eyes link=msg=2562118 date=1452627965] Once you receive 'X' the leds should blink until you receive 'x', right? You need to blink outside the check for serial input, set or clear a flag when you get an x.

int const White_pin_right = 2;
int const White_pin_left = 8; 

boolean blinkingOn = false;  // true => lights should be blinking

void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
    case 'X':
      blinkingOn = true;    // turn blinking on
      break;

    case 'x':
      blinkingOn = false;    // turn blinking off

      digitalWrite(White_pin_right, LOW);
      digitalWrite(White_pin_left, LOW);
      Serial.println("Lights OFF");
      break;
    default:
      // if nothing matches, do nothing
      break;
    }
  }

  // if blinking turned on
  if( blinkingOn )
  {
    digitalWrite(White_pin_right, HIGH);
    digitalWrite(White_pin_left, HIGH);
    delay(1000);
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    delay(1000);
    Serial.println("Lights ON");

  }
}

[/quote]

Thank you for your help.It says Error Compilling

What else does it say?

AWOL: What else does it say?

Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

BFD: core\main.cpp.o: invalid string offset 4293984260 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 65539 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 131075 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 196611 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 262147 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 327683 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 393219 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 458755 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 589827 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 720899 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 786435 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 917507 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 1048579 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 1179651 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 1310723 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 1245187 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 262178 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 327714 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 393250 >= 135 for section `tab'

BFD: core\main.cpp.o: invalid string offset 458770 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 4293984260 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 65539 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 131075 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 196611 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 4293984256 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 262147 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 327683 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 393219 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 458755 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 589827 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 720899 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 786435 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 917507 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 1048579 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 1179651 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 1310723 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 1245187 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 262178 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 327714 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 393250 >= 135 for section `tab'

BFD: core\core.a(main.cpp.o): invalid string offset 458770 >= 135 for section `tab'

c:/program files (x86)/arduino-1.6.7/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/lib/avr5/crtm328p.o:(.init9+0x0): undefined reference to `main'

collect2.exe: error: ld returned 1 exit status

exit status 1 Error compiling.

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

jack1992: Can anyone help me please?

I thought I had in the Reply immediately before yours ! Did you study it?

...R

[quote author=Blue Eyes link=msg=2562118 date=1452627965] Once you receive 'X' the leds should blink until you receive 'x', right? You need to blink outside the check for serial input, set or clear a flag when you get an x.

int const White_pin_right = 2;
int const White_pin_left = 8; 

boolean blinkingOn = false;  // true => lights should be blinking

void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
    case 'X':
      blinkingOn = true;    // turn blinking on
      break;

    case 'x':
      blinkingOn = false;    // turn blinking off

      digitalWrite(White_pin_right, LOW);
      digitalWrite(White_pin_left, LOW);
      Serial.println("Lights OFF");
      break;
    default:
      // if nothing matches, do nothing
      break;
    }
  }

  // if blinking turned on
  if( blinkingOn )
  {
    digitalWrite(White_pin_right, HIGH);
    digitalWrite(White_pin_left, HIGH);
    delay(1000);
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    delay(1000);
    Serial.println("Lights ON");

  }
}

[/quote]

i try your code.The result is when i press X button the leds turn on at the same time but when i press x button continue to blinking.They don't turn off.

i modify the code but with no success.I take the same results.Can anyone help me please?

int const White_pin_right = 7;
int const White_pin_left = 8; 

boolean blinkingOn = false;  // true => lights should be blinking

void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
    case 'X':
      blinkingOn = true;    // turn blinking on
      break;
    case 'x':
      blinkingOn = false;    // turn blinking off
      break;
    default:
      // if nothing matches, do nothing
      break;
    }
  }
  // if blinking turned on
  if( blinkingOn )
  {
    digitalWrite(White_pin_right, HIGH);
    digitalWrite(White_pin_left, HIGH);
    delay(1000);
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    delay(1000);
    Serial.println("Lights ON");
  }
  else{
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    Serial.println("Lights OFF");
      
  }
}

What do you mean by "same results"? The same as reply #9?

I compiled the code in your last post and got

Sketch uses 2,518 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 205 bytes (10%) of dynamic memory, leaving 1,843 bytes for local variables. Maximum is 2,048 bytes.

AWOL: What do you mean by "same results"? The same as reply #9?

I compiled the code in your last post and got

Sketch uses 2,518 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 205 bytes (10%) of dynamic memory, leaving 1,843 bytes for local variables. Maximum is 2,048 bytes.

No the same results as the #11.I fix the code on post #9.I forgot to write.

Can help me anyone, please?

jack1992: Can help me anyone, please?

Post the code you have now and a summary of the problem(s). I'm not wading backwards through this whole post and guessing what code you have and what "the same problem" means.

Hello i have this code and i want to blink the leds at the same time via bluetooth.It blinks at the same time but it doesn't stop(uppercase X turn on and lowercase x turn off).thank you for your help

int const White_pin_right = 7;
int const White_pin_left = 8; 

boolean blinkingOn = false;  // true => lights should be blinking

void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
    case 'X':
      blinkingOn = true;    // turn blinking on
      break;
    case 'x':
      blinkingOn = false;    // turn blinking off
      break;
    default:
      // if nothing matches, do nothing
      break;
    }
  }
  // if blinking turned on
  if( blinkingOn )
  {
    digitalWrite(White_pin_right, HIGH);
    digitalWrite(White_pin_left, HIGH);
    delay(1000);
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    delay(1000);
    Serial.println("Lights ON");
  }
  else{
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    Serial.println("Lights OFF");
      
  }
}

Put EVERY { on a new line. Use Tools + Auto Format to fix that crappy looking code.

Describe what the code ACTUALLY does. "It doesn't work" is just soooooo lame.

When i press X(upercase) the leds start to blink simultaneously When i press x(lowercase) the leds doesn't stop

the app that i use is this https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=el

When i say press i mean the triangle that it has in the app.

int const White_pin_right = 7;
int const White_pin_left = 8;

boolean blinkingOn = false;  // true => lights should be blinking

void setup() {
  pinMode(White_pin_right, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(White_pin_left, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

  Serial.begin(9600);

}

void loop() {

  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
      case 'X':
        blinkingOn = true;    // turn blinking on
        break;
      case 'x':
        blinkingOn = false;    // turn blinking off
        break;
      default:
        // if nothing matches, do nothing
        break;
    }
  }
  // if blinking turned on
  if ( blinkingOn )
  {
    digitalWrite(White_pin_right, HIGH);
    digitalWrite(White_pin_left, HIGH);
    delay(1000);
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    delay(1000);
    Serial.println("Lights ON");
  }
  else {
    digitalWrite(White_pin_right, LOW);
    digitalWrite(White_pin_left, LOW);
    Serial.println("Lights OFF");

  }
}