macca-nz

Closed loop RC Servo for position feedback

Apr 2nd, 2026
101
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.96 KB | Source Code | 0 0
  1. //Closed Loop Servo so we know position from servo feedback
  2. //IE: Add wire from wiper pin on servo position POT and connect to an analog input
  3. //Watch video of this running here <https://youtu.be/_5w9T6HmmPs>
  4.  
  5. #include      <VarSpeedServo.h>                             //AVR Servo library that allows us to slow travel speed
  6. #include      <OneButton.h>                                 //For short, long and double button press options
  7. const uint8_t closedLed = 13, midLed = 12, openLed = 11,
  8.               movingLed = 10, servoPin = 3;
  9. OneButton     button(9, true);
  10. VarSpeedServo myservo;                                      // create servo object to control a servo
  11. char          inChar;
  12. String        readString;
  13. uint16_t      closedState=0, midState=HIGH, openState=0, movingState=0,
  14.               pos=90, lastPos=90, val;
  15. bool          mid = true, opened = false, closed = false, moving = false,
  16.               updateState = true, inputs = false;
  17. long          previousTime = 0;
  18.  
  19. void setup() {
  20.   Serial.begin(115200);
  21.   pinMode(closedLed, OUTPUT);
  22.   pinMode(midLed, OUTPUT);
  23.   pinMode(openLed, OUTPUT);
  24.   pinMode(movingLed, OUTPUT);
  25.   digitalWrite(closedLed, closedState);
  26.   digitalWrite(midLed, midState);
  27.   digitalWrite(openLed, openState);
  28.   digitalWrite(movingLed, movingState);
  29.   myservo.attach(servoPin);
  30.   myservo.write(pos, 10);
  31.   button.attachDoubleClick(doubleclick);
  32.   button.attachClick(singleclick);
  33.   button.attachLongPressStop(longclick);
  34.   Serial.println("Startup Position = MIDWAY");
  35.   delay(100);
  36. }
  37.  
  38. //Button callbacks
  39. void doubleclick() {
  40.   if(pos != 90){
  41.     pos = 90;
  42.     moving = true;
  43.   }else{
  44.     Serial.print("Already at MIDWAY position: ");
  45.   }
  46. }
  47. void singleclick(){
  48.   if(pos != 180){
  49.     pos = 180;
  50.     moving = true;
  51.   }else{
  52.     Serial.print("Already at OPENED position: ");
  53.   }
  54. }
  55. void longclick(){
  56.   if(pos != 0){
  57.     pos = 0;
  58.     moving = true;
  59.   }else{
  60.     Serial.print("Already at CLOSED position: ");
  61.   }
  62. }
  63.  
  64. //Main loop
  65. void loop() {
  66.   if(inputs){                       //Flag so you can only press the button when servo stationary
  67.     button.tick();
  68.     while (Serial.available()) {
  69.       inChar = Serial.read();  //gets one byte from serial buffer
  70.       readString += inChar; //makes the String readString
  71.       delay(2);  //slow looping to allow buffer to fill with next character
  72.     }
  73.     if (readString.length() > 0) {
  74.         //Serial.print("Go to: ");
  75.         //Serial.println(readString);  //so you can see the captured String
  76.         pos = readString.toInt();  //convert readString into a number
  77.         readString="";
  78.             if(pos < 0 || pos > 180){
  79.                 Serial.println("Invalid Value, Please try again!");
  80.             }else if(lastPos == pos){
  81.                 Serial.print("Already at the position: ");Serial.println(pos);
  82.             }else{
  83.                 moving = true;        
  84.             }
  85.     }
  86.   }
  87.  
  88.   if(millis() - previousTime > 50){
  89.     previousTime = millis();
  90.     val = analogRead(A0);
  91.     int closedVal = constrain(val, 0, 100);         //Do your own tests to get what values your servo gives you
  92.     int midVal = constrain(val, 250, 270);
  93.     int openedVal = constrain(val, 540, 700);
  94.  
  95.     if(moving){
  96.         if(pos == 0){
  97.             Serial.println("Go to CLOSED position");
  98.             closed = true;      
  99.         }else if(pos == 90){
  100.             Serial.println("Go to MIDWAY position");
  101.             mid = true;
  102.         }else if(pos == 180){
  103.             Serial.println("Go to OPENED position");
  104.             opened = true;
  105.         }
  106.         movingState = HIGH;
  107.         closedState = LOW;
  108.         midState = LOW;
  109.         openState = LOW;
  110.         updateState = true;
  111.         moving = false;
  112.     }
  113.     if(closed){  
  114.         if(val == closedVal){
  115.             movingState = LOW;
  116.             closedState = HIGH;
  117.             midState = LOW;
  118.             openState = LOW;
  119.             updateState = true;
  120.             Serial.println("Position now CLOSED\n");
  121.             lastPos = pos;
  122.             inputs = true;
  123.             closed = false;
  124.         }
  125.     }
  126.     if(mid){
  127.         if(val == midVal){
  128.             movingState = LOW;
  129.             closedState = LOW;
  130.             midState = HIGH;
  131.             openState = LOW;
  132.             updateState = true;
  133.             Serial.println("Position now MIDWAY\n");
  134.             lastPos = pos;
  135.             inputs = true;
  136.             mid = false;
  137.         }
  138.     }
  139.     if(opened){
  140.         if(val == openedVal){              
  141.             movingState = LOW;
  142.             closedState = LOW;
  143.             midState = LOW;
  144.             openState = HIGH;
  145.             updateState = true;
  146.             Serial.println("Position now OPENED\n");
  147.             lastPos = pos;
  148.             inputs = true;
  149.             opened = false;
  150.         }
  151.     }
  152.     if(updateState){
  153.         myservo.write(pos, 20);
  154.         digitalWrite(closedLed, closedState);
  155.         digitalWrite(midLed, midState);
  156.         digitalWrite(openLed, openState);
  157.         digitalWrite(movingLed, movingState);
  158.         updateState = false;
  159.       }
  160.   }
  161. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment