Hi guys,
First post, hello all!
I've only recently started with arduino, primarily as a way into programming with C. I've done a little assembly with PIC's, but all the advice suggested learning C.
So on to the problem... I'm trying to write a programme for a UV exposure box, so it's all about timers and LCD's really.
I've managed to track down code/library for the LCD menu and modified that to suit my needs. Now I need to get to grip with timers, so I thought start small and move on from there. What I'm trying to achieve is when a button is pressed I get a beep from my buzzer.
I've looked at blink without delay, and also read this very useful post by Robin2:-
http://forum.arduino.cc/index.php?topic=223286.0
I've cobbled some code together but I'm not getting the behavior I expect...
When the button is pressed, I get a recurring bleep and on release the buzzer remains in one state or the other.
Here is the code:-
const int buttonPinEnter = 11; // pin for the Enter button
const int buzzerPin = 12; // pin for the buzzer
int lastButtonPushed = 0;
int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
long lastEnterDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 200; // the debounce time
int buzzerState = LOW;
long timeThen = 0; //declare local variables
long AlarmDelay = 200;
void setup()
{
pinMode(buttonPinEnter, INPUT);
pinMode(buzzerPin, OUTPUT);
} // setup()...
void loop()
{
readButtons();
if (lastButtonPushed == buttonPinEnter){
AlarmTest();
}
else {
lastButtonPushed =0;
}
//lastButtonPushed = 0;
} //loop()...
void readButtons(){ //read buttons status
int reading;
int buttonEnterState=LOW; // the current reading from the Enter input pin
int buzzerPin=LOW; //the current buzzer pin state
//Enter button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEnter);
// check to see if you just pressed the enter button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEnterState) {
// reset the debouncing timer
lastEnterDebounceTime = millis();
}
if ((millis() - lastEnterDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEnterState=reading;
lastEnterDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEnterState = reading;
//records which button has been pressed
if (buttonEnterState==HIGH){
lastButtonPushed=buttonPinEnter;
}else{
lastButtonPushed=0;
}
}
void AlarmTest() {
unsigned long timenow = millis();
if ((timenow - timeThen) > AlarmDelay) {
timeThen = timenow; // save last time buzzer on
if (buzzerState == LOW) //toggle buzzer
buzzerState = HIGH;
else
buzzerState = LOW;
digitalWrite(buzzerPin, buzzerState); //set buzzer action
}
}
Can anyone point me towards the problem?
Thanks in advance, Steve.