The sketch below is a part of a larger project, but I need to focus on this specific section first. My main requirement is to use a single button that can perform five different actions when pressed 1, 2, 3, or 4 times. Each additional press should trigger a different action. I have not come across this kind of functionality using an Arduino before, so I devised the following solution:
I connected one button to an Arduino, specifically to pin 2. Here's how it works:
- When the button is pressed once, it sends a web post for data logging and instructs the server to send the logged data over email using a PHP script.
- If the button is pressed twice, it initiates a web post every 15 seconds for one hour, continuously logging data during that time.
- The same concept applies for 3 and 4 presses, each triggering their unique actions.
Now, I'm seeking feedback on potential alternative approaches to achieve the same functionality using only one button. Any suggestions or improvements are welcome. Sketch provided:
--- Note: this code does work ---
/*
This is part of a much larger project but i am
shearing it because I could not find anything out there lik it
*/
#define buzzPin 10
const int buttonPin = 2;
int buttonPressCount = 0;
unsigned long lastResetTime = 0; // Variable to store the last time the button was pressed
unsigned long elapsedTime = 0; // Variable to store the elapsed time since the last reset
void setup() {
pinMode(buzzPin, OUTPUT);
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void buzzer() {
while (buttonPressCount > 0) {
digitalWrite(buzzPin, HIGH);
delay(50);
digitalWrite(buzzPin, LOW);
delay(400);
buttonPressCount--;
}
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
// Button has been pressed, update lastResetTime and increment buttonPressCount
lastResetTime = millis();
buttonPressCount++;
// Provide audible indication using the buzzer
digitalWrite(buzzPin, HIGH);
delay(50);
digitalWrite(buzzPin, LOW);
delay(500);
// Print buttonPressCount for debugging purposes
Serial.print(buttonPressCount);
Serial.println(" :buttonPressCount");
}
// Calculate the elapsed time since the last button press
elapsedTime = millis() - lastResetTime;
Serial.print("Elapsed Time (ms): ");
Serial.println(elapsedTime);
// Check if the button has been pressed multiple times and within the time window
if (buttonPressCount >= 1 && buttonPressCount <= 7 && elapsedTime >= 1500 && elapsedTime <= 2000) {
// Execute corresponding actions based on buttonPressCount
Serial.print("Stop - Button Pressed - Option: ");
Serial.println(buttonPressCount);
switch (buttonPressCount) {
case 1:
Serial.println("Option 1 chosen");
buzzer();
// add more code here: run motor for sometime foward
break;
case 2:
Serial.println("Option 2 chosen");
buzzer();
// add more code here: run motor for sometime backward
break;
case 3:
Serial.println("Option 3 chosen");
buzzer();
// add more code here: flash light 20 times
break;
case 4:
Serial.println("Option 4 chosen");
buzzer();
// add more code here: turn off a buzzer
break;
case 5:
Serial.println("Option 5 chosen");
buzzer();
// add more code here: turn off a buzzer but not an led
break;
case 6:
Serial.println("Option 6 chosen");
buzzer();
// add more code here: turn off an led but not a buzzer - you get the idea
break;
case 7:
Serial.println("Option 7 chosen");
buzzer();
break;
default:
Serial.println("Invalid option");
break;
}
digitalWrite(buzzPin, LOW);
//buttonPressCount = 0;
/* not need due to "void buzzer()"
function Reset buttonPressCount after
button press and option selection*/
Serial.println("Button Press Count reset");
delay(6000);// This delay is only for helping see text on the serial monitor
}
}
thanks.