Go Down

Topic: my if statements keep repeating themselves (Read 1 time) previous topic - next topic

hyugow

Hi all, I have a question regarding code. whenever i run this bit of code, the 2nd if statement runs without me pushing the button. somebody please help!

Code: [Select]
if (buttonStateOne == LOW){
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Hyugo!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
   
    digitalWrite(buttonPinOne, HIGH);
   
    if(buttonStateOne == LOW){
     lcd.clear();
    }
   
 
   
   
  }

Grumpy_Mike

How are the buttons wired.
You need to post all your code not just a bit.

hyugow

here is the whole code:

Code: [Select]
#include <LiquidCrystal.h>

const int buttonPinOne = 7;
const int buttonPinTwo = 6;
const int buttonPinThree = 8;
const int buttonPinFour = 9;
const int buttonPinFive = 10;
const int buttonPinSix = 13;


int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateThree = 0;
int buttonStateFour = 0;
int buttonStateFive = 0;
int buttonStateSix = 0;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
  // set up the LCD's number of columns and rows:
  Serial.begin(9600);
 
  pinMode(buttonPinOne, INPUT);
  pinMode(buttonPinTwo, INPUT);
  pinMode(buttonPinThree, INPUT);
  pinMode(buttonPinFour, INPUT);
  pinMode(buttonPinFive, INPUT);
  pinMode(buttonPinSix, INPUT);
 
  digitalWrite(buttonPinOne, HIGH);
  digitalWrite(buttonPinTwo, HIGH);
  digitalWrite(buttonPinThree, HIGH);
  digitalWrite(buttonPinFour, HIGH);
  digitalWrite(buttonPinFive, HIGH);
  digitalWrite(buttonPinSix, HIGH);
 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("loading...");
   delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Welcome to snack");
  lcd.setCursor(0, 1);
  lcd.print("OS V 1.1");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Please enter your");
  lcd.setCursor(0, 1);
  lcd.print("secure number");

 
 
  //START OF BUTTON SEQUENCE
 

}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
buttonStateOne = digitalRead(buttonPinOne);
buttonStateTwo = digitalRead(buttonPinTwo);
buttonStateThree = digitalRead(buttonPinThree);
buttonStateFour = digitalRead(buttonPinFour);
buttonStateFive =  digitalRead(buttonPinFive);
buttonStateSix = digitalRead(buttonPinSix);

 
  if (buttonStateOne == LOW){
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Hyugo!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
   
    digitalWrite(buttonPinOne, HIGH);
   
    if(buttonStateOne == LOW){
     lcd.clear();
    }
   
 
   
   
  }

    if (buttonStateTwo == LOW){
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Xander!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
   
  }
 
    if (buttonStateThree == LOW){
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Sarah!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
   
   
   
   
   
  }
 
    if (buttonStateFour == LOW){
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Franklin!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
   
  }
 
    if (buttonStateFive == LOW){
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Hannah!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
   
  }
 
 
 
 
}

Arrch

Code: [Select]
  if (buttonStateOne == LOW){
    ...
    if(buttonStateOne == LOW){
     lcd.clear();
    }


Why bother checking again? You never update buttonStateOne between the two if statements, so the second one will ALWAYS run.

James C4S

Code runs fast, humans are slow.

You enter the first if-statement, execute a few statements, then check to see if the button is (still) low.  You're going to need a lot of luck for that sequence to finish before you lift your finger off the button.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

hyugow

so then what code would i use to stop the sequence from running on its own

1ChicagoDave

You're assuming
Code: [Select]
    digitalWrite(buttonPinOne, HIGH);
changes the value of buttonStateOne.

Does it?

captainhooyah

I assume you want to press the button once, which set the buttonState to LOW and runs the first if statement. THEN press the same button again and execute the second if statement that clears the LCD?

If so I think this will help you... Probably not the best solution but it should work.  (I didn't test it on an arduino).

I just added a flag (x) and a timer.


Code: [Select]


#include <LiquidCrystal.h>

const int buttonPinOne = 7;
const int buttonPinTwo = 6;
const int buttonPinThree = 8;
const int buttonPinFour = 9;
const int buttonPinFive = 10;
const int buttonPinSix = 13;

//timer+flag
int x = 0;
unsigned long buttonPressMillis;

int buttonStateOne = 0;
int buttonStateTwo = 0;
int buttonStateThree = 0;
int buttonStateFour = 0;
int buttonStateFive = 0;
int buttonStateSix = 0;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
  // set up the LCD's number of columns and rows:
  Serial.begin(9600);

  pinMode(buttonPinOne, INPUT);
  pinMode(buttonPinTwo, INPUT);
  pinMode(buttonPinThree, INPUT);
  pinMode(buttonPinFour, INPUT);
  pinMode(buttonPinFive, INPUT);
  pinMode(buttonPinSix, INPUT);

  digitalWrite(buttonPinOne, HIGH);
  digitalWrite(buttonPinTwo, HIGH);
  digitalWrite(buttonPinThree, HIGH);
  digitalWrite(buttonPinFour, HIGH);
  digitalWrite(buttonPinFive, HIGH);
  digitalWrite(buttonPinSix, HIGH);

  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("loading...");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Welcome to snack");
  lcd.setCursor(0, 1);
  lcd.print("OS V 1.1");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Please enter your");
  lcd.setCursor(0, 1);
  lcd.print("secure number");



  //START OF BUTTON SEQUENCE


}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  buttonStateOne = digitalRead(buttonPinOne);
  buttonStateTwo = digitalRead(buttonPinTwo);
  buttonStateThree = digitalRead(buttonPinThree);
  buttonStateFour = digitalRead(buttonPinFour);
  buttonStateFive =  digitalRead(buttonPinFive);
  buttonStateSix = digitalRead(buttonPinSix);


  if (buttonStateOne == LOW && x == 0){
    buttonPressMillis = millis();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Hyugo!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");
    x=1;

    //digitalWrite(buttonPinOne, HIGH);
   
    //This adds a little timer so you don't satisfy both if statements
    //With one press of the button.
    if((millis()-buttonPressMillis)>=2000);
    {
    if(buttonStateOne == LOW && x == 1){
      lcd.clear();
      x=0;
      buttonPressMillis=0;
    }
    }




  }

  if (buttonStateTwo == LOW){

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Xander!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");

  }

  if (buttonStateThree == LOW){

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Sarah!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");





  }

  if (buttonStateFour == LOW){

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Franklin!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");

  }

  if (buttonStateFive == LOW){

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome Hannah!");
    delay(1000);
    lcd.clear();
    lcd.print("select snacks");
    lcd.setCursor(0, 1);
    lcd.print("for checkout");

  }

}



hyugow

the code does not work. there has to be a simpler way. maybe if i reset the button state, but how would i do that?

PeterH


the code does not work


Perhaps you ought to start by explaining what you want to happen in terms of inputs (buttons being pushed down and released) and outputs (things being displayed on the LCD). Then we can suggest  the best way to modify your code to achieve that.
I only provide help via the forum - please do not contact me for private consultancy.

1ChicagoDave


the code does not work. there has to be a simpler way. maybe if i reset the button state, but how would i do that?


How did you set the button state the first time?

Grumpy_Mike


the code does not work. there has to be a simpler way.

I get the feeling you are struggling to express yourself in English.
That is why you are struggling to understand all the replies you are getting.

You still have not answered my question as to how you have connected up your buttons.

hyugow

first, i am american and i was born in the us. i speak english very well. second, i have school, and i had homework. the way that the button is connected is one lead goes into a digital pin, then another goes into the ground pin.

hyugow

what i mean by simpler way is that there has to be some sort of code that resets the button state to high, so when the button is pressed again, it reads it independently from the previous if statement

joshuabardwell


what i mean by simpler way is that there has to be some sort of code that resets the button state to high, so when the button is pressed again, it reads it independently from the previous if statement


You've misunderstood how the button state works. Assuming you are using a momentary button, the button state is entirely dependent on whether the button is pressed or not. If the button is pressed, the input goes LOW. If the button is released, it goes HIGH. If the button is latching, then one press makes the button LOW and the next press makes it HIGH.

Are you using a latching button or a momentary one? I assume momentary, but best to be sure.

What you should do is not rely on a timer. Rather, track the state of the button and do not register the second button press until you detect that the first button press has been released.

Code: [Select]
IF buttonPressed THEN
  LOOP UNTIL buttonReleased

Now do first button press work

IF buttonPressed THEN
  LOOP UNTIL buttonReleased

Now do second button press work.


That code snipped leaves many details for you to work out for yourself, which is why I have written it in pseudocode instead of C++.

Go Up