The buttons are intended to Arm/Disarm and reset the alarm, but currently I am having trouble getting it to register the inputs. I have tested the buttons with an example program and everything appears to be functioning correctly. Here is my code so far, it would be great if somebody could read and give me any input/feedback you may have.
This worked fine in the test program, and I was under the impression that it amounted to the same thing. I will try swap it out and see if it improves things.
I changed the references to armbuttonState and used digitalRead to get all button events. Now the code will react and move on from standbymode on button press, but will just cycle through the other modes and return there.
Thanks for the help. Sorry, I should have mentioned that I was aware of that. Any ideas why it would jump past the alarm stage (it displays briefly on-screen) and go back to the standby?
You call this function from armsequence();, but as soon as it is done, it returns and the whole process repeats.
Perhaps you meant to use a while loop.
Before you put that in a while loop, I am going to point out one thing...
To begin with you have this sequence:
standbymode -> armsequence -> armed (loop in armed)
From the armed() function, you can then go to either call the functions alarm() or standbymode().
armed -> alarm -> standbymode
or
armed -> standbymode
I think you are getting confused with how C works. The loop() function once it gets to the end, repeats from the beginning again. This is because it is defined secretly as:
while(1){
loop();
}
However your functions once they reach the end exit and go back to where they were in the last function.
Here is some pseudo code to try and illustrate
loop:
call standbymode()
if button high
call armsequence()
call armed()
if sensor tripped
call alarm()
if button high
call standbymode() //notice here if you call standbymode() the whole sequence repeats?
if button high
call armsequence()
call armed()
if sensor tripped
call alarm()
if button high
call standbymode()
//notice here if you call standbymode() the whole sequence repeats?
return to armed();
return to alarm()
if button high
call standbymode() //notice here if you call standbymode() the whole sequence repeats?
//notice here if you call standbymode() the whole sequence repeats?
return to armed();
return to armsequence();
return to standbymode();
return to armed()
return to alarm()
if button high
call standbymode() //notice here if you call standbymode() the whole sequence repeats?
if button high
call armsequence()
call armed()
if sensor tripped
call alarm()
if button high
call standbymode()
//notice here if you call standbymode() the whole sequence repeats?
return to armed();
return to alarm()
if button high
call standbymode()
//notice here if you call standbymode() the whole sequence repeats?
return to armed();
return to armsequence();
return to standbymode();
return to armed()
return to armsequence()
return to standbymode()
return to loop()
jump to loop
What you need to do is let all the functions return to the loop. Once possibility is to have a variable which says which mode you are in.
Here is something to get you started. Shout if you need more help.
byte mode = 0;
void loop() {
switch (mode){
case 0:
standbymode();
break;
case 1:
armsequence();
break;
case 2:
armed();
break;
case 3:
alarm();
break;
default:
case = 0; //switch to standbymode if mode not defined
break;
}
}
void standbymode() {
digitalWrite(laserPin, LOW);
lcd.begin(16,2);
lcd.print("System Ready");
lcd.blink();
if (digitalRead(armButton) == HIGH) {
mode = 1; //arm sequence mode
return; //next time the loop runs, it will detect that mode =1 and call the armsequence() function.
//But as it is done from the main loop, you avoid recursion.
}
delay(100);
}
Spot on, and the only change I'd suggest is to change standbymode() to standby(). Both for consistency with the other function names, and to avoid a situation where you have two identifiers which only differ in capitalisation.
There's not much point in showing you what I've come up with, because it dosen't work.
Sure there is. But, you also need to describe what it actually does, and how that differs from what you want. There have been a lot of suggestions made, and no clue as to which of them you have implemented, or how.