Servo moving when it is not supposed to

Hello, I am attempting to make a 360 degree continuous rotation servo only move when a certain phrase is inputed. And when that phrase is inputted into the serial monitor it only move for a certain period of time before stopping. However, whenever I run this code the servo keeps moving randomly in different directions in what seems like chaos. I am positive that it is nothing to do with my power supply, my power supply is fine, I have one servo hooked up to a 6 volt 1.2 amp battery. I am not sure what the error is and the code is below, please help, thanks!

code:

#include <Servo.h>

Servo servo1;
int servopin = 9;
String input;

void setup()
{
Serial.begin(9600);
servo1.attach(servopin);
servo1.write(90);
}
void loop()
{
servo1.write(90);
while(Serial.available()) {

input = Serial.readString();
if (Serial.readString() = “Butter my toast”)
{
Serial.print(“Sure, let me butter that toast”);
servo1.write(100);
delay(20); //time to butter the toast
while(1) { } // stop the program
}
else
{
Serial.print(“Radical”);
}

}

}
:confused:

Have you tried a simple sketch to do only the servo.write(90) to check that 90 is the correct "stop" value? I've never used a continuous servo but I believe that although 90 is nominally"stop", it can vary servo by servo by a few degrees, between say 88 and 92. I think (not sure) that some have a small adjust screw to calibrate the value to 90.

You do have the ground of the servo connected to the Arduino ground?

I have tried both, trying different values in the ranges you specific did not result in a noticeable difference, I however did not connect the servo ground to the Arduino ground, and this helped immensely. I still notice that the servo is moving very slowly when no text is inputted, and that the Serial Monitor is not outputting the else statement text.

The servo also does not stop after 20 milliseconds as specified in the "delay(20);"

To test equality in an if you need == not =. Your if is probably always evaluating to true for some arcane C reason. Once it's in there the while(1) means it will never get out.

if (Serial.readString() = "Butter my toast")

The compiler let that by?

Delta_G:

if (Serial.readString() = "Butter my toast")

The compiler let that by?

Sure. The Serial.readString() function returned a String. The assignment operator gave that String a new value.

Of course, the compiler is dumb a box of rocks, so it lets the programmer be dumb as a box of rocks, too. Syntactically valid is nowhere near the same thing as logically correct.