/******************************** name: Potentiometer and LED (project a) function: Potentiometer provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED turns on and off. By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. ********************************/ //Email:info@acoptex.com //Website:www.acoptex.com /********************************/ // include libraries // no libraries needed for this project /********************************/ #define ledPin 8 // select the pin for the LED #define potPin A1 // select the input pin (must be analog) for the potentiometer /********************************/ int potValue = 0; // value read from the potentiometer int finalValue = 0;// value output to the PWM (analog out) /********************************/ void setup() { Serial.begin(9600); // initialize serial communications at 9600 bps pinMode(ledPin, OUTPUT);// declare the ledPin as an OUTPUT pinMode(potPin, INPUT);// declare the potPin as an INPUT } /********************************/ void loop() { potValue = analogRead(potPin);// read the value from the potentiometer // it will be a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin finalValue = map(potValue, 0, 1023, 0, 255);// re-maps a number from one range to another: from 0 and 1023 to 0 and 255 analogWrite(ledPin, finalValue); // give result to the LED // print the results to the serial monitor: Serial.print("sensor = "); Serial.print(potValue); Serial.print("\t output = "); Serial.println(finalValue); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }