Need code help :(

So I am trying to set up two buttons to increase and decrease the count, however when i watch the serial monitor the numbers are all over the place, can anyone tell me what is wrong with my code?

#include <LiquidCrystal.h>

const int  buttonPin2 = 7;
const int  buttonPin = 6;
const int ledPin = 13;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int buttonPushCounter = 0;   
int buttonState = 0;
int buttonState2 = 0; 
int lastButtonState = 0; 


void setup() {
  pinMode(buttonPin, INPUT);
  
  pinMode(buttonPin2, INPUT);
    
    Serial.begin(9600);

  lcd.begin(16, 2);
  
  pinMode(ledPin, OUTPUT);
 
  lcd.print("LED Menu");

}

void loop() {
 
  buttonState = digitalRead(buttonPin);

 
 if (buttonState != lastButtonState) {
   
 if (buttonState == HIGH) {buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {

      Serial.println("off"); 
    }
  }







  lastButtonState = buttonState;
  
  
  buttonState2 = digitalRead(buttonPin2);

 
   if (buttonState2 != lastButtonState) {
     

     
     if (buttonState2 == HIGH)
     
     {
       
      buttonPushCounter--;

     }
     
             
   }
   lastButtonState = buttonState2; 
   
   
  
  
  
  
  
  
  
  
 if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
   
}

Where is lastButtonState2?

did not know you had to run that! I will try putting it in :)

I did the change but the second button is still putting off the count

#include <LiquidCrystal.h>

const int  buttonPin2 = 7;
const int  buttonPin = 6;
const int ledPin = 13;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int buttonPushCounter = 0;   
int buttonState = 0;
int buttonState2 = 0; 
int lastButtonState = 0;
int lastButtonState2 = 0; 


void setup() {
  pinMode(buttonPin, INPUT);
  
  pinMode(buttonPin2, INPUT);
    
    Serial.begin(9600);

  lcd.begin(16, 2);
  
  pinMode(ledPin, OUTPUT);
 
  lcd.print("LED Menu");

}

void loop() {
 
  buttonState = digitalRead(buttonPin);

 
 if (buttonState != lastButtonState) {
   
 if (buttonState == HIGH) {buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {

      Serial.println("off"); 
    }
  }







  lastButtonState = buttonState;
  
  
  buttonState2 = digitalRead(buttonPin2);


 
   if (buttonState2 != lastButtonState) {
     

     
     if (buttonState2 == HIGH)
     
     {
       
      buttonPushCounter--;

     }
     
             
   }
   lastButtonState2 = buttonState2; 
   
   
  
  
  
  
  
  
  
  
 if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
   
}

I am trying to set it up so when the count equals 1 and the button connected to pin 8 is pushed the LED lights up how ever it is not working :frowning: please help

#include <LiquidCrystal.h>

const int  buttonPin3 = 8;
const int  buttonPin2 = 7;
const int  buttonPin1 = 6;
const int ledPin = 13;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int buttonPushCounter = 0;   
int buttonState1 = 0;
int buttonState2 = 0; 
int lastButtonState1 = 0;
int lastButtonState2 = 0; 


void setup() {
  pinMode(buttonPin1, INPUT);
  
  pinMode(buttonPin2, INPUT);
  
  pinMode(buttonPin3, INPUT);
    
    Serial.begin(9600);

  lcd.begin(16, 2);
  
  pinMode(ledPin, OUTPUT);
 
  lcd.print("LED Menu");
  
  lcd.setCursor(0,1);

}

void loop() {
 
  buttonState1 = digitalRead(buttonPin1);

 
 if (buttonState1 != lastButtonState1) {
   
 if (buttonState1 == HIGH) {buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
  lcd.setCursor(7,1);
           
      lcd.print(buttonPushCounter);  
  } 
    else {

      Serial.println("off"); 
    }
  }







  lastButtonState1 = buttonState1;
  
  
  buttonState2 = digitalRead(buttonPin2);


 
   if (buttonState2 != lastButtonState2) {
     

     
     if (buttonState2 == HIGH)
     
     {
       
      buttonPushCounter--;
lcd.setCursor(7,1);
          
      lcd.print(buttonPushCounter);
     }
       
             
   }
   lastButtonState2 = buttonState2; 
   
   
  
  
  
  
  
  
  
  
 if (buttonPushCounter % 1 == 0 && buttonPin3 == HIGH)

 {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
   
}

In the words you say you want when the buttonPushCounter equals one. But that's not what you wrote.

if (buttonPushCounter % 1 == 0 && buttonPin3 == HIGH)

That first condition is always true. Can you give any example where you divide an integer by one and get any remainder? Maybe instead you meant to check if buttonPushCounter is equal to 1.

I ended up fixing this problem :P was trying to make it so both the count and the push button had to be in the right state for the LED to work, I'm now trying to make it so for this to happen the LED must be in the right state! do you know how i do this?

 buttonState3 = digitalRead(buttonPin3);



 if (buttonState3 == HIGH && buttonPushCounter == 2 && ledPin == LOW)

 {
    digitalWrite(ledPin, HIGH);
  }
  else {
   if (buttonState3 == HIGH && buttonPushCounter == 2 && ledPin == HIGH)
   {digitalWrite(ledPin, LOW);}
  }
   
}
ledPin == LOW)

Should there be a digitalRead there?

I have never done a Digital read before could you please write an example :) for a LED light that is

Across the top of the screen, just above where the white section ends, is a set of tabs. One reads 'Reference'. Third item down on the right hand side.

I tried to do a digital read the same way I would do it for a switch but it did not work out :frowning:

#include <LiquidCrystal.h>

const int  buttonPin3 = 10;
const int  buttonPin2 = 7;
const int  buttonPin1 = 6;
const int ledPin = 13;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int buttonPushCounter = 0;   
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int Ledstate = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0; 


void setup() {
  pinMode(buttonPin1, INPUT);
  
  pinMode(buttonPin2, INPUT);
  
  pinMode(buttonPin3, INPUT);
    
    Serial.begin(9600);

  lcd.begin(16, 2);
  
  pinMode(ledPin, OUTPUT);
 
  lcd.print("LED Menu");
  
  lcd.setCursor(0,1);

}

void loop() {
  
 
  buttonState1 = digitalRead(buttonPin1);

 
 if (buttonState1 != lastButtonState1) {
   
 if (buttonState1 == HIGH) {buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
  lcd.setCursor(7,1);
           
      lcd.print(buttonPushCounter);  
  } 
    else {

      Serial.println("off"); 
    }
  }







  lastButtonState1 = buttonState1;
  
  
  buttonState2 = digitalRead(buttonPin2);


 
   if (buttonState2 != lastButtonState2) {
     

     
     if (buttonState2 == HIGH)
     
     {
       
      buttonPushCounter--;
lcd.setCursor(7,1);
          
      lcd.print(buttonPushCounter);
     }
       
             
   }
   lastButtonState2 = buttonState2; 
   
   
  
  
  
  
  
  
 buttonState3 = digitalRead(buttonPin3);
 
 Ledstate = digitalRead(ledPin);
 
 if (buttonState3 == HIGH && buttonPushCounter == 2 && Ledstate == LOW)

 {
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(ledPin, LOW);
  }


 
   if (buttonState3 == HIGH && buttonPushCounter == 2 && Ledstate == HIGH)
   {digitalWrite(ledPin, LOW);}
 
   else{
    digitalWrite(ledPin, HIGH);
  }
}

Do I have to make the Pin mode both input and out put?

peter86: Do I have to make the Pin mode both input and out put?

You can't , so, no.

So there is no way for me to use the LED state as a parameter?

peter86: So there is no way for me to use the LED state as a parameter?

You were the last to write to the LED, so you should know what state it is in.

Mmmmmmmm thanks.. I need to processes to know what state it is in to see if it is okay to change it.