Skip to content

Instantly share code, notes, and snippets.

@natendaben
Created November 16, 2018 00:01
Show Gist options
  • Save natendaben/49e1da58e5809ae97cb08823a9ae989c to your computer and use it in GitHub Desktop.
Save natendaben/49e1da58e5809ae97cb08823a9ae989c to your computer and use it in GitHub Desktop.
Servo Code
#include <Servo.h>
//make instance of servo object
Servo mike;
void setup() {
// put your setup code here, to run once:
//attach servo motor
//works with any digital pin
mike.attach(9);
}
void loop() {
int sensor = analogRead(A0);
//servo.Write() takes values between 0-180
int angle = map(sensor, 0, 1023, 0, 180);
// put your main code here, to run repeatedly:
mike.write(angle);
//to fix jitter, add a delay to write to servo less frequently
//delay(20);
//millis() counts milliseconds since program started
if(millis()%20<2)
{
//functionally the same as delay(20) but doesn't delay the rest of your code
mike.write(angle); //takes value between 0-180
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment