would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)
const int Button = A0;
const int Light = 8;
unsigned long time;
int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000
void setup(){
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}
void loop(){
buttonstate = digitalRead(Button);
if(buttonstate == HIGH){
timeVal = time;
digitalWrite(Light, HIGH);
}
else{
}
if(timeoffVal = time) {
timeoffVal = 0;
digitalWrite(Light, LOW);
}
}
You left off a semicolon up in the declarations. This resulted in a weird error on my Arduino IDE. I didn't get the "A0 not declared in this scope" error though. When I added the semicolon the error went away and the sketch compiled.
However... it still doesn't do what you want or need.
First, you initialize a variable "time" but you don't give it a value. This usually results in a value of 0 but you really ought to do it explicitly to be sure. Then, in loop{} , nothing happens to change this value.
Next, in the last if statement you have the wrong syntax. You are assigning (=) rather than comparing (==). This doesn't result in a compiler error because it's OK to do this if it's what you really need, but in this case it isn't, and it always makes the "if" statement enclosing it evaluate as "true".
Ok, so those are the easy errors. Now let's flow through the loop{} after corrections.
First, read the button.
Now if the button is High, assign the value in "time" (0) to "timeVal". Now both "time" and "timeVal" contain the value 0.
Then turn ON the light.
Now test to see if "timeoffVal" is equal to "time", which of course is 0. But "timeoffVal" was declared as the initial value of "timeVal" (0) plus 30000, so the test fails and the next two statements aren't executed. So "timeoffVal" stays at 30000 and the light isn't turned off, so it stays ON.
Now go back to the top of the loop{} and read the button.
Now if the button is LOW, the first if statement fails so the next two statements aren't executed. "timeVal" stays at 0 and the light stays at whatever it was at before, which is ON.
Now test to see if "timeoffVal" is equal to "time", which of course is 0. But "timeoffVal" still has what it had the last time through which is 30000, so the test fails and the next two statements aren't executed. So "timeoffVal" stays at 30000 and the light isn't turned off.
And go to the top of the loop{} .....
So when you start the sketch, with the button "unpressed" (LOW), the light will be off. Then when you press the button the light will turn on and stay on, and remain on no matter what else happens with the button, until the sketch is restarted.
If you include
digitalWrite(Light, LOW);
inside that empty else statement at least the light will turn off when the button goes LOW, but I think you want the light to stay on for a while once it has been turned on, even if the button is "unpressed" after being pressed.
So I think what you need to do is to test to see if some length of time has elapsed since the light was turned on, and if it has, turn off the light.