Monday, January 31, 2011

LED Blink Speed / Analog Sketch

I'm doing a series of mini projects that will build up to a motor controller, but need to get some basics across first. This first sketch is a Arduino, reading a potentiometer on an analog port, and using that data to control the blink speed of a LED on a PWM port. The potentiometer (pot) connects to ground and +5 on the outside legs, and Analog 0 on the center leg. The LED connects + to pin 7 and - to a 270 ohm resistor connected to ground. The value of the pot determines the delay on the LED. This not true PWM, that's coming next.

(video uploaded to youtube)

int sensorPin = A0;    // Analog input pin to pot
int ledPin = 7;      // PWM pin to LED
int sensorValue = 0;  // variable to store pot value

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);                  
}

No comments:

Post a Comment