Hi Robin,
Thank's for your patience, I'm sure it must get tedious, answering the same questions over and over again!
I've re-written the code, simplifying the button press as you suggested, but I'm afraid I get exactly the same behavior as before.
i.e. The buzzer sounds as soon as the button is pressed, and when the button is released, then the delay kicks in. Just to be clear the behavior I was expecting is:-
Button pressed => buzzer sounds =>timed delay(100/200ms) => buzzer stops (regardless of whether the button is held down or not.)
The behavior I get is:-
Button pressed => buzzer sounds => button released => (read as delay starts here) buzzer continues until delay is up => buzzer stops.
Perhaps, we could leave that aside for a moment (I'm a little frustrated.) As far as I can see it, the concept of using Millis() as a timed delay, works like this:-
We issue a variable "A" with a timestamp (Millis()), next we issue another variable "B" with a later timestamp (+Millis()), then we compare the difference of the two variables against a known quantity "I" (interval), then depending on whether the difference is larger or smaller than the quantity "I" (interval) a descision is made.
So to make a truth table of this we get:-
B - A > I = true
B - A = I = true
B - A < I = false
Am I correct?
Here is the simplified version of my/your 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 = true; //variable to hold the state of the button
boolean alarmSounding = false;
long AlarmDelay = 500;
long currentMillis =0;
long alarmStartMillis = 0;
void setup()
{
pinMode(buttonPinEnter, INPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); //for debugging
} // 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 == false) {
alarmStartMillis = millis();
alarmSounding = true;
Serial.println("timer started");
}
}
void soundAlarm() {
if (alarmSounding == true) {
if (currentMillis - alarmStartMillis <= AlarmDelay) {
buzzerState = HIGH;
Serial.println("alarm started");
}
else {
buzzerState = LOW;
alarmSounding = false;
Serial.println("alarm stopped");
}
}
digitalWrite(buzzerPin, buzzerState);
}
I've stepped through the code and I think it should work.... Probably a loose wire in front of the keyboard ![]()
If you keep the button pressed strange things will happen and you could extend the code to deal with that - but is that necessary?
This comment got me thinking... and actually I should be able to do this simply using delay(), since the MCU will only countdown, update the lcd and switch a mosfet on and off.
However I might need an external interupt to abort the countdown sequence should there be a problem, would that be a problem if using delay()?
Anyway I feel that I should persist with millis() delay, even just so that I understand it and am able to use the full flexibility of the MCU.
Regards Steve.