This week I prototyped the most basic functionality of my project:
when the photocell is covered, servo motor stops. When photocell is uncovered, servo motor sweeps 180ยบ.
Over the next week I will iterate on this by both multiplying instances of the functionality and giving shape to the story (adding the hammer to the motor, embedding photocell in foam core poster).
I used a few web resources for this code, which are listed at the top of my .ino sketch for now.
Schematic courtesy of Debbie Ding: http://openurbanism.blogspot.com/2013/08/simple-light-meter-arduino-servo.html
[code]
// code from:
//http://forum.arduino.cc/index.php?topic=149576.0
//https://www.arduino.cc/en/Tutorial/Sweep
// https://www.arduino.cc/en/Reference/ServoWrite
#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 analogValue = 0; // the value returned from the analog sensor
int servoPin = 9; // Control pin for servo motor. As of Arduino 0017, can be any pin
int pos = 0;
void setup() {
servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object
}
void loop()
{
analogValue = analogRead(analogPin); // read the analog input (value between 0 and 1023)
if(analogValue < 400) {
servoMotor.write(90); // Spin servo at max speed clockwise
} else {
servoMotor.write(0); // Stop the servo
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servoMotor.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servoMotor.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}
}
delay(15); // waits for the servo to get there
}
[/code]