While loop doesn't break

Hello everyone! I've been trying for a few days to solve this one problem, but nothing seems to be working. I've tried reading other posts with similar issues and implementing the solutions proposed therel but still can't figure out what i'm doing wrong since the code does not work the way i intend it to. I'm hoping a more experienced used can help me out.
Here's the issue:
As part of a larger project i'm writing a piece of code that involves two LEDs and should do the following:
-If LED1 is on, then turn on LED2.
-If not, LED2 is off.
I managed to get the first part working, but afterwards, even if LED1 is turned on I can't get LED2 to do the same. Suggestions?
Here's the code:

int led1 = 12;
int led2 = 8; 
int led1state;

void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2,OUTPUT);

}

void loop() {
int state = Serial.parseInt();
  if (state== 2){ // turn led1 on
  digitalWrite(led1,HIGH);
  }
  led1state=digitalRead(led1);
while(led1state==HIGH){ // turn led2 on while led1 is also on
  int state = Serial.parseInt();
  digitalWrite(led2,HIGH);
  if (state==3){ //turn led1 off
    digitalWrite(led1,LOW);
  }
  led1state=digitalRead(led1); // update while condition to break out of the loop if no longer true
}

}

I've tried using break; inside the if statement, but it yields the same practical result: LED2 does not turn off even if LED1 does.
Thanks in adavance!

SOLVED
Working sketch follows

int led1 = 12;
int button = 11;
int led2 = 8;
int led1state;

const unsigned long interval = 5000;
unsigned long currentM= 0;
unsigned long previouscheckM =0;

void TimeCheck(){
 if(millis()-previouscheckM>=interval && digitalRead(led2)==HIGH){
    digitalWrite(led2,LOW);
    Serial.println("off");
  }
}

void setup() {
Serial.begin(9600);
 pinMode(led1, OUTPUT);
 pinMode(led2,OUTPUT);
pinMode(11,INPUT_PULLUP);
}

void loop() {
  int state=Serial.parseInt();
  if (digitalRead(button)==0){
  digitalWrite(led2, HIGH);
  Serial.println("on");
   previouscheckM=millis();
  }
  TimeCheck();
  if (state== 2){
  digitalWrite(led1,HIGH);
  }
  led1state=digitalRead(led1);
while(led1state==HIGH){ 
  int state = Serial.parseInt();
  digitalWrite(led2,HIGH);
  if (state==3){ 
    digitalWrite(led1,LOW);
    digitalWrite(led2, LOW);
  }
  led1state=digitalRead(led1); 
}
  
}

You never set led2 to LOW. If that must happen when led1 goes LOW. you need to add it in the if block

  if (state==3){ //turn led1 off
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
  }

Oh, i see. However, shouldn't led2 automatically be turned off when the loop breaks? Since it is only turned on when the loop condition is true.

You only ever turn led2 on so why would it turn off ?

The way i understood it, led2 turned on only when led1 was turned on, so i thought once that was no longer true led2 would turn off. Turns out my understanding was wrong, thanks for teaching me!
And this also solve my problem, so thanks for that too!

I am glad that you got it working

Please post your working sketch

Right away!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.