How to use the While loop inside the If condition

Please trying to blink an led inside the While loop with the if then condition to put it on and also another if then to put it off using the Bluetooth Smart phone device.

The first if then condition will put it on but the second condition will not put it off. Just continuing to blink forever.

Please how do I go about the code trying to upload code here but am a new user telling me I can't upload a file as a new user

This will explain how to post your code.

1 Like

A while loop will repeat itself until the condition it checks becomes false. The processor is running lines one by one. If you have it going in circles in one part of the code with a while loop then that's all that's going to happen. Nothing outside of that while loop can be reached unless the condition becomes false.

1 Like

To post images etc. you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can...

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

However, you don't need any of that to post code as code is text, which you can and should post as text, correctly formatted with code tags as per the instructions in reply #2.

This while() and if() blink an LED.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  unsigned long oldTime, newTime;
  while (1) {
    newTime = millis();
    if (newTime - oldTime >= 1000) {
      oldTime = newTime;
      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    }
  }
}

void loop() {}
//Alright thanks am having 8 LED bulb this how I //wrote the code.

#include <SoftwareSerial.h>

SoftwareSerial BT ( 2, 3 ); // ( TX pin 2, RX pin 3 )

String state;

int led1 = 12; // site light
int led2 = 11; // terrace light
int led3 = 10; // car park light
int led4 = 9; // first floor light
int led5 = 8; // second floor light
int led6 = 7; // third floor light
int led7 = 6; // fourth floor light
int led8 = 5; // fifth floor light




void setup() {

  BT.begin (9600);

  pinMode ( led1, OUTPUT );
  pinMode ( led2, OUTPUT );
  pinMode ( led3, OUTPUT );
  pinMode ( led4, OUTPUT );
  pinMode ( led5, OUTPUT );
  pinMode ( led6, OUTPUT );
  pinMode ( led7, OUTPUT );
  pinMode ( led8, OUTPUT );

  // puting off the relay switch to low state that is //off state

  digitalWrite ( led1, HIGH ); // site light off
  digitalWrite ( led2, HIGH ); // terrace light off
  digitalWrite ( led3, HIGH ); // carpark light off
  digitalWrite ( led4, HIGH ); // first floor light off
  digitalWrite ( led5, HIGH ); // second floor light 
  digitalWrite ( led6, HIGH ); // third floor light off
  digitalWrite ( led7, HIGH ); // fourth floor light off
  digitalWrite ( led8, HIGH ); // fifth floor light off


  // display for a dynamic effect when ON

  for( int i = 0; i < 1; i++) {


    digitalWrite ( led1, LOW ); //site light on
    delay (700);
    digitalWrite ( led2, LOW ); // terrace light on
    delay (700);
    digitalWrite ( led3, LOW ); //carpark light on
    delay (700);
    digitalWrite ( led4, LOW ); //first floor light on

    delay (700);
    digitalWrite ( led5, LOW ); // second floor light 
    delay (700);
    digitalWrite ( led6, LOW ); //third floor light on
    delay (700);
    digitalWrite ( led7, LOW ); // fourth floor light on
    delay (700);
    digitalWrite ( led8, LOW ); // fifth floor light on
    delay (700);


    // switching off the bulb from led 8

    digitalWrite ( led8, HIGH ); //site light on
    delay (700);
    digitalWrite ( led7, HIGH ); // terrace light on
    delay (700);
    digitalWrite ( led6, HIGH ); //carpark light on
    delay (700);
    digitalWrite ( led5, HIGH ); //first floor light on
    delay (700);
    digitalWrite ( led4, HIGH ); // second floor light 
     //on
    delay (700);
    digitalWrite ( led3, HIGH ); //third floor light on
    delay (700);

    digitalWrite ( led2, HIGH ); // fourth floor light 
   // on
    delay (700);
    digitalWrite ( led1, HIGH ); // fifth floor light on
    delay (700);
    // switching off the bulb from led 8
    
  }

    digitalWrite ( led1, LOW ); //site light on
    delay (700);
    digitalWrite ( led2, LOW ); // terrace light on
    delay (700);
    digitalWrite ( led3, LOW ); //carpark light on
    delay (700);
}

void loop() {

  while (BT.available()){
    delay (10);
    char jos = BT.read();
    state += jos;
  }

  if ( state.length()>0){
    Serial.println (state);



   if ( state == "site light on") {
      digitalWrite ( led1, LOW );
    }

    if ( state == "site light off") {
      digitalWrite ( led1, HIGH );
    }

    if ( state == "race light on") {
      digitalWrite ( led2, LOW );
    }

    if ( state == "race light off") {
      digitalWrite ( led2, HIGH);
    }

    if ( state == "car park light on") {
      digitalWrite ( led3, LOW );
    }

    if ( state == "car park light off") {
      digitalWrite ( led3, HIGH);
    }

    if ( state == "first floor light on") {
      digitalWrite ( led4, LOW );
    }


    if ( state == "first floor light off") {
      digitalWrite ( led4, HIGH );
    }

    if ( state == "second floor light on") {
      digitalWrite ( led5, LOW );
    }

    if ( state == "second floor light off") {
      digitalWrite ( led5, HIGH );
    }

    if ( state == "third floor light on") {
      digitalWrite ( led6, LOW);
    }

    if (state == "third floor light off") {
      digitalWrite ( led6, HIGH );
    }

    if ( state == "forth floor light on") {
      digitalWrite ( led7, LOW );
    }

    if ( state == "forth floor light off") {
      digitalWrite ( led7, HIGH );
    }

    if ( state == "fifth floor light on") {
      digitalWrite ( led8, LOW );
    }

    if ( state == "fifth floor light off") {
      digitalWrite ( led8, HIGH );
    }

    if ( state == "dynamic light on") {

      while!(state == "dynamic light off") {
        digitalWrite ( led1, LOW ); //site light on
        delay (700);
        digitalWrite ( led2, LOW ); // terrace light on
        delay (700);
        digitalWrite ( led3, LOW ); //carpark light on
        delay (700);
        digitalWrite ( led4, LOW ); //first floor light on
        delay (700);
        digitalWrite ( led5, LOW ); // second floor light 
       // on
        delay (700);
        digitalWrite ( led6, LOW ); //third floor light on
        delay (700);


        digitalWrite ( led7, LOW ); // fourth floor light 
        // on
        delay (700);
        digitalWrite ( led8, LOW ); // fifth floor light on
        delay (700);

    // switching off the bu;b from led 8

        digitalWrite ( led8, HIGH ); //site light on
        delay (700);
        digitalWrite ( led7, HIGH ); // terrace light on
        delay (700);
        digitalWrite ( led6, HIGH ); //carpark light on
        delay (700);
        digitalWrite ( led5, HIGH ); //first floor light on
        delay (700);
        digitalWrite ( led4, HIGH ); // second floor 
        // light on
        delay (700);
        digitalWrite ( led3, HIGH ); //third floor light 
        // on
        delay (700);
        digitalWrite ( led2, HIGH ); // fourth floor light 
        // on
        delay (700);
        digitalWrite ( led1, HIGH ); // fifth floor light 
        // on
        delay (700);
      }
    }


// switching off the bulb from led 8

    if ( state == "dynamic light off"){
      digitalWrite ( led1, HIGH ); // site light off
      digitalWrite ( led2, HIGH ); // terrace light off
      digitalWrite ( led3, HIGH ); // carpark light off
      digitalWrite ( led4, HIGH ); // first floor light off
      digitalWrite ( led5, HIGH ); // second floor light 
     // off
      digitalWrite ( led6, HIGH ); // third floor light off
      digitalWrite ( led7, HIGH ); // fourth floor light 
     // off
      digitalWrite ( led8, HIGH ); // fifth floor light off
    }
    
    
     

      state ="";
    }
  
}

Get rid of all the delays. Google "blink without delay arduino" or "blink with millis". Plenty of examples that illustrate the concept.

@joshkatech
Please insert your code using the code tags. You can go back and fix your original post.
Read the forum guidelines to see how to properly insert the code

How often do you think this loop iterates? What values of i will be less than 1?

Yes thanks see some but don't know how to implement them for my 8 LED bulbs. The ones I find are just like 1 or 2 led bulb. And am new to to this. Any sample where I can get will be of help.

Thanks.

This part is only for once. That's when the system start up at beginning. But inside the void loop I need it to continue to work with the use of the if then condition. Once the condition is meet.

1 Like

You need to learn the use of arrays, and to keep your comments in line with what your code is doing(many errors). More lines = more editing complexity, leading to errors and inconsistencies.
I rewrote your setup(). See if this makes sense to you:

#include <SoftwareSerial.h>
SoftwareSerial BT ( 2, 3 ); // ( TX pin 2, RX pin 3 )
String state;
const int NUMLEDS = 8;
const int LEDpins[NUMLEDS] = {12, 11, 10, 9, 8, 7, 6, 5};
enum LOCATIONS {SITE, TERRACE, CARPARK, FIRST, SECOND, THIRD, FOURTH, FIFTH};
const int LEDOFF = HIGH;
const int LEDON = LOW;

void setup() {
  BT.begin (9600);
  for (int index = 0; index < NUMLEDS; index++ ) {
    pinMode ( LEDpins[index], OUTPUT );
    digitalWrite ( LEDpins[index], LEDOFF ); //light off
  }
  // display for a dynamic effect when ON
  for (int index = 0; index < NUMLEDS; index++ ) {
    digitalWrite ( LEDpins[index], LEDON ); //light on
    delay (700);
  }
  for (int index = NUMLEDS - 1; index >= 0; index-- ) {
    digitalWrite ( LEDpins[index], LEDOFF ); //light off
    delay (700);
  }
  //now do the explicit stuff
  digitalWrite ( LEDpins[SITE], LOW ); //site light on
  delay (700);
  digitalWrite ( LEDpins[TERRACE], LOW ); // terrace light on
  delay (700);
  digitalWrite ( LEDpins[CARPARK], LOW ); //carpark light on
  delay (700);
}
void loop() {}

Concepts: Arrays, enumerations, and definition of constants that help make sense of the output state.

Wow this beautiful.
Much sense. Reduce bulgy lines of code.
Learnt another thing here.

Will implement this.

Can you please help me figure out my issues.

Thanks in advance.

Describe these issues, and how you imagine your code to work.

1 Like

My setup edit was intended as the first step of this. Once you understand those concepts, we can work on a loop() that is much more concise. Editing many lines of unnecessary code is painful and error prone. First, can you see how your loop() is that way?

No my issues is on the dynamic light code are.

With the code here.

void loop (){
if ( state == "dynamic light on") {
  while !( state == "dynamic light off"){
        digitalWrite ( led1, LOW ); //site light on
        delay (700);
        digitalWrite ( led2, LOW ); // terrace light on
        delay (700);
        digitalWrite ( led3, LOW ); //carpark light on
        delay (700);
        digitalWrite ( led4, LOW ); //first floor light on
        delay (700);
        digitalWrite ( led5, LOW ); // second floor light 
        // on
        delay (700);
        digitalWrite ( led6, LOW ); //third floor light on
        delay (700);

        digitalWrite ( led7, LOW ); // fourth floor light 
       // on
        delay (700);
        digitalWrite ( led8, LOW ); // fifth floor light on
        delay (700);
    // switching off the bulb from led 8

        digitalWrite ( led8, HIGH ); //site light on
        delay (700);
        digitalWrite ( led7, HIGH ); // terrace light on
        delay (700);
        digitalWrite ( led6, HIGH ); //carpark light on
        delay (700);
        digitalWrite ( led5, HIGH ); //first floor light on
        delay (700);
        digitalWrite ( led4, HIGH ); // second floor 
        // light on
        delay (700);
        digitalWrite ( led3, HIGH ); //third floor light 
       // on
        delay (700);
        digitalWrite ( led2, HIGH ); // fourth floor light 
       // on
        delay (700);
        digitalWrite ( led1, HIGH ); // fifth floor light 
       // on
        delay (700);
      }
    }
  if ( state == "dynamic light off"){
      digitalWrite ( led1, HIGH ); // site light off
      digitalWrite ( led2, HIGH ); // terrace light off
      digitalWrite ( led3, HIGH ); // carpark light off
      digitalWrite ( led4, HIGH ); // first floor light off
      digitalWrite ( led5, HIGH ); // second floor light 
     // off
      digitalWrite ( led6, HIGH ); // third floor light off
      digitalWrite ( led7, HIGH ); // fourth floor light 
      // off
   }
   state ="";
    }
  
}     

Now the issue is once condition for if then is meet it will keep on working no condition will work after that again until I off the whole system.

And I imagine or want it to work in a way that once I press if dynamic light of the whole light should go off after putting on the dynamic light on.

well, if the if() is satisfied, the while() will be, correct? So while() continues forever, because state never updates while you're in the while loop.

So what can be used, or how to write the code?

Is this a button press to turn OFF the LEDs?

A button from a smart phone. All is controlled from a Smart phone