The first lab assignment can be found
here.
Basically, I was asked to connect a digital input circuit and digital output circuit to a microcontroller. In this case, the digital input is a switch. The digital output: two LEDs. Closing the switch lights one LED, opening it lights the other. Here's an open switch:
The white wires are the switch. Touching them together gives you this:
Pretty simple. Super fun. The next part of the assignment was to get creative with the actual switch as well as the digital input, with the goal of making a combination lock.
I found two items on the ITP junk shelf that I decided to use as the basis for my switch. First was an LED connected to two pushpins:
The other was this cable, which likely has a name, and can generally be found connecting stuff on your motherboard or something green and circuity:
The wire allowed me to make a switch that was fancy looking, but actually really simple. I created five switches and wired them from the bottom of this white cable (after using a multimeter to make sure each end point did in fact connect somewhere on the opposite end) to the ground on my breadboard. I then soldered a pushpin to a wire and connected that to my power source. When I insert the pin in one end of the white cable, it closes that particular switch. So, in effect I'm using one roving power source for five different switches. This solves the problem of people closing two switches at the same time (the equivalent of pressing two buttons) and also looks cool.
Now I wanted to make a combination. That's all programming. I decided that if you closed switches 1, 4 and 5, in that order, a green light would turn on. After each of the first two correct guesses, a green light would brink to let you know you were on the right path. If you messed up a long the way, a red light would flicker. After a couple of seconds, the sequence would restart so you could try again.
I got super stuck until I copied some code from
Zoe Fraade. And then I got super stuck until
Craig Kapp helped me figure it out. He sat down next to me and generated most of this code, explaining point by point why the way I'd done things wasn't the most simple way to get from point a to point b. Thanks, Craig. I'll figure this stuff out eventually.
Pin in switch one (the green light flashes briefly).P
in in switch four (the green light flashes briefly).Pin in switch five (the green light stays on!).I also wanted to include a red light if you put the pin in the wrong spot. Here, the red light goes off if you put the pin in the first switch after having put it in the first and fourth. More simply: if you think switch one is the final key instead of switch five:
One problem: I tried to figure out the code to make a red light flash if you put the pin into switch two or three at any time. But I couldn't figure it out before class. In the code below, nothing at all happens if you put the pin into switch two or three:
Pin in switch one, after being in switch four.
There are a couple of other problems as well. The reset mechanism is pretty untidy, for example. And there's no good way of telling the user how many switches are in the password. (That's not a problem I diagnosed myself; it came from class discussion of other projects).
Anyway, here's my code. I'll try to revisit it when I learn a little more about variables and loops and subroutines and other things that are, right now, totally over my head.
// declare variables:
int switchPin1 = 1; // digital input pin for switch 1
int switchPin2 = 2; // digital input pin for switch 1
int switchPin3 = 3; // digital input pin for switch 1
int switchPin4 = 4; // digital input pin for switch 1
int switchPin5 = 5; // digital input pin for switch 1
int greenLEDpin = 8; // digital output pin for an LED
int redLEDpin = 9; // digital output pin for an LED
int switchState1 = 0; // the state of the switch1
int switchState2 = 0;
int switchState3 = 0;
int switchState4 = 0;
int switchState5 = 0;
int myprogress = 0; //this counts the progress you've made in getting the right combo
int switchon = 1; //this is an idea that craig had, which he forgot to explain or incorporate later. ask him later.
int conditionsFilled = 0; //Keeps track of how many conditions so far have been filled
void setup() {
pinMode(switchPin1, INPUT); // set the switch pin to be an input
pinMode(switchPin2, INPUT); // set the switch pin to be an input
pinMode(switchPin3, INPUT); // set the switch pin to be an input
pinMode(switchPin4, INPUT); // set the switch pin to be an input
pinMode(switchPin5, INPUT); // set the switch pin to be an input
pinMode(greenLEDpin, OUTPUT); // set the yellow LED pin to be an output
pinMode(redLEDpin, OUTPUT); // set the red LED pin to be an output
}
void loop() {
// read the switch inputs:
switchState1=digitalRead(switchPin1); //Sets the inputs to variables that won't take so long to type
switchState2=digitalRead(switchPin2);
switchState3=digitalRead(switchPin3);
switchState4=digitalRead(switchPin4);
switchState5=digitalRead(switchPin5);
//beginning mode
// condition 1
if (switchState1 == 1 && myprogress == 0) //if first switch is on, and nothing else has happened...
{
myprogress = 1; //switch myprogress to one
digitalWrite(greenLEDpin, 1); //blink the green light
delay(1000);
digitalWrite(greenLEDpin, 0);
}
else if (switchState1 == 1 && myprogress != 0) //if switch 1 is on but you've already started down the path
{
myprogress = 0;
digitalWrite (redLEDpin, 1);
delay (1000);
digitalWrite (redLEDpin, 0);
}
// condition 2
if (switchState4 == 1 && myprogress == 1)
{
myprogress = 2;
digitalWrite(greenLEDpin, 1);
delay(1000);
digitalWrite(greenLEDpin, 0);
}
else if (switchState4 == 1 && myprogress != 1)
{
myprogress = 0;
digitalWrite (redLEDpin, 1);
delay (1000);
digitalWrite (redLEDpin, 0);
}
// condition 3
if (switchState5 == 1 && myprogress == 2)
{
myprogress = 3;
digitalWrite(greenLEDpin, 1);
delay(1000);
digitalWrite(greenLEDpin, 0);
digitalWrite(redLEDpin, 0);
}
else if (switchState5 == 1 && myprogress != 2)
{
myprogress = 0;
digitalWrite (redLEDpin, 1);
delay (1000);
digitalWrite (redLEDpin, 0);
}
// goal!
if (myprogress == 3)
{
digitalWrite(greenLEDpin, 1);
delay (5000);
digitalWrite(greenLEDpin, 0);
myprogress = 0;
}
// else {
// digitalWrite(redLEDpin, 1);
// delay(100);
//digitalWrite(redLEDpin, 0);
//delay(100);
//digitalWrite(redLEDpin, 1);
// myprogress == 0;
// }
/*
if (switchState2 == 1 || switchState3 == 1)
{
conditionsFilled = 0; //These aren't part of the password, so they're always wrong. Try again, McGyver.
digitalWrite (redLEDpin, 1); //just so they feel really bad about it
delay (200);
digitalWrite (redLEDpin, 0);
delay (200);
digitalWrite (redLEDpin, 1);
delay (200);
digitalWrite (redLEDpin, 0);
delay (200);
digitalWrite (redLEDpin, 1); //so the red light stays on.
}
*/
}