// This is the code for the interrupt which changes the case in my switch case
void interrupt(){
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{ if (state < 8){
state = state + 1;}
else {
state = 1;}
}
last_interrupt_time = interrupt_time;
}
I found this code online and I do not understand how it is working, but it does seem to be working. My main problem with the code is that when checking the if statment last_interrupt_time will allways equal zero since they assign it as zero two lines above the if statement. What is the point of the the last line of code then? I am also confused with what a "static variable" is? If someone could walk me through this that would be great. I am looking to possibly use one of these variables to put arduino to sleep after a 20 minute period, but dont understand how they work. Here is the project I am using it on:
I found this code online and I do not understand how it is working, but it does seem to be working.
Possibly because state is always less than smiley face.
My main problem with the code is that when checking the if statment last_interrupt_time will allways equal zero since they assign it as zero two lines above the if statement.
No, they don't. The static keyword means that the variable keeps its value between calls to the function, and the initialization only occurs once.
Thats funny I didnt even notice the face in there.
So what your saying is that by putting the "static" before the variable then last_interrupt_time = 0 only the first time the interrupt is called. All other times it will take on the value of interrupt_time ?