Daniel Pok
Created October 5, 2015

HW4: Arduino Delay

Arduino without the delays

BeginnerShowcase (no instructions)965
HW4: Arduino Delay

Things used in this project

Story

Read more

Schematics

Untitled.jpg

Code

Untitled file

C/C++
int oldVal = 0;
unsigned long stateChangedAt;
int stateChangeDelay;
int state = 0;
const int DELAY_FAST = 100;
const int DELAY_SLOW = 1000;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(8, INPUT);

  stateChangedAt = millis();
  oldVal = 0;
  stateChangeDelay = DELAY_SLOW;

  digitalWrite(13, LOW);
}

void loop() {
  
  int newVal = digitalRead(8);
  if (newVal && !oldVal) {
    delay(100);
    if (stateChangeDelay == DELAY_SLOW) stateChangeDelay = DELAY_FAST;
    else stateChangeDelay = DELAY_SLOW;
  }
  oldVal = newVal;

  if (millis() > stateChangedAt + stateChangeDelay){
    state = 1 - state;
    if(state == 1) {
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
    stateChangedAt = millis();
  }
  
}

Credits

Daniel Pok

Daniel Pok

6 projects • 1 follower

Comments