/******************************** name: Potentiometer and LED (project b) 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 blinks. 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 /********************************/ int potPin = A1; // select the input pin (must be analog) for the potentiometer int ledPin = 8; // select the pin for the LED int potValue = 0; // variable to store the value coming from the sensor 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 digitalWrite(ledPin, HIGH); // turn the ledPin on delay(finalValue); // stop the program for some time digitalWrite(ledPin, LOW); // turn the ledPin off delay(finalValue); // stop the program for some time Serial.print("\t output = "); Serial.println(finalValue); }