My goal with this assignment was to get something mechanical working. The result was so amusing that I posted a video here, (downloadable and playable by mplayer on a mac).
The Hardware
- I built a cabinet out of foamcore. It is very hard to cut foamcore in a straight line. The bottom was hollow; I used mounting tape to stick the Arduino and breadboard onto it.
- I ran 3 leds up the back of the cabinet, one to each shelf.
- A piezo buzzer from RadioShack was mounted onto the bottom with tape.
- A hobby-motor circuit was constructed on the breadboard, using the tutorial here. Two AA batteries served as the battery source.
- A pair of IR emitter/detector LEDs were obtained from RadioShack, added to the breadboard, then poked up through the bottom shelf. The emitter is on a simple power-resister-emitter-ground circuit; it gives off IR. I used a camera to see that it was glowing. The detector connects to an analog pin; I tested what the pin’s value was when the detector was covered vs. open, and found a threshold of 400 worked well.
- The wiring:
What It Does
An object is placed over the detector. The doors to the cabinet are open and the LEDs cycle pleasantly. But! If one steals the object, the LEDs flash warningly and a buzzer goes off. If the object is returned in time (i.e. the detector’s value goes back below the threshold), everything returns to normal. If not, the doors close and the buzzer cycles unendingly. One images that an Indiana Jones attack would be most effective…
The states machine appears in the following diagram:
The Software
/* chest of drawers */ /* 0 sleep - LEDs blinking (sleep -> arousal via IR) 1 arousal - LEDSs flash (arousal -> sleep via IR returns, arousal -> attract via IR remains) 2 attract - motor goes (attract -> reward via time) 3 reward - buzzer (sink) */ int led1 = 4; int led2 = 2; int led3 = 3; int motor = 9; int buzzer = 12; int detector = 0; int detector_threshold = 400; int state = 0; int stateCounter = 0; void setup() { pinMode(led1, OUTPUT); // declare the ledPin as as OUTPUT pinMode(led2, OUTPUT); // declare the ledPin as as OUTPUT pinMode(led3, OUTPUT); // declare the ledPin as as OUTPUT pinMode(buzzer, OUTPUT); } void loop() { int detector_value = analogRead(detector); stateCounter++; if (state == 3) { // do nothing } else if (state == 2) { state = 3; } else if (detector_value > detector_threshold) { if (state == 0) { state = 1; stateCounter = 0; } else if (state == 1 && stateCounter > 10) { state = 2; stateCounter = 0; } } else if (detector_value < detector_threshold) { if (state == 1) { state = 0; stateCounter = 0; digitalWrite(buzzer, 0); digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); } } if (state == 0) { int s = stateCounter % 6; if (s==0) digitalWrite(led1, HIGH); if (s==1) digitalWrite(led2, HIGH); if (s==2) digitalWrite(led3, HIGH); if (s==3) digitalWrite(led1, LOW); if (s==4) digitalWrite(led2, LOW); if (s==5) digitalWrite(led3, LOW); delay(500); } if (state == 1) { int s = stateCounter % 2; if (s==0) { digitalWrite(led1, HIGH); digitalWrite(led2, HIGH); digitalWrite(led3, HIGH); digitalWrite(buzzer, HIGH); } else if (s==1) { digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(buzzer, LOW); } delay(300); } if (state == 2) { analogWrite(motor, 255); delay(6000); analogWrite(motor, 0); } if (state == 3) { digitalWrite(buzzer, HIGH); delay(1000); digitalWrite(buzzer, LOW); delay(4000); } }