Combo Lock

My first of many ITP posts…

Here’s my combination lock for the first Physical Computing assignment.

IMG_0620  

You need to find the right combo using the switches to light the green LED. It works kind of like Mastermind in that the yellow LED will light up when you do something that is part of the combo. If the red LED lights up after the yellow one, that means you did the right thing, but at the wrong time. 


HINTS:

  • You can press more than one push-button at a time.
  • That shiny thing is a mercury switch.
For the solution, you’ll have to look through the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//-------------------------------
// constants
int switch1_Pin = 2;
int switch2_Pin = 3;
int switch3_Pin = 4;
int tilt_Pin = 5;
 
int red_Pin = 9;
int yellow_Pin = 10;
int green_Pin = 11;
 
int middle_State = 0;
int outer_State = 1;
int tilt_State = 2;
int open_State = 3;
 
//-------------------------------
// global variables
int state;
boolean error;
 
//-------------------------------
void setup() {
  // set the switch pins as inputs
  pinMode(switch1_Pin, INPUT);
  pinMode(switch2_Pin, INPUT);
  pinMode(switch3_Pin, INPUT);
  pinMode(tilt_Pin, INPUT);
 
  // set the LED pins as outputs
  pinMode(red_Pin, OUTPUT);
  pinMode(yellow_Pin, OUTPUT);
  pinMode(green_Pin, OUTPUT);
 
  // set the state to start
  state = middle_State;
}
 
//-------------------------------
void loop() {
  error = false;
 
  // 1. press the middle button
  if (digitalRead(switch2_Pin) == 1) {
    // make sure it's only the middle button
    if (digitalRead(switch1_Pin) == 1 || digitalRead(switch3_Pin) == 1) {
      error = true;
    } else {
      flash(yellow_Pin, 500);
      if (state == middle_State) {
        // correct, move to the next state
        state = outer_State;
      } else {
        error = true;
      }
    }
  }
 
  // 2. press the outer buttons
  else if (digitalRead(switch1_Pin) == 1 && digitalRead(switch3_Pin) == 1) {
    // make sure the middle button is not pressed
    if (digitalRead(switch2_Pin) == 1) {
      error = true;
    } else {
      flash(yellow_Pin, 500);
      if (state == outer_State) {
        // correct, move to the next state
        state = tilt_State;
      } else {
        error = true;
      }
    }
  }
 
  // 3. tilt the breadboard
  else if (digitalRead(tilt_Pin) == 1) {
    flash(yellow_Pin, 500);
    if (state == tilt_State) {
      // correct, open the lock
      state = open_State;
    } else {
      error = true;
    }
  }
 
  // if only one outer button is pressed, flag the error
  else if (digitalRead(switch1_Pin) == 1 || digitalRead(switch3_Pin) == 1) {
    error = true;
  }
 
  // analyze the results after this iteration
  if (error) {
    // flash the red light
    flash(red_Pin, 2 * 1000);
    // reset the state
    state = middle_State;
  }
  else if (state == open_State) {
    // flash the green light
    flash(green_Pin, 2 * 1000);
    // reset the state
    state = middle_State;
  }
 
  // wait a bit before iterating again
  delay(100);
}
 
//-------------------------------
void flash(int pin, int ms) {
  digitalWrite(pin, HIGH);
  delay(ms);
  digitalWrite(pin, LOW);
}
I haven’t bought an Arduino yet, so I had to pull this one out of my faceCloth_Live controller. I had forgotten how messy that thing is, I’m surprised it actually worked.
IMG_1573 

0 Responses to “Combo Lock”


  • No Comments

Leave a Reply