/* Tested on an Uno */
/* Connect simple push to make buttons between 0V and pin 2, 0V and pin 3, 0V and pin 4 and between 0V and pin 5 */
#define noOfButtons 2 //Exactly what it says; must be the same as the number of elements in buttonPins
#define noOfLeds 2
#define bounceDelay 20 //Minimum delay before regarding a button as being pressed and debounced
#define minButtonPress 3 //Number of times the button has to be detected as pressed before the press is considered to be valid
const int buttonPins[2] = {2,3}; // Input pins to use, connect buttons between these pins and 0V
const int ledPins[2] = {4,5};
uint32_t previousMillis[noOfButtons]; // Timers to time out bounce duration for each button
uint8_t pressCount[noOfButtons]; // Counts the number of times the button is detected as pressed, when this count reaches minButtonPress button is regared as debounced
uint8_t testCount[noOfButtons]; //Test count, incremented once per button press
const char *Message_1[3] = {"OFF", "STANDBY", "CHARGING TIME"};
const char *Message_2[3] = {"MODE", "CHARGING TIME"};
void setup() {
testCount[0]=0; // default off
testCount[1]=5; // default 5hrs charge
uint8_t i;
uint8_t j;
uint32_t baudrate = 115200;
Serial.begin(baudrate);
Serial.println("");
Serial.print("Serial port connected: ");
Serial.println(baudrate);
//setup leds and test
for (j = 0; j < noOfLeds; ++j) {
pinMode(ledPins[j], OUTPUT);
// test leds
digitalWrite(ledPins[j], HIGH);
delay (500);
digitalWrite(ledPins[j], LOW);
}
//setup inputs buttons and display defaults
for (i = 0; i < noOfButtons; ++i) {
pinMode(buttonPins[i], INPUT_PULLUP);
Serial.print("Default Status ");
Serial.print(Message_2[i]);
Serial.print(" = ");
Serial.println(testCount[i]);
}
}
void loop() {
debounce();
delay(10); //Your other code goes here instead of this delay. DO NOT leave this delay here, it's ONLY for demonstration.
}
void debounce() {
uint8_t i;
uint32_t currentMillis = millis();
for (i = 0; i < noOfButtons; ++i) {
if (digitalRead(buttonPins[i])) { //Input is high, button not pressed or in the middle of bouncing and happens to be high
previousMillis[i] = currentMillis; //Set previousMillis to millis to reset timeout
pressCount[i] = 0; //Set the number of times the button has been detected as pressed to 0
} else {
if (currentMillis - previousMillis[i] > bounceDelay) {
previousMillis[i] = currentMillis; //Set previousMillis to millis to reset timeout
++pressCount[i];
if (pressCount[i] == minButtonPress) {
if(i==0){
if(testCount[i]<1){
++testCount[i]; //use for status
} else{
testCount[i] = 0;
}
}
if(i==1){
if(testCount[i]>1){
--testCount[i]; //use for status
} else{
testCount[i] = 5;
}
}
buttonPressed(i); //Button has been debounced. Call function to do whatever you want done.
}
}
}
}
}
void buttonPressed(int a){
int k = 0;
digitalWrite(ledPins[a], HIGH); // button press detected
delay (200);
digitalWrite(ledPins[a], LOW); // button press detected
Serial.print("Button Press ");
Serial.print(Message_2[a]);
Serial.print(" = ");
Serial.print(testCount[a]);
Serial.println("");
}