How to move a servomotor back to the initial position?

Hello,
I am trying to create a program so that I can move a servomotor at a specific angle and after a delay, it turns back to its initial position.

Theoretically, if I write move the servo motor should move 60º and then come back (-60º)

This is what I have but I don't know why it is not working


#include <Servo.h>

Servo servo1; 

int i = 0;

String command;

void setup() {
  
  Serial.begin(9600);
  servo1.attach(2);

}

void loop() {

  if(Serial.available()){

      command = Serial.readStringUntil('\n');

      if(command.equals("move")){

         for (i = 0; i < 60; i++) { 
           servo1.write(i);                   
           delay(10);
         }
         for (i = 59; i > 0; i--) { 
            servo1.write(i); 
            delay(10);
         }

         command = 'o';
      }
    }      
  }
1 Like

How about if the String contains "move"?

What's that for?

1 Like

How does that look like? A big kaboom? Hope not. Any move at all, no move at all......?

1 Like

Add a Serial.print or two to see what you're actually getting in command and where the code gets to.

Just saying "not working" isn't very helpful. What exactly does it do? What servo are you using and how is it powered?

Steve

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.