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
//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);
}
}
}
}