small problem with a switch pushbutton and coding [SOLVED]

hello guys,

here is my problem :

i have a pushbutton, a pot and 8 LEDs, the pushbutton i am using is actually an On Off pushbutton.
So my final target is this :

i have 8 LEDs connected to a shift register, when i turn the pot that has a value starting from around 500 till 1023, the leds should start to get on one by one starting from Led 5 till 8 (that is the forward state)

when i click the pushbutton, i should have a state of reverse, so the Leds should start from led 4 and lit down till led 0

what i am getting is this result but i must click the pushbutton 2 times to go from forward to reverse state or from reverse to forward,
when i don’t click the button but simply put little force on it without clicking it, this mechanism works fine and the pushbutton functions like a tactile

so any solution in the codes please ?

int throttlePotPin = A0;
int latchPin = 9;
int clockPin = 10;
int dataPin = 8;
int leds = 0;

int reverseButton = 3;
boolean lastReverseButton = LOW;
boolean currentReverseButton = LOW;
boolean reverseButtonState = false;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  
  pinMode(reverseButton, INPUT_PULLUP);
  Serial.begin(9600);
}

//Debounce function, it takes 2 parameters, the boolean state and the pin number of the input button
boolean debounce(boolean last, int switchPin)
{
  boolean current = digitalRead(switchPin);
  if(last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
   currentReverseButton = debounce(lastReverseButton, reverseButton);
   if(lastReverseButton == LOW && currentReverseButton == HIGH)
   {
     reverseButtonState = !reverseButtonState;
    
   }
   lastReverseButton = currentReverseButton;
   
   if(reverseButtonState == LOW){
     int reading = analogRead(throttlePotPin);
     reading = map(reading, 0, 1023, 1023, 0); 
     Serial.print(reading);
     Serial.print('\n');
     
     if(reading >= 0 && reading <= 650){
       leds = B00000000;
     }else if(reading > 650 && reading <= 700){
       leds = B00001000;
     }else if(reading > 700 && reading <= 800){
       leds = B00001100;
     }else if(reading > 800 && reading <= 900){
       leds = B00001110;
     }else if(reading > 900 && reading <= 1023){
       leds = B00001111;
     }
     
     /*int numLEDSLit = reading / 114; //1023 / 9 
     leds = 0; 
     for (int i = 4; i < numLEDSLit; i++)
     {
        bitSet(leds, i);
     }*/
     updateShiftRegister();
      
  }else if(reverseButtonState == HIGH){
     int reading = analogRead(throttlePotPin);
     reading = map(reading, 0, 1023, 1023, 0); 
     Serial.print(reading);
     Serial.print('\n');
     
     if(reading >= 0 && reading <= 650){
       leds = B00000000;
     }else if(reading > 650 && reading <= 700){
       leds = B00010000;
     }else if(reading > 700 && reading <= 800){
       leds = B00110000;
     }else if(reading > 800 && reading <= 900){
       leds = B01110000;
     }else if(reading > 900 && reading <= 1023){
       leds = B11110000;
     }
     
     /*int numLEDSLit = reading / 114; //1023 / 5
     leds = 0;
      
     for (int i = 4; i < numLEDSLit; i++)
     {
        bitSet(leds, i);
     }*/
     updateShiftRegister();
  }

}

void updateShiftRegister() //from left it starts
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

pushbutton picture.jpg

the pushbutton i am using is actually an On Off pushbutton.

Can you please clarify whether the pushbutton latches in each position ?

UKHeliBob: Can you please clarify whether the pushbutton latches in each position ?

well, mm the pushbutton just latches when it's on, it's an on off button but as you see in the sketch i used the internal pullup resistor

Try removing the debounce code and see what happens?

...R

Robin2: Try removing the debounce code and see what happens?

...R

haha thank you so much ! i don't know how I missed that ! totally dumb mistake :-[ :D

firashelou: haha thank you so much ! i don't know how I missed that ! totally dumb mistake :-[ :D

Do you mean that it worked ? - it was a very wild guess. :) :)

...R

Robin2: Do you mean that it worked ? - it was a very wild guess. :) :)

...R

lol yes it did ! +1 ;D

it was very simple but i didn't noticed all day trying to figure out what is the problem in the codes :-* :D

here are the new codes that works fine, very simple:

int throttlePotPin = A0;
int latchPin = 9;
int clockPin = 10;
int dataPin = 8;
int leds = 0;

int reverseButton = 3;



void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  
  pinMode(reverseButton, INPUT_PULLUP);
  Serial.begin(9600);
}

//Debounce function, it takes 2 parameters, the boolean state and the pin number of the input button
boolean debounce(boolean last, int switchPin)
{
  boolean current = digitalRead(switchPin);
  if(last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{  
   int reverseButtonState = digitalRead(reverseButton); 
  
   if(reverseButtonState == LOW){
     int reading = analogRead(throttlePotPin);
     reading = map(reading, 0, 1023, 1023, 0); 
     Serial.print(reading);
     Serial.print('\n');
     
     if(reading >= 0 && reading <= 650){
       leds = B00000000;
     }else if(reading > 650 && reading <= 700){
       leds = B00001000;
     }else if(reading > 700 && reading <= 800){
       leds = B00001100;
     }else if(reading > 800 && reading <= 900){
       leds = B00001110;
     }else if(reading > 900 && reading <= 1023){
       leds = B00001111;
     }
     
     /*int numLEDSLit = reading / 114; //1023 / 9 
     leds = 0; 
     for (int i = 4; i < numLEDSLit; i++)
     {
        bitSet(leds, i);
     }*/
     updateShiftRegister();
      
  }else if(reverseButtonState == HIGH){
     int reading = analogRead(throttlePotPin);
     reading = map(reading, 0, 1023, 1023, 0); 
     Serial.print(reading);
     Serial.print('\n');
     
     if(reading >= 0 && reading <= 650){
       leds = B00000000;
     }else if(reading > 650 && reading <= 700){
       leds = B00010000;
     }else if(reading > 700 && reading <= 800){
       leds = B00110000;
     }else if(reading > 800 && reading <= 900){
       leds = B01110000;
     }else if(reading > 900 && reading <= 1023){
       leds = B11110000;
     }
     
     /*int numLEDSLit = reading / 114; //1023 / 5
     leds = 0;
      
     for (int i = 4; i < numLEDSLit; i++)
     {
        bitSet(leds, i);
     }*/
     updateShiftRegister();
  }

}

void updateShiftRegister() //from left it starts
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}