Count press up and down within a range

I have the following code for counting button press:

//Button//
uint8_t buttonPin = 2;       // uint8_t is an unsigned (positive) 8-bit integer.  Giving the compiler this detail rather than a generic 'byte' helps it treat it as you would expect, resulting in Serial.print not needing that 'DEC' argument.
uint8_t buttonPressCount = 0; // Note an uint8_t stores a number from 0-255, so after 255 the buttonPressCount will go back to 0 again.  If you want to store a larger integer, use uint16_t (0-65535), uint32_t (0-4294967295), or even uint64_t (0-18446744073709551615).
uint8_t PUSHED = 0;
unsigned long timePushed = 0;
uint8_t currentButtonState = HIGH;
uint8_t lastButtonState = HIGH;
uint32_t millisTimePress = 0; // unsigned longs are uint32_t, an unsigned 32 bit integer.


void buttonPress ()
{
    byte currentButtonState = digitalRead (buttonPin);
    if (lastButtonState != currentButtonState)  {
        lastButtonState = currentButtonState;
         delay (35);             // debounce


        //******************
        //has the switch been pushed ?
        if (currentButtonState == PUSHED)
        {
           //record the time the switch closed
           timePushed = millis();
          
        }
        //******************
    //switch has been released
    else
    {
      //calculate the time the switch was closed
      timePushed = millis() - timePushed;
      
      if(timePushed < 1800)
      {
        buttonPressCount++;
        Serial.print("button count: ");
        Serial.println(buttonPressCount);
      }
      
      else if (timePushed > 2000)
      {
        buttonPressCount--;
        Serial.print("button count: ");
  Serial.println(buttonPressCount);

      }
      
    }
  }
}

How can I make this count to be within a limit of 0 and 4 so if counter is 4 next count up will be 0 and if count os 0 next count down will be 4

Thanks

Have a go at doing it yourself….
If it doesn’t work, post your new code here, and we can comment - so you lear where you missed something.

You can use an if statement:

if (value == something) value = something_else;

of you can use modulo arithmetic (but this does not work as well for the zero case):

value = (value + 1) % 4;

thanks. I have managed:

//Button//
uint8_t buttonPin = 2;       // uint8_t is an unsigned (positive) 8-bit integer.  Giving the compiler this detail rather than a generic 'byte' helps it treat it as you would expect, resulting in Serial.print not needing that 'DEC' argument.
uint8_t buttonPressCount = 0;// Note an uint8_t stores a number from 0-255, so after 255 the buttonPressCount will go back to 0 again.  If you want to store a larger integer, use uint16_t (0-65535), uint32_t (0-4294967295), or even uint64_t (0-18446744073709551615).
uint8_t lstbuttonPressCount = 0;
uint8_t PUSHED = 0;
const uint8_t MINBTNCOUNT = 0;
const uint8_t MAXBTNCOUNT = 4;
unsigned long timePushed = 0;
uint8_t currentButtonState = HIGH;
uint8_t lastButtonState = HIGH;
uint32_t millisTimePress = 0; // unsigned longs are uint32_t, an unsigned 32 bit integer.


void buttonPress ()
{
    byte currentButtonState = digitalRead (buttonPin);
  
    if (lastButtonState != currentButtonState)  {
        lastButtonState = currentButtonState;
         delay (35);             // debounce


        //******************
        //has the switch been pushed ?
        if (currentButtonState == PUSHED)
        {
           //record the time the switch closed
           timePushed = millis();
          
        }
        //******************
    //switch has been released
    else
    {
      //calculate the time the switch was closed
      timePushed = millis() - timePushed;
      
      if(timePushed < 500)
      {
        buttonPressCount++;
        if( buttonPressCount == 0)
  {
    buttonPressCount = 4;
  }
  if( buttonPressCount == 5)
  {
    buttonPressCount = 1;
  }
        Serial.print("button count: ");
        Serial.println(buttonPressCount);
      }
      
      else if (timePushed > 600)
      {
        buttonPressCount--;
        if( buttonPressCount == MINBTNCOUNT)
  {
    buttonPressCount = 4;
  }
  if( buttonPressCount > MAXBTNCOUNT )
  {
    buttonPressCount = 0;
  }
        Serial.print("button count: ");
  Serial.println(buttonPressCount);

      }
      
    }
  }
}

Hello hk_jh

Make it simple and stupid :nerd_face:

constexpr byte ButtonPin{A0};
void setup()
{
  Serial.begin(9600);
  pinMode(ButtonPin, INPUT_PULLUP);
}
void loop()
{
  static unsigned long stamp = millis();
  if (millis() - stamp >= 500 && !digitalRead(ButtonPin))
  {
    static bool direction = false;
    static int counter = 0;
    stamp = millis();
    Serial.print("counter= "), Serial.println(counter);
    direction ? counter-- : counter++;
    if (counter == 0 || counter == 4 ) direction = !direction;
  }
}

Have a nice day and enjoy coding in C++.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.