(Solved) Run Loop on Command and Multiple If Statements

Good morning all,

I am hoping for a bit of help. I am completely new to coding so please go easy on me. I have spent some time going through the simple projects such as turning an LED on and basic button functions as well as following tutorials for creating "crystal balls" and the likes, which all went well. I have now started on my own project and have come across a couple of problems.

The platform is an Arduino Uno, with a 16 x 2 LCD screen and 2 push buttons.

What I am trying to achieve: -

When the Arduino is powered up a message should be displayed, after a set period of time this message changes to another. To proceed from the second message a button must be pressed before the loop begins.

The loop then consists of some text and a counter. When one button is pressed the counter increases and when another button is pressed the counter goes back to 0.

The two items which aren't working are the proceed to loop on button command and the counter returning to zero. I have spent a lot of time trying to fix the problem and researching but am not getting anywhere so this post is a last resort - I don't usually like bothering others for what is a simple problem for most!

Hopefully someone will be able to help!

Thank you.

#include <LiquidCrystal.h>

LiquidCrystal lcd(2,3,4,5,6,7);

const int buttonDownAdd = 8;           //  button position on Arduino
int countDownAdd = 10;                  // button counter
int buttonStateDownAdd = 0;            // current state of button
int lastButtonStateDownAdd = 0;        // previous state of button

const int buttonEnterClear = 9;        // set up clear button
int buttonStateEnterClear = 0;         // current state of button
int lastButtonStateEnterClear = 0;     // previous state of button

void setup() 
{
  pinMode (buttonDownAdd, INPUT);                    // set up button as input
  pinMode (buttonEnterClear, INPUT);                // set up button as input

  lcd.begin(16, 2);                                  // set up LCD
  lcd.setCursor(3,0);                                // print message to LCD
  lcd.print("Welcome to");
  lcd.setCursor(4,1);
  lcd.print("MiScore!");
  
  delay(3000);                                       // delay 3 seconds before changing the message
  
  lcd.clear();                                      // print new message to screen
  lcd.setCursor(1,0);
  lcd.print("Press Green to");
  lcd.setCursor(5,1);
  lcd.print("Begin!");
}

void loop() 
{
buttonStateDownAdd = digitalRead(buttonDownAdd);      // read buttonDownAdd to see it's current position  
  
  if (buttonStateDownAdd != lastButtonStateDownAdd) { // check to see if the button has changed from its previous state
    
      if (buttonStateDownAdd == HIGH){           // if the button has changed then move to the query
        
      {buttonStateDownAdd = digitalRead(buttonDownAdd);      // read buttonDownAdd again to see it's current position
      
          if (buttonStateDownAdd != lastButtonStateDownAdd) {  // check to see if the button has changed from its previous state
      
              if (buttonStateDownAdd == HIGH){
              
              countDownAdd++;                                    // if the button has been pressed add 1 to countDownAdd

              lcd.clear();                                          // write the current countDownAdd to the LCD
              lcd.setCursor(2,0);
              lcd.print("You have had");
              lcd.setCursor(4,1);
              lcd.print(countDownAdd);
              lcd.setCursor(8,1);
              lcd.print("dabs");  
                                
              }
      
              else {}
              
              delay (25);
      
          }
      
          else {}
          
          delay (25);
          
          lastButtonStateDownAdd = buttonStateDownAdd;}          // save the current state of the button for the next loop   
          
     {buttonStateEnterClear = digitalRead(buttonEnterClear);    // read buttonEnterClear to see it's current position
          
              if (buttonStateEnterClear != lastButtonStateEnterClear) {  // check to see if the button has changed from its previous state
              
                  if (buttonStateEnterClear == HIGH){                      // if the button has been pressed reset CountDownAdd and update the LCD screen
                  
                  countDownAdd=0;

                  lcd.clear();
                  lcd.setCursor(2,0);
                  lcd.print("You have had");
                  lcd.setCursor(4,1);
                  lcd.print(countDownAdd);
                  lcd.setCursor(8,1);
                  lcd.print("dabs");
                  
                  }
                  
                  else {}
                  
                  delay (25);
              
              }
              
              else {}
              
              delay (25);
              
              lastButtonStateEnterClear = buttonStateEnterClear;}          // save the current state of the button for the next loop    
          
      }    
              
      else {}
    
      delay (25);
    
      lastButtonStateDownAdd = buttonStateDownAdd;          // save the current state of the button for the next loop    
    
  }
  
  else {}
  
  delay (25);
  
    lastButtonStateDownAdd = buttonStateDownAdd;          // save the current state of the button for the next loop
}

Where do you read the state of a switch in setup()? How are your switches wired?

Waiting, in setup(), while the state of the appropriate pin is "unpressed" (whether that means HIGH or LOW depends on how your switch is wired), is trivial.

There is no recognized coding convention where ANYTHING follows the {. Using a recognized convention, and properly indenting your code makes it far easier to help you.

"The two items which aren't working are the proceed to loop on button command and the counter returning to zero. "

first problem can be fixed with something like this. (as you haven't got a green button I changed that to enter for this example)

void setup()
{
  pinMode (buttonDownAdd, INPUT);                    // set up button as input
  pinMode (buttonEnterClear, INPUT);                // set up button as input

  lcd.begin(16, 2);                                  // set up LCD
  lcd.setCursor(3, 0);                               // print message to LCD
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");

  delay(3000);// delay 3 seconds before changing the message
  lcd.clear();//moved or screen will flash

  while (digitalRead(buttonEnterClear) == LOW) {
    lcd.setCursor(1, 0);
    lcd.print("Press enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
}

problem 2 is way to many {} all over the place and code that makes no sense
I used the sledge hammer approach as listing all the problems takes to much typing

#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int buttonDownAdd = 8;           //  button position on Arduino
int countDownAdd = 10;                  // button counter
int buttonStateDownAdd = 0;            // current state of button
int lastButtonStateDownAdd = 0;        // previous state of button

const int buttonEnterClear = 9;        // set up clear button
int buttonStateEnterClear = 0;         // current state of button
int lastButtonStateEnterClear = 0;     // previous state of button

void setup()
{
  pinMode (buttonDownAdd, INPUT);                    // set up button as input
  pinMode (buttonEnterClear, INPUT);                // set up button as input

  lcd.begin(16, 2);                                  // set up LCD
  lcd.setCursor(3, 0);                               // print message to LCD
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");

  delay(3000);// delay 3 seconds before changing the message
  lcd.clear();//moved or screen will flash

  while (digitalRead(buttonEnterClear) == LOW) {
    lcd.setCursor(1, 0);
    lcd.print("Press enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
  lcd.clear();
}

void loop()
{
  buttonStateDownAdd = digitalRead(buttonDownAdd);      // read buttonDownAdd to see it's current position
  buttonStateEnterClear = digitalRead(buttonEnterClear);    // read buttonEnterClear to see it's current position

  if (buttonStateDownAdd != lastButtonStateDownAdd) { // check to see if the button has changed from its previous state
    delay(40);//crude debounce should be millis based timer
    if (buttonStateDownAdd == HIGH) {          // if the button has changed then move to the query
      countDownAdd++;  // isnt down normally - not add?
    }
  }
  lastButtonStateDownAdd = buttonStateDownAdd;

 if (buttonStateEnterClear == HIGH) {  // if the button has been pressed reset CountDownAdd and update the LCD screen
    countDownAdd = 0;
  }

  lcd.setCursor(2, 0);
  lcd.print("You have had");
  lcd.setCursor(4, 1);
  lcd.print(countDownAdd);
  lcd.setCursor(8, 1);
  lcd.print("dabs");
}

Hi Paul & Gpop,

Thanks for your quick responses.

PaulS:
Where do you read the state of a switch in setup()? How are your switches wired?

At the moment I wasn't reading the state of the switch in setup, I was reading it at the start of the loop via an If statement on the swtichstate, if selected the rest of the code was in the if true brackets.

PaulS:
Waiting, in setup(), while the state of the appropriate pin is "unpressed" (whether that means HIGH or LOW depends on how your switch is wired), is trivial.

I have attached a photo of my wiring if this is helpful?

PaulS:
There is no recognized coding convention where ANYTHING follows the {. Using a recognized convention, and properly indenting your code makes it far easier to help you.

Sorry but you have lost me on this bit? Please could you elaborate?

gpop1:
"The two items which aren't working are the proceed to loop on button command and the counter returning to zero. "

first problem can be fixed with something like this. (as you haven't got a green button I changed that to enter for this example)

void setup()

{
  pinMode (buttonDownAdd, INPUT);                    // set up button as input
  pinMode (buttonEnterClear, INPUT);                // set up button as input

lcd.begin(16, 2);                                  // set up LCD
  lcd.setCursor(3, 0);                              // print message to LCD
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");

delay(3000);// delay 3 seconds before changing the message
  lcd.clear();//moved or screen will flash

while (digitalRead(buttonEnterClear) == LOW) {
    lcd.setCursor(1, 0);
    lcd.print("Press enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
}





problem 2 is way to many {} all over the place and code that makes no sense 
I used the sledge hammer approach as listing all the problems takes to much typing 



#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int buttonDownAdd = 8;          //  button position on Arduino
int countDownAdd = 10;                  // button counter
int buttonStateDownAdd = 0;            // current state of button
int lastButtonStateDownAdd = 0;        // previous state of button

const int buttonEnterClear = 9;        // set up clear button
int buttonStateEnterClear = 0;        // current state of button
int lastButtonStateEnterClear = 0;    // previous state of button

void setup()
{
  pinMode (buttonDownAdd, INPUT);                    // set up button as input
  pinMode (buttonEnterClear, INPUT);                // set up button as input

lcd.begin(16, 2);                                  // set up LCD
  lcd.setCursor(3, 0);                              // print message to LCD
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");

delay(3000);// delay 3 seconds before changing the message
  lcd.clear();//moved or screen will flash

while (digitalRead(buttonEnterClear) == LOW) {
    lcd.setCursor(1, 0);
    lcd.print("Press enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
  lcd.clear();
}

void loop()
{
  buttonStateDownAdd = digitalRead(buttonDownAdd);      // read buttonDownAdd to see it's current position
  buttonStateEnterClear = digitalRead(buttonEnterClear);    // read buttonEnterClear to see it's current position

if (buttonStateDownAdd != lastButtonStateDownAdd) { // check to see if the button has changed from its previous state
    delay(40);//crude debounce should be millis based timer
    if (buttonStateDownAdd == HIGH) {          // if the button has changed then move to the query
      countDownAdd++;  // isnt down normally - not add?
    }
  }
  lastButtonStateDownAdd = buttonStateDownAdd;

if (buttonStateEnterClear == HIGH) {  // if the button has been pressed reset CountDownAdd and update the LCD screen
    countDownAdd = 0;
  }

lcd.setCursor(2, 0);
  lcd.print("You have had");
  lcd.setCursor(4, 1);
  lcd.print(countDownAdd);
  lcd.setCursor(8, 1);
  lcd.print("dabs");
}

Thank you for taking the time to look at the code in detail gpop. I have ran this on the Arduino this morning but it is still skipping the "press Green to Continue" page. Also, the increase count and return to zero buttons no longer work?

I am still a little lost if you have any ideas?

Many thanks for your help so far.

Sorry but you have lost me on this bit? Please could you elaborate?

Sure. This:

      if (buttonStateDownAdd == HIGH){           // if the button has changed then move to the query
        
      {buttonStateDownAdd = digitalRead(buttonDownAdd);      // read buttonDownAdd again to see it's current position

has code following the {, on the last line. If you put every { on a new line, which is one of the recognized conventions, and used Tools + Auto Format to properly indent the code, you'd see that the { on the last line is not needed.

The other recognized convention has the { on the line with the function or statement that it goes with, as in the first line, BUT separated by a space, with the next line of code on the next line (not separated by a blank line). Personally, I do not like that convention.

Not to be too picky, but most of your comments are useless. We know what digitalRead does.

Comments on the same line as the code need to be very short.

Comments (when needed) that are long should precede the code that they are describing.

      // if the button has changed then move to the query
      if (buttonStateDownAdd == HIGH)
      {
        {
           buttonStateDownAdd = digitalRead(buttonDownAdd);

would be how I would reorganize that snippet, making it very clear that there are more { than are needed.

A simple rule for comments is that they should describe the why, not the what (the code is the what).

please look in the learning tab top of the page, references then INPUT_PULLUP. Read how and what it does. Then decide if PULLUP can be used or if you need to add a resistor between the input pin and ground to act as a pull down.

PaulS:
Sure. This:

      if (buttonStateDownAdd == HIGH){           // if the button has changed then move to the query

{buttonStateDownAdd = digitalRead(buttonDownAdd);      // read buttonDownAdd again to see it's current position



has code following the {, on the last line. If you put every { on a new line, which is one of the recognized conventions, and used Tools + Auto Format to properly indent the code, you'd see that the { on the last line is not needed.

The other recognized convention has the { on the line with the function or statement that it goes with, as in the first line, BUT separated by a space, with the next line of code on the next line (not separated by a blank line). Personally, I do not like that convention.

Not to be too picky, but most of your comments are useless. We know what digitalRead does.

Comments on the same line as the code need to be very short.

Comments (when needed) that are long should precede the code that they are describing.



// if the button has changed then move to the query
      if (buttonStateDownAdd == HIGH)
      {
        {
          buttonStateDownAdd = digitalRead(buttonDownAdd);



would be how I would reorganize that snippet, making it very clear that there are more { than are needed.

Hi Paul,

I understand your comments now, thank you for the further explanation. I will take this on board with future coding. I wasn't aware of the Tools + Auto Format but that will also be very helpful.

Thanks.

gpop1:
please look in the learning tab top of the page, references then INPUT_PULLUP. Read how and what it does. Then decide if PULLUP can be used or if you need to add a resistor between the input pin and ground to act as a pull down.

Thank you for pointing me in the right direction, it works.....it was my wiring causing the problem then! There is one problem left...

When I press the green button ("buttonDownAdd") which is the button name I slightly adjusted your code to, it goes to the correct screen but it also increases the "countDownAdd" figure to a number, where as this needs to start from 0. I tried adding a delay in but this didn't work.

I then press the "buttonEnterClear" but it doesn't clear to zero, it is taking it back to 05 on this occasion, then when the "buttonDownAdd" is pressed it then goes up in 10's untill it reaches 100 and goes back to 10 when it goes back up in 1's, the clear button then takes it back to 08 on this occasion and it starts again.

Do you have any ideas on where I am going wrong?

Many thanks.

Once you make changes in a sketch you are asking for assistance, repost the new version so we don't have to guess its current state of affairs.

need to add a few if's to write blanks to where the numbers use to be or use lcd.clear()

probably the easiest way is to take a prevCount then compare to countDownAdd

something like

if (prevCount != countDownAdd){
lcd.clear();
}
prevCount= countDownAdd;

Thank you for your help.

I have now got it running with the following code. I appreciate there may be better ways of writing it in a more condensed format but at least it is doing what I want! Now to add to it and learn to condense it.

#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int buttonDownAdd = 8;           //  button position on Arduino
int countDownAdd = 10;                  // button counter
int buttonStateDownAdd = 0;            // current state of button
int lastButtonStateDownAdd = 0;        // previous state of button

const int buttonEnterClear = 9;        // set up clear button
int buttonStateEnterClear = 0;         // current state of button
int lastButtonStateEnterClear = 0;     // previous state of button

void setup()
{
  pinMode (buttonDownAdd, INPUT);                    // set up button as input
  pinMode (buttonEnterClear, INPUT);                // set up button as input

  lcd.begin(16, 2);                                  // set up LCD
  lcd.setCursor(3, 0);                               // print message to LCD
  lcd.print("Welcome to");
  lcd.setCursor(4, 1);
  lcd.print("MiScore!");

  delay(3000);
  lcd.clear();

  while (digitalRead(buttonDownAdd) == LOW) {
    lcd.setCursor(1, 0);
    lcd.print("Press enter to");
    lcd.setCursor(5, 1);
    lcd.print("Begin!");
  }
  lcd.clear();
  countDownAdd = -1;
}

void loop()
{
  buttonStateDownAdd = digitalRead(buttonDownAdd);
  if (buttonStateDownAdd != lastButtonStateDownAdd)
  {
    if (buttonStateDownAdd == HIGH)
    {
      countDownAdd++;
      lcd.clear();
      lcd.setCursor(2, 0);
      lcd.print("You have had");
      lcd.setCursor(4, 1);
      lcd.print(countDownAdd);
      lcd.setCursor(8, 1);
      lcd.print("dabs");
    }
    delay (20);
  }
  delay (20);
  lastButtonStateDownAdd = buttonStateDownAdd;

  buttonStateEnterClear = digitalRead(buttonEnterClear);
  if (buttonStateEnterClear != lastButtonStateEnterClear)
  {
    if (buttonStateEnterClear == HIGH)
    {
      countDownAdd = 0;
      lcd.clear();
      lcd.setCursor(2, 0);
      lcd.print("You have had");
      lcd.setCursor(4, 1);
      lcd.print(countDownAdd);
      lcd.setCursor(8, 1);
      lcd.print("dabs");
    }
    delay (20);
  }
  delay (20);
  lastButtonStateEnterClear = buttonStateEnterClear;
}