Loving it! (stupid pet trick 1.0)

Cigar box with copper leads. The metal ball completes the circuit and then rolls to the next set of leads.

Cigar box with copper leads. The metal ball completes the circuit and then rolls to the next set of leads.

I am in rejoice mode because I got the code for the cigar box combo lock working today.  Over the last few weeks I’d received help on the code from several different people, each with slightly different approaches. After a while it all started to blend together in a giant soup of ints, ifs, states and errors. While I love Arduino, I wish the programming environment looked different than the Processing environment. Or vice versa.

At the bottom, I’ve copied the code that finally worked for the basic function of the combo lock remembering the state of the previous step, and then activating something once the steps are executed correctly.

Shahar Zaks tipped the scales when he explained that the int I’m using for the counter (switchState) had to be defined as a global variable at the top. I had done that before, but somehow got mixed up in different methods of coding for counters and lost it. He broke down the sequence for me in Arduino (rather than by talking or writing on paper), which helped because we could run the sequence right then to see if it worked.

The cigar box is a tilt sensing combination lock. Each corner has a pair of disconnected copper leads connected to the Arduino through the bread board. When a steel ball rolls from corner to corner in the correct combination the counter increases. When the counter reaches 4, the Arduino sends power to the belt motor, initiating the zoetrope.

I’m planning to make some kind of housing for it at the AMS, with the cigar box mounted on a flexible arm, and the belt motor system contained in a box somehow.

This is a video of the cigar box combo lock working. I hooked up the belt motor system (with zoetrope on top) to my bread board so it gets turned on when the right sequence is executed in the cigar box.

I will make a system of four LEDs that light up every time a step of the combo lock is entered correctly. And the belt motor mechanism will get the add-ons I wrote about before. One add-on will make it a music box, one will make it into a mini-light show, and one will be a micro-camera that will send the “view” of the zoetrope to a screen.  Framerate!

—- —- —- —- — — — – - -  – - -   -    -

// define terms needed
// then set up
// then loop

// set pin numbers:
int ledPin1 = 2;      // the number of the RED LED pin
int ledPin2 = 3;      // the number of the GREEN LED pin
int switchPin1 = 4;     // Top right switch
int switchPin2 = 5;     // bottom right switch
int switchPin3 = 6;     // bottom left switch
int switchPin4 = 7;     // top left switch
int switchCount = 0;  // boolean for limiting switch count

void setup(){
// for debugging
Serial.begin(9600);

//initialize LED pins as output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);

// initialize the switches as input:
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
pinMode(switchPin4, INPUT);
}

void loop(){

//read the state of the pushbutton value
int state1 = digitalRead(switchPin1);
int state2 = digitalRead(switchPin2);
int state3 = digitalRead(switchPin3);
int state4 = digitalRead(switchPin4);

if (state1 == HIGH && switchCount == 0) {
Serial.println(“You got the first one”);
switchCount = 1;

}
if(state2 == HIGH && switchCount == 1){
Serial.println(“that’s the second one”);
switchCount = 2;
}
if(state3 == HIGH && switchCount == 2){
Serial.println(“the third one”);
switchCount = 3;
}
if(state4 == HIGH && switchCount == 3){
Serial.println(“and the fourth”);
Serial.println(“Watch the animation”);
switchCount = 4;
}

if(switchCount == 4){
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
}
}

Tags: , , , ,

Comments are closed.