I can't switch because Im not able to modify the variable

hi, im trying to make a relay switcher with 2 buttons, 1 for UP and DOWN, and then display the selected numbers from 0 to 6.

but when i try to change or increment my variable it can’t.

here is the code

HELP :frowning:

#include <LiquidCrystal.h>

const int rs = A0, en = A1, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int Pin1 = 2;
const int Pin2 = 3;
const int Pin3 = 4;
const int Pin4 = 5;
const int Pin5 = 6;
const int Pin6 = 7;
const int Pin7 = 8;
int Dir = 0;
int buttonState = 0;
int buttonState0 = 0;

void setup() {

lcd.begin(8, 2);
Serial.begin(9600);
pinMode(13, INPUT);
pinMode(11, INPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
pinMode(Pin4, OUTPUT);
pinMode(Pin5, OUTPUT);
pinMode(Pin6, OUTPUT);
pinMode(Pin7, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
}

void loop()
{
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, HIGH);
digitalWrite(Pin5, HIGH);
digitalWrite(Pin6, HIGH);
digitalWrite(Pin7, HIGH);

if (digitalRead(13) == HIGH)
{
if(digitalRead(13) == LOW)
{
Dir++;
Serial.println(Dir);

delay(10);
}
}

if (Dir == 0)
{
digitalWrite(Pin1, LOW);
lcd.print(“Silo1”);
Serial.print(“S1”);

}else {
delay(10);
digitalWrite(Pin1, HIGH);
}

if (Dir == 1)
{
digitalWrite(Pin2, LOW);
lcd.print(“Silo2”);
Serial.print(“S2”);

}else {
delay(10);
digitalWrite(Pin2, HIGH);
}

if (Dir == 2)
{
digitalWrite(Pin3, LOW);
lcd.print(“Silo3”);
Serial.print(“S3”);
}else {
delay(100);
digitalWrite(Pin3, HIGH);
}

if (Dir == 3)
{
digitalWrite(Pin4, LOW);
lcd.print(“Silo4”);
Serial.print(“S4”);
}else {
delay(10);
digitalWrite(Pin4, HIGH);
}

if (Dir == 4)
{
digitalWrite(Pin5, LOW);
lcd.print(“Silo5”);
Serial.print(“S5”);
}else {
delay(10);
digitalWrite(Pin5, HIGH);
}

if (Dir == 5)
{
digitalWrite(Pin6, LOW);
lcd.print(“Silo6”);
Serial.print(“S6”);
}else {
delay(10);
digitalWrite(Pin6, HIGH);
}

if (Dir == 6)
{
digitalWrite(Pin7, LOW);
lcd.print(“Silo7”);
Serial.print(“S7”);
}else {
delay(10);
digitalWrite(Pin7, HIGH);
}

}

Please think about this

 if (digitalRead(13) == HIGH) 
 {
  if(digitalRead(13) == LOW)
  {
    Dir++;

if the digital is low it will never pass the first if. If the digital is high, it will never pass the second if. The only time it will work is if the digital switches from high to low in the few microseconds between the two if statements, which is a highly unlikely event.

if (digitalRead(13) == HIGH)
{
if(digitalRead(13) == LOW)

Not going to work :wink:

You are allowing yourself a very short window (6 microseconds?) each time through loop() in which to hit the button. Too early and both reads return LOW. Too late and both reads return HIGH. Look at the StateChangeDetect example for how to detect a button press. Depending on how often loop() runs you may also need to add button debounce.

Oh thanks, I solved it :wink: