Page 194 - Making things move_ DIY mechanisms for inventors, hobbyists, and artists
P. 194

172        Making Things Move





                 3. Connect the red wire to 5V power, the black wire to ground, and the yellow
                     wire to digital pin 2 on the Arduino. Sometimes this third wire is green or
                     orange, but it will always be different from the red (power) and black (ground)
                     wires.


                 4. Connect one leg of the photocell directly to power.

                 5. Connect the other leg to a row on the breadboard of your choice. Then
                     connect this row to ground through the resistor. Also connect this row to
                     analog pin 0 on the Arduino. Your circuit should now look like Figure 6-33.

                 NOTE     We need to use the analog pins with a photocell because it’s not
                 just an on-off type of input like the switches we’ve used until now. The
                 photocell will actually indicate a value between 0 and 1023 (as will any
                 analog sensor), depending on how much light is hitting it.

                 6. Open a new sketch in Arduino and type the following code. Then verify and
                     upload the code to the Arduino.

                     /*
                     Servo control from an analog input using the Arduino Servo library

                     Created June 2010
                     By Stina Marie Hasse Jorgensen and Dustyn Roberts
                     Adapted from code at http://itp.nyu.edu/physcomp/Labs/Servo
                     */
                     #include <Servo.h>       // include the servo library

                     Servo servoMotor;     // creates an instance of the servo object
                                           // to control a servo
                     int analogPin = 0;       // the analog pin that the sensor is on
                     int servoPin = 2;        // the digital pin for the yellow servo
                                              // motor wire
                     int analogValue = 0;     // the value returned from the photocell

                     void setup()
                     {
                     servoMotor.attach(servoPin);   // attaches the servo on Arduino pin
                                                    // 2 to the servo object
                     }
   189   190   191   192   193   194   195   196   197   198   199