logical timing

I have some doubts about logical timing, or whatever it's called.
For example,
A=1,
if A==1, then aCount++;

what will happen to this aCount value? will it keep going up indefinitely?

I have this code to set sbA HIGH when button A is pressed, and once released, sbA goes LOW;

if (cbuttonA != pbuttonA){
if(cbuttonA==LOW){     // buttonA
sbA=1;
}
else sbA=0;
}
pbuttonA=cbuttonA;

and I have this code to count the button, but it doesn't work.

if(sbA==1){
count++;}

I know I can put count++ directly to the button code above, but I am writing a program that have different modes and so the buttons have different functions. essentially, I am try to make an universal button state value, so that every mode can use them in different ways. any suggestion?

You must do the increment in the code that tests cButtonA != pButtonA etc.

The whole point is to only increment when the button state changes, not every time
round the loop(). If you set a variable saying "the button changed" then that will continue
to read true in the future, you need to execute your state-change code when the state
changes, not if you've seen it change at some point in the past.

This is the idiom to use, don't change it to something different!

  cButtonA = digitalRead (....) ;
  if (cButtonA == LOW && pButtonA == HIGH)  // button pin transition to LOW
  {
    count++ ;
  }
  pButtonA = cButtonA ;

[ you probably also need debouncing code ]

MarkT:
You must do the increment in the code that tests cButtonA != pButtonA etc.

The whole point is to only increment when the button state changes, not every time
round the loop(). If you set a variable saying "the button changed" then that will continue
to read true in the future, you need to execute your state-change code when the state
changes, not if you've seen it change at some point in the past.

This is the idiom to use, don't change it to something different!

  cButtonA = digitalRead (....) ;

if (cButtonA == LOW && pButtonA == HIGH)  // button pin transition to LOW
 {
   count++ ;
 }
 pButtonA = cButtonA ;




[ you probably also need debouncing code ]

you kinda confuse me more. so, I need to write code to detect changes from LOW to HIGH, but not just HIGH?

you kinda confuse me more. so, I need to write code to detect changes from LOW to HIGH, but not just HIGH?

Yes. You need to do the increment when the button state becomes HIGH, not when it is HIGH otherwise the increment will happen every time through loop() if the button state is HIGH.

UKHeliBob:

you kinda confuse me more. so, I need to write code to detect changes from LOW to HIGH, but not just HIGH?

Yes. You need to do the increment when the button state becomes HIGH, not when it is HIGH otherwise the increment will happen every time through loop() if the button state is HIGH.

thanks...then, I need to write every button code for each function??

If you want the button state to be available to all functions in your program declare it as global. Is that what you mean ?

UKHeliBob:
If you want the button state to be available to all functions in your program declare it as global. Is that what you mean ?

I think my button is global. I want every function to interpret the button differently, and I don't want to write every button code for every function. Is there a simpler way to do that?

You are not making yourself clear here. Can you post your code where you expect every function to take a diffrent action from the same button press.