Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Peter Cuellar
Created October 6, 2015

Blink Without Delay And Soldering a Ring

Homework for Intro to Design Innovation

Showcase (no instructions)23
Blink Without Delay And Soldering a Ring

Story

Read more

Code

State Machine

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

Peter Cuellar

Peter Cuellar

10 projects • 2 followers
Designer, Engineer, Environmentalist

Comments