While statement question

I’m using a

while(digitalRead(buttona)==LOW);
digitalWrite(led,HIGH);
delay(1000)
digitalWrite(led,LOW);

while(digitalRead(buttonb)==LOW);
digitalWrite(led,HIGH);
delay(1000)
digitalWrite(led,LOW);

(This is an example)
statement, and was wondering why would buttona (from the example above) only be allowed to go first, instead of buttonb?

Also having an issue with once pressed, I cannot press it again (digitally not physically).

Any suggestions / code rewrites would be appreciated! :slight_smile:

//Night light sketch
//Made by Alex (AyItsHusky) 2018
//Arduino coded!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TimeLib.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int tenmin = 4; // Button slot
int thirtymin = 3; // Button slot
int led = 13; //LED slot
int start = 5; //Button slot
int minutes = 10; //start min
int seconds = 0; //start seconds
int hours = 0; //start hours
int minutea = 30; //start min (thirtymin)
int seconda = 0; //start seconds (thirtymin)
int houra = 0; //start hours (thirtymin)

void startz() {
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Yllw to setup");
 lcd.setCursor(0,1);
 lcd.print("Blue to begin");
}
void stepDown() {
 if (seconds > 0) {
 seconds -= 1;
 } else {
 if (minutes > 0) {
 seconds = 59;
 minutes -= 1;
 } else {
 if (hours > 0) {
 seconds = 59;
 minutes = 59;
 hours -= 1;
 } else {
 trigger();
 }
 }
 }
}

void stepDowna() {
 if (seconda > 0) {
 seconda -= 1;
 } else {
 if (minutea > 0) {
 seconda = 59;
 minutea -= 1;
 } else {
 if (houra > 0) {
 seconda = 59;
 minutea = 59;
 houra -= 1;
 } else {
 trigger();
 }
 }
 }
}
 
void trigger() {
 lcd.clear(); // clears the screen and buffer
 lcd.setCursor(5, 1); // set timer position on lcd for end.
 
 lcd.print("ok done");
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(500);
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(500);
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(500);
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(1000);
 loop();
}

void setup()
{
  pinMode(led,OUTPUT); // LED output
  pinMode(thirtymin,INPUT); // Button input
  pinMode(tenmin,INPUT); // Button input
  pinMode(start,INPUT); //Button input
  lcd.begin(16,2);
  lcd.clear();
  lcd.print("Night light proj");
  lcd.setCursor(0,1);
  lcd.print("By Alex");
  delay(5000);
  startz();
}

void loop()
{
  while(digitalRead(start)==LOW);
  digitalWrite(led,HIGH);
  delay(100);
  digitalWrite(led,LOW);
  
  while(digitalRead(tenmin)==LOW);
  digitalWrite(led,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Remaining time:");
  while (hours > 0 || minutes > 0 || seconds >= 0) {
 
  lcd.setCursor(4, 2);
 
  (hours < 10) ? lcd.print("0") : NULL;
  lcd.print(hours);
  lcd.print(":");
  (minutes < 10) ? lcd.print("0") : NULL;
  lcd.print(minutes);
  lcd.print(":");
  (seconds < 10) ? lcd.print("0") : NULL;
  lcd.print(seconds);
  lcd.display();
  stepDown();
  delay(1000);
  }
  digitalWrite(led,LOW);
  
  while(digitalRead(thirtymin)==LOW);
  digitalWrite(led,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Remaining time:");
  while (houra > 0 || minutea > 0 || seconda >= 0) {
 
  lcd.setCursor(4, 2);
 
  (houra < 10) ? lcd.print("0") : NULL;
  lcd.print(houra);
  lcd.print(":");
  (minutea < 10) ? lcd.print("0") : NULL;
  lcd.print(minutea);
  lcd.print(":");
  (seconda < 10) ? lcd.print("0") : NULL;
  lcd.print(seconda);
  lcd.display();
  stepDowna();
  delay(1000);
  digitalWrite(led,LOW);

}
}
while(digitalRead(buttona)==LOW);

See the semicolon at the end of that line?
That's where the while statement ends.
Because there is no code there, nothing is actually happening while that buttonPin is LOW.
The Arduino just waits there until the buttonPin becomes HIGH again.
If you want the LED to flash inside that while statement, then use curly braces around that LED code block.

 while (digitalRead(buttona) == LOW) {
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
  }

I assume you have a physical pull up resistor on the switch.
An easier way (no pull up resistor) is to enable pull up in software.
pinMode(thirtymin, INPUT_PULLUP);
Leo..

Thanks for the response, but when I altered the main code (bottom of original post), I pushed my start button (with no use so I will remove, but from now I know it has nothing to do with this error), and it goes to my ten min button which was weird, and also on reset of my arduino it automatically turned the light on and wouldn't go off.

but when I altered the main code (bottom of original post)

If you have problems after that, please post your complete (new) code.

Merry Christmas.

//Night light sketch
//Made by Alex (AyItsHusky) 2018
//Arduino coded!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TimeLib.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int tenmin = 4; // Button slot
int thirtymin = 3; // Button slot
int led = 13; //LED slot
int start = 5; //Button slot
int minutes = 10; //start min
int seconds = 0; //start seconds
int hours = 0; //start hours
int minutea = 30; //start min (thirtymin)
int seconda = 0; //start seconds (thirtymin)
int houra = 0; //start hours (thirtymin)

void startz() {
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Yllw to setup");
 lcd.setCursor(0,1);
 lcd.print("Blue to begin");
}
void stepDown() {
 if (seconds > 0) {
 seconds -= 1;
 } else {
 if (minutes > 0) {
 seconds = 59;
 minutes -= 1;
 } else {
 if (hours > 0) {
 seconds = 59;
 minutes = 59;
 hours -= 1;
 } else {
 trigger();
 }
 }
 }
}

void stepDowna() {
 if (seconda > 0) {
 seconda -= 1;
 } else {
 if (minutea > 0) {
 seconda = 59;
 minutea -= 1;
 } else {
 if (houra > 0) {
 seconda = 59;
 minutea = 59;
 houra -= 1;
 } else {
 trigger();
 }
 }
 }
}
 
void trigger() {
 lcd.clear(); // clears the screen and buffer
 lcd.setCursor(5, 1); // set timer position on lcd for end.
 
 lcd.print("ok done");
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(500);
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(500);
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(500);
 digitalWrite(led,HIGH);
 delay(500);
 digitalWrite(led,LOW);
 delay(1000);
 startz();
 loop();
}

void setup()
{
  pinMode(led,OUTPUT); // LED output
  pinMode(thirtymin,INPUT); // Button input
  pinMode(tenmin,INPUT); // Button input
  pinMode(start,INPUT); //Button input
  lcd.begin(16,2);
  lcd.clear();
  lcd.print("Night light proj");
  lcd.setCursor(0,1);
  lcd.print("By Alex");
  delay(5000);
  startz();
}

void loop()
{
  while (digitalRead(tenmin)==LOW) {
  digitalWrite(led,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Remaining time:");
  while (hours > 0 || minutes > 0 || seconds >= 0) {
 
  lcd.setCursor(4, 2);
 
  (hours < 10) ? lcd.print("0") : NULL;
  lcd.print(hours);
  lcd.print(":");
  (minutes < 10) ? lcd.print("0") : NULL;
  lcd.print(minutes);
  lcd.print(":");
  (seconds < 10) ? lcd.print("0") : NULL;
  lcd.print(seconds);
  lcd.display();
  stepDown();
  delay(1000);
  }
  digitalWrite(led,LOW);
  }
  
  while(digitalRead(thirtymin)==LOW) {
  digitalWrite(led,HIGH);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Remaining time:");
  while (houra > 0 || minutea > 0 || seconda >= 0) {
 
  lcd.setCursor(4, 2);
 
  (houra < 10) ? lcd.print("0") : NULL;
  lcd.print(houra);
  lcd.print(":");
  (minutea < 10) ? lcd.print("0") : NULL;
  lcd.print(minutea);
  lcd.print(":");
  (seconda < 10) ? lcd.print("0") : NULL;
  lcd.print(seconda);
  lcd.display();
  stepDowna();
  delay(1000);
  digitalWrite(led,LOW);
  }
  }
}

Either I mis-typed or misunderstood you. Still a reply back would be helpful!

First get rid of the recursion; your code will crash. You should basically never call loop() from inside your own code !!

loop() calls stepDown() or stepDowna(); both will call trigger() which in turn will call loop() again. This might only happen under certain circumstances but it is an accident waiting to happen.

Next please give an explanation of what you want your code to do.

I would like the code so when either button 10 mins or 30 mins can set off the countdown, and after the countdown to turn off and wait for another signal from a button.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using?

How have you got your buttons wired?

In the IDE, select TOOLs then Auto Format, it will indent your code and make it easier to read and track your { } pairs.

Tom... :slight_smile: