There are several ways to accomplish this, but here's an easy one:
void setup()
{
pinMode(10, INPUT); // button
pinMode(11, OUTPUT); // LED
digitalWrite(11, LOW);
}
int pinState = HIGH;
const int x = 500; // 500ms delay
void loop()
{
int newState = digitalRead(10);
if (pinState != newState) // button state differs from last recorded state?
{
pinState = newState;
if (pinState == LOW) // button newly pressed?
delay(x);
digitalWrite(11, !pinState); // set LED HIGH if button LOW and vice versa
}
}
This is great, thanks.
Question: Does the delay cause the whole program to pause ? I will need to be able to have a 10 second delay...while watching the state of another pin, and digitalWrite if that state changes....
Yes. If you need to monitor an input continuously or do more than one thing at once, it is best to avoid calling delay.
A good strategy in circumstances like this is to do something like this in your loop:
Check the inputs to see if they have changed state. If so, respond accordingly. For example, if the button has just been released, turn off the LED. If the event needs to trigger some action in the future, then record the current time (millis()) and set a flag so that at a future time you can perform the action.