awinograd

EE 47 - Lab 3 - Analog Input Code

Apr 25th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. /*
  2.   Analog Input
  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.  Created by David Cuartielles
  21.  modified 30 Aug 2011
  22.  By Tom Igoe
  23.  
  24.  This example code is in the public domain.
  25.  
  26.  http://arduino.cc/en/Tutorial/AnalogInput
  27.  
  28.  */
  29.  
  30. int sensorPin = A0;    // select the input pin for the potentiometer
  31. int ledPin = 9;      // select the pin for the LED
  32.   // Pin 13: Arduino has an LED connected on pin 13
  33.   // Pin 11: Teensy 2.0 has the LED on pin 11
  34.   // Pin 6: Teensy++ 2.0 has the LED on pin 6
  35. int sensorValue = 0;  // variable to store the value coming from the sensor
  36.  
  37. void setup() {
  38.   // declare the ledPin as an OUTPUT:
  39.   pinMode(ledPin, OUTPUT);  
  40. }
  41.  
  42. void loop() {
  43.   // read the value from the sensor:
  44.   sensorValue = analogRead(sensorPin);    
  45.   // turn the ledPin on
  46.   digitalWrite(ledPin, HIGH);  
  47.   // stop the program for <sensorValue> milliseconds:
  48.   delay(sensorValue);          
  49.   // turn the ledPin off:        
  50.   digitalWrite(ledPin, LOW);  
  51.   // stop the program for for <sensorValue> milliseconds:
  52.   delay(sensorValue);                  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment