Week 3: Digital Input, Analog Input and Digital Output
Combo Lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int led_pin = 8; | |
int switch_pin = 9; | |
int pot_pin_1 = A1; | |
int pot_pin_2 = A2; | |
int pot_pin_3 = A3; | |
int last_switch_state = LOW; | |
void setup() { | |
pinMode(led_pin, OUTPUT); | |
pinMode(pot_pin_1, INPUT); | |
pinMode(pot_pin_2, INPUT); | |
pinMode(pot_pin_3, INPUT); | |
pinMode(switch_pin, INPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
int pot_one_reading = analogRead(pot_pin_1); | |
delay(1); | |
int pot_two_reading = analogRead(pot_pin_2); | |
delay(1); | |
int pot_three_reading = analogRead(pot_pin_3); | |
delay(1); | |
int switch_reading = digitalRead(switch_pin); | |
// Potentiomenter position mapped to 0-9 digits | |
int pot_one_digit = map(pot_one_reading, 0, 1024, 0, 10); | |
int pot_two_digit = map(pot_two_reading, 0, 1024, 0, 10); | |
int pot_three_digit = map(pot_three_reading, 0, 1024, 0, 10); | |
Serial.print("\t"); | |
Serial.print(pot_one_digit); | |
Serial.print("\t"); | |
Serial.print(pot_two_digit); | |
Serial.print("\t"); | |
Serial.println(pot_three_digit); | |
// Evaluate when switch is pressed | |
if (last_switch_state == LOW && switch_reading != last_switch_state) { | |
if (pot_one_digit == 3 && pot_two_digit == 6 && pot_three_digit == 9) { | |
digitalWrite(led_pin, HIGH); | |
} else { | |
digitalWrite(led_pin, LOW); | |
} | |
} | |
last_switch_state = switch_reading; | |
delay(100); | |
} |
Using Arduino’s Serial Monitor to display current combo digits (because
I was too lazy to do it with p5).