« Observation Assignment 1 | Main | Lab 2 - Analog Inputs (part 2) »

Lab 2 - Analog Inputs (part 1)

LAB 3 - Pulse Width Modulation

In addition to the adjustable LED level suggested in the lab notes, I decided to program the Arduino board to gradually fade the LED level down from the level set by the potentiometer. Last week when I was working on my game of catch I wanted to have the first LED gently fade in and out during the "wind-up" phase of the throw. This week, I figured out how to do it.
/* PWM Lab
   I've added my own twist to the code to make the the LED fade
    
   Michael Chladil
*/

#define ANALOG_INPUT  0
#define PWM_OUTPUT    9
#define DELAY_TIME    10

int  potValue        = 0;
long lastMillis      = 0;
int  currentLEDLevel = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  potValue = analogRead (ANALOG_INPUT);
  
  Serial.println (potValue); 
  /* Use the value read from the potentiometer to determine 
     a starting level for the LED.                          */
  
  for (currentLEDLevel = potValue; currentLEDLevel >= 0; currentLEDLevel--)
  {
    analogWrite(PWM_OUTPUT, currentLEDLevel);
    delay (DELAY_TIME);
  }
  
  delay (DELAY_TIME);  
}

TrackBack

TrackBack URL for this entry:
http://itp.nyu.edu/~mjc497/cgi-bin/mt/mt-tb.cgi/13

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)