Arduino Blink LED with POT.JPG

Code

  1. /*
  2. Blink LED with POT
  3. Demonstrates analog input by reading an analog sensor on analog pin 0 and
  4. turning on and off a light emitting diode(LED) connected to digital pin 13.
  5. The amount of time the LED will be on and off depends on
  6. the value obtained by analogRead().
  7.  
  8. The circuit:
  9. * Potentiometer attached to analog input 0
  10. * center pin of the potentiometer to the analog pin
  11. * one side pin (either one) to ground
  12. * the other side pin to +5V
  13. * LED anode (long leg) attached to digital output 13
  14. * LED cathode (short leg) attached to ground
  15.  
  16. * Note: because most Arduinos have a built-in LED attached
  17. to pin 13 on the board, the LED is optional.
  18.  
  19. */
  20. int sensorPin = A0; // select the input pin for the potentiometer
  21. int ledPin = 13; // select the pin for the LED
  22. int sensorValue = 0; // variable to store the value coming from the sensor
  23. void setup() {
  24. // declare the ledPin as an OUTPUT:
  25. pinMode(ledPin, OUTPUT);
  26. }
  27. void loop() {
  28. // read the value from the sensor:
  29. sensorValue = analogRead(sensorPin);
  30. // turn the ledPin on
  31. digitalWrite(ledPin, HIGH);
  32. // stop the program for <sensorValue> milliseconds:
  33. delay(sensorValue);
  34. // turn the ledPin off:
  35. digitalWrite(ledPin, LOW);
  36. // stop the program for for <sensorValue> milliseconds:
  37. delay(sensorValue);
  38. }