Skip to content

Instantly share code, notes, and snippets.

@topnotcher
Forked from bowserthewizard/servo control
Last active August 29, 2015 14:08
Show Gist options
  • Save topnotcher/c64bfda05415813cf380 to your computer and use it in GitHub Desktop.
Save topnotcher/c64bfda05415813cf380 to your computer and use it in GitHub Desktop.
#include <Servo.h>
Servo myservo;
int duty_cycle=0;
void setup() //This function executes only once
{
// myservo.attach(3);
pinMode(3, OUTPUT);
}
void loop() // This function executes repeatedly
{
while (1) {
readinput();
delay(100);
servocontrol();
}
}
int readinput() // This function executes repeatedly
{
int addata = analogRead(0);
int a = .7;
int b = 2.4;
int freq = 40;
//now we want to take the input and map it to [a,b] for our pulse width
float scale = ((float)(b-a))/(1023-0);
return scale*addata*freq ;
}
void servocontrol()
{
int duty_cycle = readinput();
// myservo.write(pwm);
analogWrite(3, duty_cycle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment