Skip to content

Instantly share code, notes, and snippets.

@lisajamhoury
Created September 30, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisajamhoury/8b06dff68e8993b6dec3 to your computer and use it in GitHub Desktop.
Save lisajamhoury/8b06dff68e8993b6dec3 to your computer and use it in GitHub Desktop.
/* I started with:
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
int lspin = A0; // analog pin used to connect the potentiometer
int val = 0; // variable to read the value from the analog pin
int ledPinRed = 4;
int ledPinYell = 5;
int ledPinGreen = 6;
void setup()
{
Serial.begin(9600);
myservo1.attach(3); // attaches the servo on pin 9 to the servo object
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinYell, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
}
void loop()
{
val = analogRead(lspin); // reads the value of the potentiometer (value between 0 and 1023)
int angle = map(val, 300, 800, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(angle); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
if (angle > 60) {
digitalWrite(ledPinGreen, HIGH);
}
else {
digitalWrite(ledPinGreen, LOW);
}
if (angle > 110) {
digitalWrite(ledPinYell, HIGH);
}
else {
digitalWrite(ledPinYell, LOW);
}
if (angle > 175) {
digitalWrite(ledPinRed, HIGH);
}
else {
digitalWrite(ledPinRed, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment