Hi guys
I am going crazy over this. I need to have a switch, that when pressed turn on a LED (digitalWrite). Then after
a set duration (like 100 ms), it goes low. This should occur, even if the button stays pressed (or is released)
I have tried a thousand things, this is the latest. I really hope somebody can help me out
Moderators note : used code tags
I am going crazy over this. I need to have a switch, that when pressed turn on a LED (digitalWrite). Then after
a set duration (like 100 ms), it goes low. This should occur, even if the button stays pressed (or is released)
I have tried a thousand things, this is the latest. I really hope somebody can help me out
C:
// constants won't change. Used here to set a pin number :
const int ledPin = 13;
const int button = 21;
unsigned short timer = 0; //Create a variable with range 0-65535
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
delay(1); //1 millisecond delay so you know how often this loop runs (appx 1000Hz)
// here is where you'd put code that needs to be running all the time.
if (digitalRead(button) == HIGH)
{
digitalWrite(ledPin, HIGH); //Turn on LED
timer = 1000; //Turn it off after 1 second
}
if (timer) { //Is timer active? (more than 0)
timer--; //Decrement by 1
if (timer == 0) { //Did timer run out?
digitalWrite(ledPin, LOW); //Turn off LED
}
}
}