/* ----- A demonstration of using the analogWrite() function ------
* which sends a PWM (Pulse-Width_Modulation) signal
* (Only works on digital pins that do PWM: 3,5,6,9,10 or 11)
*
* setup: - Wire an LED to pin 6 through a resistor to ground
* - Create a voltage divider (or pot), send output to analog pin 5
(Pot: 1 outer leg to +, center pin to chip pin 28, other outer leg to Gnd)
* -----------------------------------------------------------------*/
int ledPin = 6; // Digital pin 6 (physical pin 12) used to light an LED
int analogPin = A5; // potentiometer connected to analog pin 2
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
val = map(val, 0, 1023, 0, 255); // scale data to from 0-1023 to 0-255
analogWrite(ledPin, val); // PWM analog value to LEDpin
}
|