Tuesday, May 27, 2008

Lab 2

This lab was to create a "combination lock" using the Arduino to trigger the final switch. We were to create a homemade switch from junk. I tried various things, but ended up using two pair of chopsticks. At each narrow end, I attached aluminum foil; when the correct pairs came in contact, the unlocker should also use the pressure switch to trigger the access to the "treasure," in this case, a yellow LED.


It was a simple design, but was interesting as a lock; it was very difficult to actually hold the chopsticks together and simultaneously press the trigger, which I left dangling. Notice the tape on the chopsticks above. If someone had help, it would be easy. Use it to open a big, heavy door to add danger. Here's code:

// declare variables:
int switch1 = 2; // digital input pin for a switch
int switch2 = 3; // digital input pin for a switch
int switch3 = 4; // digital input pin for a switch
int ledPin = 13; // digital output pin for an LED
int switchState1 = 0; // the state of the switch
int switchState2 = 0; // the state of the switch
int switchState3 = 0; // the state of the switch

void setup() {
pinMode(switch1, INPUT); // set the switch pin to be an input
pinMode(switch2, INPUT); // set the switch pin to be an input
pinMode(switch3, INPUT); // set the switch pin to be an input
pinMode(ledPin, OUTPUT); // set the yellow LED pin to be an output
}

void loop() {
// read the switch input:
switchState1 = digitalRead(switch1);
switchState2 = digitalRead(switch2);
switchState3 = digitalRead(switch3);

if (switchState1 == 1 && switchState2 == 1 && switchState3 == 1) {
// if the switch is closed:
digitalWrite(ledPin, HIGH); // turn on the yellow LED

}
else {
// if the switch is open:
digitalWrite(ledPin, LOW); // turn off the yellow LED

}
}





No comments: