while(statebutton!=HIGH)
{
modifyLights();
statebutton=digitalRead(BUTTON);
if(statebutton==HIGH)
{
untilhere=millis();
while(statebutton==HIGH)
{
statebutton=digitalRead(BUTTON);
}
time=millis()-untilhere;
}
}
There looks to be a bit of a problem here. If your outer while loop is checking that statebutton!=HIGH, then you are pretty likely to leave this loop as soon as it does go HIGH so the if statement: if(statebutton==HIGH) will probably never be reached (unless you are extremely lucky and it happens to change in the loop). Even if you do happen to get the change between entering the while loop and the if statement, your inner while loop will continue until statebutton is low again, and so you won't exit the outer while loop.
Effectively you either go through outer loop indefinitely or you leave it without ever entering the inner loop.
For what (I think) you are trying to do you would need to do:
while(statebutton!=HIGH)
{
modifyLights();
statebutton=digitalRead(BUTTON);
}
untilhere=millis();
while(statebutton==HIGH)
{
statebutton=digitalRead(BUTTON);
}
time=millis()-untilhere;
Though I'm not sure if this is actually what you want to do, one of the main problems you have with this code is the way that the flow is structured, i.e. the way you step through your code. The way you have written it is:
while waiting for user input:
check light level
if the lights are low: turn on LEDs, use buzzer, display message
else turn off LEDs
when button has been pressed:
check how long button is pressed down for
if it's less than 1200ms turn on an LED, display message
if it's greater than 1200ms turn off an LED, display message
But this way the user input is what causes the program to continue.
I think what you are trying to do is more along the lines of:
while light level is high: do nothing
when light level goes low: turn on LEDs, use buzzer, display message
wait for user input
check how long button is pressed down for
if it's less than 1200ms turn on an LED, display message
if it's greater than 1200ms turn off an LED, display message
This way the routines will be triggered by the light level, and then it will wait for the user input. You can add more advanced logic, for example making that it self-clear when the light goes high again, but this just requires a bit of thought. I would also be a bit careful with the way you check for user input, at the moment if they press the button for less then 800 ms your program doesn't define a state of what should happen, you should add some extra code for this case and loop until your user input is valid.
Hope this helps,
Tobyb121