Making Things Interactive

March 2, 2008

assignment 6: servo and brightness

Filed under: 6: More Motion,Assignments,Gee Kim — gskim @ 12:40 am

Pic of the circuit setup

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
   }
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: