Thanks for all the replies,
I think I must have been more tired than I thought last night, since I didn't give a very good account of what this project is about!
I am making the controller for a UV exposure box, when I first thought about this, an up button, a down button and a start button (to set the time, and start the countdown,) would have sufficed.
But as often happens with projects, feature creep sets in. When I found the 'menubackend library' great, now I can introduce all sorts of options (most of which I don't need and will never use!)
Nevertheless it does provide a real and valuable learning experience.
So back on topic, I've tried to take in what everybody has said, and I'm quite pleased, because. I've managed to get something that works, almost!
I'm very grateful to all that have helped me along the way. I think the thing that opened my eyes was this comment from PaulS:-
The biggest problem with your code, though, is that you are not testing for when the switch BECOMES pressed, but rather testing when the switch IS pressed. So, as long as the switch IS pressed, the event time (when the switch became pressed) keeps moving forward. It doesn't stop moving until you release the switch.
The relationship between actual time and code time is the key, I'm not sure if I've put that succinctly, but anyway the code seemed much clearer after reading the quote above.
Here is the code:-
//Code:
const int buttonPinEnter = 11; // pin for the Enter button
const int buzzerPin = 12; // pin for the buzzer
int buzzerState = LOW; //variable to hold the state of the buzzer
byte buttonState = HIGH; //variable to hold the state of the button
byte lastButtonState = 0; //previous state of the button
boolean alarmSounding = false;
long AlarmDelay = 200;
long currentMillis =0;
long alarmStartMillis = 0;
void setup()
{
pinMode(buttonPinEnter, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
} // setup()...
void loop(){
currentMillis = millis();
readButtons();
activateAlarm();
soundAlarm();
} //loop()...
void readButtons(){ //read buttons status
//Enter button
// read the state of the switch into a variable:
buttonState = digitalRead(buttonPinEnter);
}
void activateAlarm() {
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
alarmStartMillis = millis();
alarmSounding = true;
Serial.println("button change start event");
}
else {
buttonState = HIGH;
alarmSounding = false;
Serial.println("button change stop event");
}
}
lastButtonState = buttonState;
}
void soundAlarm() {
if (alarmSounding == true) {
if (currentMillis - alarmStartMillis <= AlarmDelay) {
buzzerState = HIGH;
Serial.println("alarm run");
}
else {
buzzerState = LOW;
alarmSounding = false;
Serial.println("alarm stop");
}
}
digitalWrite(buzzerPin, buzzerState);
}
This works most of the time but sometimes the buzzer "locks on" and it takes a few more presses of the button to resume "normal" operation. I want to say that this because of bouncing in the switch, but I thought that the whole point of edge detect was to lock out changes after the first "key" change.
Anyway comments are most welcome.
Regards Steve.