An interesting class project over at the University of Western Ontario — prototype a tangible interface for Google Earth.
March 2, 2008
assignment 6: servo and brightness
The angle of the servo changes as I turn the potentiometer. And when the value of the servo is at its highest, or when the angle of the servo is turned to its highest value of 93, the LED will gradually get bright.
pic of when servo val=93, and led gets brighter
And when the value of the servo is at its lowest, or when the angle of the servo is turned to its lowest value of 0, the LED will gradually get dim.
pic of when servo val=0, and led dims
here’s the circuit diagram: circuit diagram
here’s the code:
int servoPin = 3; // connect servo to pin 3
int sensorPin = 2; // connect potentiometer to pin 2
int brightness = 0;
int ledPin = 5; // connect led to pin 5
void setup ()
{
beginSerial (9600); //
pinMode (servoPin, OUTPUT); // set servo as analog output
pinMode (sensorPin, INPUT); // set potentiometer as analog input
pinMode (ledPin, OUTPUT); // set led as analog output
}
void loop ()
{
int val = analogRead(sensorPin) / 11; // set "val" equal to the analog read of the sensor divided by 11
Serial.println(val); // print the value of "val" on the serial monitor
analogWrite(servoPin, val); // as the value of the sensor changes, the servo should spin according to "val"
if (analogRead(servoPin == 0)) // if the analogRead of the servoPin is 0
{ for (brightness = 0; brightness > 255; brightness ++) // make LED get bright
analogWrite (ledPin, brightness);
delay (500); // delay for 0.5 seconds
}
if (analogRead(servoPin == 93))
{ for (brightness = 255; brightness > 0; brightness --) // make LED dim
analogWrite (ledPin, brightness);
delay (500); // delay for 0.5 seconds
}
}