1 minute read
Software
208 Hacking Electronics
the Arduino. The LED/resistor combo is the same as we used in Chapter 6. The positive end goes to digital pin 8 on the Arduino and the negative end to GND. The buzzer fits between pins D3 and D6—D6 being connected to the positive end of the buzzer. If the pins on your buzzer are at a different spacing, then you can pick other pins, but remember to change the variables “gndPin2” and “buzzerPin” to whatever pins you end up using.
Both of the accelerometer modules will fit in the Arduino sockets A0 to A5, as shown in Figure 8-18. However, their pin allocations are quite different.
The project is powered from a 9V battery using an adapter, and the Arduino and battery are attached to the spoon with rubber bands.
Software
There are two versions of the sketch provided: “egg_and_spoon_adafruit” and “egg_and_spoon_freetronics”. Make sure you get the right one, and then program the Arduino with it BEFORE you attach the accelerometer.
Figure 8-17 The schematic diagram for the Arduino Egg and Spoon
Figure 8-18 The components attached to the Arduino
CHAPTER 8: Hacking with Sensors 209
The only difference between the two sketches is the pin allocations.
This is the sketch for the Adafruit version.
We start by defining the pins used.
// egg_and_spoon_adafruit int gndPin1 = A2; int gndPin2 = 3; int xPin = 5; int yPin = 4; int zPin = 3; int plusPin = A0; int ledPin = 8; int buzzerPin = 6;
The two variables “levelX” and “levelY” are used to measure the resting values of acceleration for X and Y if the spoon is level.
int levelX = 0; int levelY = 0;
The “ledThreshold” and “buzzerThreshold” can be adjusted to set the degree of wobble before the LED lights and the buzzer sounds to indicate a “dropped egg.”
int ledThreshold = 10; int buzzerThreshold = 40;
The “setup” function initializes the pins and then calls the function “calibrate” that sets the values of “levelX” and “levelY”.
void setup() { pinMode(gndPin1, OUTPUT); digitalWrite(gndPin1, LOW); pinMode(gndPin2, OUTPUT); digitalWrite(gndPin2, LOW); pinMode(plusPin, OUTPUT); pinMode(ledPin, OUTPUT); pinMode(buzzerPin, OUTPUT); digitalWrite(plusPin, HIGH); calibrate(); }