Pages: [1] 2   Go Down
Author Topic: my if statements keep repeating themselves  (Read 1429 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
    }
   
 
   
   
  }
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the whole code:

Code:
#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");
   
  }
 
 
 
 
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Does it?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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");

  }

}


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Chicago
Offline Offline
Sr. Member
****
Karma: 5
Posts: 456
With every answer comes more questions.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 522
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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++.
Logged

Pages: [1] 2   Go Up
Jump to: