Eric Chen
Created October 1, 2015

Homework 4: No-Delay() Blink and Soldering

One-click change and lots of fumes.

BeginnerShowcase (no instructions)40
Homework 4: No-Delay() Blink and Soldering

Story

Read more

Schematics

Circuitry

Code

No-Delay Example 1

C/C++
const int LED = 13, BUTTON = 7;

int ledState = HIGH, buttonState, lastButtonState = LOW;
boolean blinkState = false;

long lastDebounceTime = 0, debounceDelay = 50;
unsigned long previousMillis = 0;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);

  digitalWrite(LED, ledState);
}

void loop() {
  unsigned long currentMillis = millis();
  int reading = digitalRead(BUTTON);
  if (reading != lastButtonState) {
    lastDebounceTime = currentMillis;
  }

  if ((currentMillis - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        blinkState = !blinkState;
      }
    }
  }
  if (currentMillis - previousMillis >= ((blinkState) ? 1000 : 100)) {
    previousMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(LED, ledState);
  }
  lastButtonState = reading;
}

Credits

Eric Chen
9 projects • 2 followers
EECS student at UC Berkeley

Comments