Can someone expain me how the count up and down works in this code


int FWGear = 3; //Foward button
int BWGear = 4; //Backward button

//variables
int GearCount = 1;
int FWGearNew;
int FWGearOld = 1;
int BWGearNew;
int BWGearOld = 1;

void setup ()
{

  pinMode(FWGear, INPUT_PULLUP); //Foward button
  pinMode(BWGear, INPUT_PULLUP); //Backward button


}

void loop ()
{
  FWbutton();
  BWbutton();
}

void FWbutton()
{
  FWGearNew=digitalRead(FWGear);
  delay(100);
   if(FWGearOld==1 && FWGearNew==0 && GearCount <8)
   {
    GearCount = GearCount += 1;
   }
   FWGearOld=FWGearNew;
}

// Backward Gear Button Count

void BWbutton()
{
  BWGearNew=digitalRead(BWGear);
  delay(100);
   if(BWGearOld==1 && BWGearNew==0 && GearCount >1)
   {
    GearCount = GearCount -= 1;
   }
   BWGearOld=BWGearNew;
}

Which statement, I see some bad code that should be changed to a simple
GearCount++
and
GearCount--

can't understand the part FWGearOld=FWGearNew? What does that means

The old value is replaced by the new value.

dear if you are not busy, can you explain in little more,
how counter worked and replace the new value

You need to take a course in basic C.

This is the second times (maybe more) that you come to this forum with this code...
I assume you didn't do it; so where does it come from?

bro i didnt say that I wrote this code,
I'm trying to learn!
with my office work, i dont have time do courses, so at night i ask from other and try to read and learn ,
is learning wrong bro?

Hey why do you take this wrong?
The first time I just thought you needed help to do something, in your own code.
It seems like it's not your code, I'm just asking
So where does it come from?

I did some chages as i need the code, but actually a guy on redit told me to use the code for count
but after few days that guy dissappered and his acc is deleted. now he left many questions ,
so trying to ask from others and learn.

FWGearNew = digitalRead(FWGear);
This line reads the current state of the forward button (connected to pin 3) and stores it in the variable FWGearNew.

If the button is not pressed, it will read HIGH.

If the button is pressed, it will read LOW, because we're using INPUT_PULLUP.

delay(100);
This short pause (100 milliseconds) is added to avoid false triggering due to button bouncing. When a button is pressed, it may send quick, noisy signals for a few milliseconds. This delay helps ignore that noise.

if (FWGearOld == 1 && FWGearNew == 0 && GearCount < 8)
This if statement checks three conditions at once:

  1. FWGearOld == 1: The button was not pressed the last time we checked (previous state was HIGH).

  2. FWGearNew == 0: The button is now pressed (current state is LOW).

  3. GearCount < 8: We're not already at the maximum gear (which is 8).

If all three conditions are true, it means the button was just pressed and we can safely increase the gear count.

  {
    GearCount += 1;
  }

This line increases the gear count by 1.

FWGearOld = FWGearNew;
Finally, we update FWGearOld with the current button state, so next time we call this function, we can detect if the button has been newly pressed again.

hey thanks alot,
I understood it perfectly.
thanks for teaching :heart_eyes:

  • Did you try the code offered you here ?

Just now I saw this, Ill try this,
Thanks for the teaching
Much love :heart:

The C Programming Language was written by one of the inventors of C, is brief and has good examples that can help you learn how to write good programs

@Anthony_P provided a good explanation. But I think it's interesting that you didn't ask about the delay.

the delay is to avoid swith bounce which would have resulted in multiple increments although the button was pressed just once

but the code calls the delay regardless if there is a button state change which means there's always a 100 msec delay

a better approach would be to only call the delay when there's a state change

  FWGearNew=digitalRead(FWGear);

   if(FWGearOld==1 && FWGearNew==0 && GearCount <8)
   {
     delay(100);
     GearCount = GearCount += 1;
   }
   FWGearOld=FWGearNew;

another aspect of this code is that it's specific for incrementing a count instead of being a bit more generic by separately recognizing when the button press and incrementing the count.

also, GearCount = GearCount += 1 is redundant and can be replaced with the increment , "++" operation

  FWGearNew=digitalRead(FWGear);

   if(FWGearOld==1 && FWGearNew==0)
   {
     FWGearOld=FWGearNew;
     delay(100);
     if (GearCount <8)
         GearCount++;
   }

And this code is specific for recognizing when the button is pressed. What if you want to recognize either a press or release? And all you need to know is old state of the button

    byte but = digitalRead (FWGear);

    if (FWGearOld != but)      {
        FWGearOld = but;
        delay (100);

        if (HIGH == but)  {
            if (GearCount < 8)
                GearCount++;
            Serial.println (GearCount);
        }
    }

and finally, Capitalize Constant, not variable or function name, and i think it's a good idea to make pin names easily identifiable

const byte PinFwGear = A2;
byte       fwGear;

const int  MaxCount = 8;
int        gearCount;

// -----------------------------------------------------------------------------
void
loop ()
{
    byte but = digitalRead (PinFwGear);

    if (fwGear != but)      {
        fwGear = but;
        delay (100);

        if (HIGH == but)  {
            if (gearCount < MaxCount)
                gearCount++;
            Serial.println (gearCount);
        }
    }
}

i hope you can see the value that the outside if handles button state changes and the inside if does what the press or release needs to do