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!
Eric Chen
Created September 27, 2015

Homework 3: Automata (and Arduino Examples)

Pressing button, cutting wood, and stressing out.

BeginnerShowcase (no instructions)487
Homework 3: Automata (and Arduino Examples)

Story

Read more

Schematics

Circuit 1

Code

Example 1

C/C++
const int LED = 13;
const int BUTTON = 7;
int val = 0;

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

void loop(){
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

Example 2

C/C++
const int LED = 13;
const int BUTTON = 7;
int val = 0;
int state = 0;

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

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    state = 1 - state;
  }

  if (state == 1) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

Example 3

C/C++
const int LED = 13;
const int BUTTON = 7;
int val = 0;
int old_val = 0;
int state = 0;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);
}
void loop(){
  val = digitalRead(BUTTON);
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
  }

  old_val = val;

  if (state == 1) {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
}

Credits

Eric Chen

Eric Chen

9 projects • 2 followers
EECS student at UC Berkeley

Comments