Go Down

Topic: LED's controlled via bluetooth  (Read 245 times) previous topic - next topic

wuldiba

I am an absolute noob who has literally got around 10 hours of experience. so please forgive my ignorance.
I am currenty making a railway signal 4 aspect (4leds)....ive got everything working as it should via bluetooth app, all working individually and also in sequence going through the light sequence.....however in (case 5), how do i get it to keep looping until another case is  received from bluetooth?....it is only doing it once but i would like it to keep looping until another case is inputed.....im positive it is really simple, but as i said im totally new.
thanks in advance.
will



Code: [Select]
void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT); //red led          (case 1)
pinMode(9, OUTPUT); //singleyellow led (case 2)
pinMode(10, OUTPUT); //twoyellows led  (case 3)
pinMode(11, OUTPUT);//green led        (case 4)
                    //                 (case 5 led sequence)
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
  if(Serial.available()>0)
   {   
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
      {
        case '1': digitalWrite(8, HIGH);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);break; // when red is pressed on the app on your smart phone
                 
        case '2': digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);break; // when yellow is pressed on the app on your smart phone

        case '3': digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, HIGH);
                  digitalWrite(11, LOW);break; // when twoyellow is pressed on the app on your smart phone

        case '4': digitalWrite(8, LOW);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, HIGH);break; // when green is pressed on the app on your smart phone

        case '5': digitalWrite(8, HIGH);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, HIGH);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, HIGH)
                  delay(5000);break;           // sequence
       
      }
      Serial.println(data);
   }
   delay(50);
}

bos1714

Hello there!

With the current structure of your code, the LED sequence associated with each case is only happening when some data is sent via the bluetooth. With no data being sent, the program just sits in the loop without any actions until the next case is sent via bluetooth.

To keep the case 5 sequence running, the following code may help:

Code: [Select]

......
  case '5':
    //your sequence here
    continuousMode = 1; //add a global integer called "continuousMode" before void setup()
    //add "continuousMode = 0;" to the other case sequences
    break;           // sequence
    }
  Serial.println(data);
  }
  if(continuousMode == 1)
  {
    //your LED sequence here
  }
  else
  {
  delay(50);
  }
}


This case sets a flag that is checked after the switch statement, and will keep the LED sequence going even without data being sent. Try it and let me know
Time line? Time isn't made out of lines. It is made out of circles. That is why clocks are round.

wuldiba

unfortunately its still the same, just runs once......but im more than positive that its me not doing something simple......heres what i tried to implement with your suggestion..........thanks for your time BTW>
Code: [Select]
int continuousMode;

void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT); //red led          (case 1)
pinMode(9, OUTPUT); //singleyellow led (case 2)
pinMode(10, OUTPUT); //twoyellows led  (case 3)
pinMode(11, OUTPUT);//green led        (case 4)
                    //                 (case 5 led sequence)
}
void loop() {
  // put your main code here, to run repeatedly:
 
  if(Serial.available()>0)
   {   
      char data= Serial.read(); // reading the data received from the bluetooth module
      switch(data)
      {
        case '1': digitalWrite(8, HIGH);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  continuousMode = 0;
                  break; // when red is pressed on the app on your smart phone
                 
        case '2': digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  continuousMode = 0;
                  break; // when yellow is pressed on the app on your smart phone

        case '3': digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, HIGH);
                  digitalWrite(11, LOW);
                  continuousMode = 0;
                  break; // when twoyellow is pressed on the app on your smart phone

        case '4': digitalWrite(8, LOW);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, HIGH);
                  continuousMode = 0;
                  break; // when green is pressed on the app on your smart phone

        case '5': digitalWrite(8, HIGH);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, HIGH);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, HIGH);
                  delay(5000);       
                  break;      //sequence
                           
 }
  Serial.println(data);
  }
  if(continuousMode == 1)
  {
                  digitalWrite(8, HIGH);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, LOW);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, HIGH);
                  digitalWrite(10, HIGH);
                  digitalWrite(11, LOW);
                  delay (5000);
                  digitalWrite(8, LOW);
                  digitalWrite(9, LOW);
                  digitalWrite(10, LOW);
                  digitalWrite(11, HIGH);
                  delay(5000);
                 
  }
  else
  {
  delay(50);
  }
}

wuldiba

sorry, found it...i forgot to add the continuousMode = 1; to case 5 like you suggested

thanks again my friend.

Go Up