Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Global Variables
- const byte BUTTON=2; // our button pin
- const byte LED=13; // LED (built-in on Uno)
- unsigned long buttonPushedMillis; // when button was released
- unsigned long ledTurnedOnAt; // when led was turned on
- unsigned long turnOnDelay = 2500; // wait to turn on LED
- unsigned long turnOffDelay = 5000; // turn off LED after this time
- bool ledReady = false; // flag for when button is let go
- bool ledState = false; // for LED is on or not.
- void setup() {
- pinMode(BUTTON, INPUT_PULLUP);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, LOW);
- }
- void loop() {
- // get the time at the start of this loop()
- unsigned long currentMillis = millis();
- // check the button
- if (digitalRead(BUTTON) == LOW) {
- // update the time when button was pushed
- buttonPushedMillis = currentMillis;
- ledReady = true;
- }
- // make sure this code isn't checked until after button has been let go
- if (ledReady) {
- //this is typical millis code here:
- if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
- // okay, enough time has passed since the button was let go.
- digitalWrite(LED, HIGH);
- // setup our next "state"
- ledState = true;
- // save when the LED turned on
- ledTurnedOnAt = currentMillis;
- // wait for next button press
- ledReady = false;
- }
- }
- // see if we are watching for the time to turn off LED
- if (ledState) {
- // okay, led on, check for now long
- if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
- ledState = false;
- digitalWrite(LED, LOW);
- }
- }
- }
- /* Code Example on how to create virtual delays using Arduino Millis()
- ** written by James at www.baldengineer.com
- **
- ** For more examples and full explanation visit:
- ** https://www.baldengineer.com/use-millis-with-buttons-to-delay-events.html
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement