Physical Computing Week 4: Analog Output & Servo Motor

Week 4 of Intro to Physical Computing @ ITP involved learning about analog output on the Arduino microprocessor and getting familiar with servo motors.

Physical Computing Week 4 – Evil Servobot from lee-sean on Vimeo.

I decided to take a riff on the lab assignment and attempt to create pure evil with limited resources (and skills for that matter).  I hooked up the servo to a photocell via the Arduino, which controls the movement of the servo based on how much light the photocell detects.  Then I added an LED that dims/brightens, also following the analog reading on the photocell.  And for the final step (and the one the really unleashed pure evil), I added a small speaker that modulates its audio output, also according to the photocell reading.  With all the wires going into the LED and servo acting as anchors, the Evil Servobot is able to dance and shake its demonic robot hips.

Here is the code:

int servoPin = 2;     // Control pin for servo motor
int buzzerPin = 5;
int ledPin = 11;  // Control pin for buzzer
int minPulse = 500;   // Minimum servo position
int maxPulse = 2500;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo
int brightness = 0;        //pitch of the

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the sensor’s on

void setup() {
pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
pinMode(buzzerPin, OUTPUT); //set buzzer pin as output
pinMode(ledPin, OUTPUT);  //set LED as output
pulse = minPulse;           // Set the motor position value to the minimum

void loop() {

analogValue = analogRead(analogPin);      // read the analog input
brightness = map(analogValue,0,800,0,255);
pulse = map(analogValue,0,800,minPulse,maxPulse);    // convert the analog value
// to a range between minPulse
// and maxPulse.
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() – lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH);   // Turn the motor on
delayMicroseconds(pulse);       // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW);    // Turn the motor off
lastPulse = millis();           // save the time of the last pulse