15 – Analogue Dial

15 Analogue dial

 

/*
 Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED) connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */
#include <Servo.h>    
Servo myservo;
int pos = 0; 
int lightPin = 0;
     
void setup() {
        
myservo.attach(9); // servo to pin 9
}
     
void loop() { // start a loop
// next line reads the voltage 
int lightLevel = analogRead(lightPin); 

// this analog pin has 1024 values
lightLevel = map(lightLevel, 0, 1023, 0, 359);
pos = constrain(lightLevel, 0, 359);
       
myservo.write(pos); // sets the servo angle
delay(100);
}

 

Advertisements