State variables and millis help.

OK guys,

I'm sorry I'm a little late replying, we've had some illness in the family, luckily it's not serious, but it has consumed more of my time than usual!

Well where shall I start? HHHMM...

@CrossRoads

That makes complete sense, I shall make it "Force of habit" !!!

@Robin2

I had assumed that your intention, by breaking the code down to separate "activate" and "soundalarm" functions was to make it clearer for a noob like me to understand what was going on in the code.

Anyway, I ended up re-writing the code several times before I was able to achieve something even remotely like what I require.

I added some serial output to help with debugging, something I shall bear in mind for the future......

Here is the code as it stands:-

const int buttonPinEnter = 11;   // pin for the Enter button
const int buzzerPin = 12;        // pin for the buzzer

int lastButtonPushed = 0;
int lastButtonState = HIGH;   // the previous reading from the Enter input pin
int buzzerState = LOW;
int buttonEnterState=HIGH;             // the current reading from the Enter input pin

boolean alarmSounding = false;

long AlarmDelay = 200;
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
  int buttonState;


  //Enter button
  // read the state of the switch into a local variable:
  buttonState = digitalRead(buttonPinEnter);
  // check to see if you just pressed the enter button 
  // (i.e. the input went from HIGH to LOW )                  
  if (buttonState != lastButtonState) {
    // If the switch changed, due to noise or pressing:
    if (buttonState == LOW) {
      buttonEnterState = buttonState;
      Serial.println("button pushed");
    }                

    // save the reading.  Next time through the loop,
    // it'll be the lastButtonState:
    lastButtonState = buttonState;

    //records which button has been pressed
    if (buttonEnterState==buttonState){
      lastButtonPushed=buttonEnterState;
      Serial.println("buttton set");
    }
    else{
      lastButtonPushed=1;
    }                  
  }
}           

void activateAlarm(){

  if (lastButtonPushed == buttonEnterState) {
    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);
}

As you can see, I've used the button change state code to trigger the alarmdelay (duration). (at least, that's what I think I'm doing!!)

However, what is actually happening, is that the alarm(buzzer) sounds as soon as the button is pressed, and the alarmdelay(duration) "period" is 'run' when the button is released.

This is particularly noticable if the delay time is extended, for instance to 500ms.

Looking at the serial output :-

button pushed
buttton set
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm stopped

We get multiple instances of 'timer started', is this due to switch bounce, and the alarm actually starts at the first instance of 'alarm started' ?

No it isn't, it can't be, since if the button is held down, the alternating pattern continues......

button pushed
buttton set
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
timer started**
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm started
alarm sto

I have a feeling that I should change something in the function 'void activateAlarm()'?

I should say I'm trying to get a bleep of a certain duration whether the button is pressed or held....

Thanks in advance,

Steve.