Skip to content

Instantly share code, notes, and snippets.

@kmicheli
Created April 12, 2018 04:23
Show Gist options
  • Save kmicheli/e3862a860ce5f24083246f1166971b9c to your computer and use it in GitHub Desktop.
Save kmicheli/e3862a860ce5f24083246f1166971b9c to your computer and use it in GitHub Desktop.
#include <Servo.h>
//creates an instance of the servo object to control a servo
Servo myServo;
void setup() {
//initialize serial communications
Serial.begin(9600);
//attaches the servo on pin 2 to the servo object
myServo.attach(3);
}
void loop()
{
//read the analog input
int sensorVal = analogRead(A0);
//print it
Serial.println(sensorVal);
//new int called angle
//sensor range is 0 to 1023 since it is a potentiometer
int angle = map(sensorVal, 0, 1023, 0, 180);
//move the servo using the angle from the sensor:
myServo.write(angle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment