Servo Potentiometer Code


#include <Servo.h> //this includes the library of servo information

Servo servo1;
const int SERVO = 9; //the signal cable is in port 9
const int POT = 0; //the potentiometer is in analog port 0


double valueRead = 0;
double valueWrite = 90; //sets the default signal value to 90,
                                      //when the servo is still. 
void setup() 

  Serial.begin(9600);   //begins the serial monitor so the programmer can
                                   //see the valueRead values
  servo1.attach(SERVO); //attaches the servo to pin 9
  servo1.write(valueWrite);  // set servo to no speed
  


void loop() {
  valueRead = analogRead(POT); //reads the rotation of the potentiometer
  
  valueWrite = valueRead*0.17578125; //proportionally decreased the value of the potentiometer
                                                              //to the range for the servo
  Serial.println(valueWrite);
  servo1.write(valueWrite);          //sets the servo to the new value
  delay(5);                    //waits 5 milliseconds

No comments:

Post a Comment