Hi, I'm trying to stop my continuous servo from rotating.
I've got a SM-S4315R servo hooked up to the arduino sensor shield 4.0.
I've got a usb cable and 9 volt battery hooked up to the arduino uno r3.
I know for continuous servos if I input a value of 90 it should stop.
I tried this on its own and it worked.
However, after I used the if and elseif statements the continuous servo keeps spinning even though my photosensor values are above requirements to make it stop rotating.
Is there something wrong with the way my if and elseif statements are structured?
Hi I'm checking the braces you mentioned but don't know what is wrong.
The only thing I can think of is changing the variable and just using the analog pin value in the code but I don't think that will do anything.
I am using the serial monitor to monitor my values.
If a place a dark black object over the photoresistor my values are in the low 10s and won't exceed 15.
If I leave it alone the values just spike up in the hundreds but the motor keeps spinning.
I've attached a picture of my setup.
I just turned my resistor the other way to determine if that is the cause of the problem.
Hi thanks it seems like in my photo I'm reading from pin 1 but I am reading from pin 0. Checked it. Based on other examples online the orientation of my resistor appears to be right.
I saw the analogread examples on the arduino site and have modified my code.
So the code works initially when my values are > 30
but after I cover the photosensor up and uncover again it continues to spin slowly.
#include <Servo.h>
Servo myservo; //continuous rotation servo SM-4315R
int val = 0;
void setup()
{
Serial.begin(9600); //Begin serial communcation
myservo.attach(9); // servo attached to pin 9
}
void loop()
{
Serial.println(val);
if (val = analogRead(0) > 30) // read values from analog pin 0 if > 30 servo motor will stop rotating
{
myservo.write(90);
}
else if (val = analogRead(0) < 30) // read values from analog pin 0 if < 30 servo motor will continue rotating
{
myservo.write(87);
}
}
Well I've simplified my code and got it to work. Thanks everybody for all the help!
It's weird sometimes it continues to spin even though the photoresistor is not covered.
What is even more weird is that when I removed power to the photoresistor and feedback to the analog pin it kept on spinning.
#include <Servo.h>
Servo myservo; //continuous rotation servo SM-4315R
// goal of code is to have a continuous servo rotate until a photoresistor sees light then it will stop rotating.
// for a continuous servo: 0 = full speed rotation in one direction, 180 = full speed in other direction, 90 = no movement
void setup()
{
Serial.begin(9600); //Begin serial communication
myservo.attach(9); // servo attached to pin 9
}
void loop()
{
Serial.println(analogRead(0));
if (analogRead(0) > 30) // read values from analog pin 0 if > 30 servo motor will stop rotating
{
myservo.write(90);
}
else // for all values less than 30 servo will move
{
myservo.write(87);
}
}
What is even more weird is that when I removed power to the photoresistor and feedback to the analog pin it kept on spinning.
Below is some servo test code you can use to find the servo's microsecond command position for stopping, which usually has better resolution than writing degrees. If the servo keeps drifting, you might use the servo attach/detach function.
// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a
// to decrease) and enter to change servo position
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;
void setup()
{
myservo.attach(7, 400, 2600); //servo control pin, and range if desired
Serial.begin(9600);
Serial.println("serial servo incremental test code");
Serial.println("type a character (s to increase or a to decrease)");
Serial.println("and enter to change servo position");
Serial.println("use strings like 90x or 1500x for new servo position");
Serial.println();
}
void loop()
{
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
if(readString.indexOf('x') >0) {
pos = readString.toInt();
}
if(readString =="a"){
(pos=pos-1); //use larger numbers for larger increments
if(pos<0) (pos=0); //prevent negative number
}
if (readString =="s"){
(pos=pos+1);
}
if(pos >= 400) //determine servo write method
{
Serial.println(pos);
myservo.writeMicroseconds(pos);
}
else
{
Serial.println(pos);
myservo.write(pos);
}
}
readString=""; //empty for next input
}
I found out what was the problem.
It was a hardware issue.
The code works great.
I had to fine tune the rest-point adjustment potentiometer on the continuous servo.
I'm lucky I bought two and the other motor worked fine for a while.
So I was able to narrow down the issue this way.
Now I'm going to fine tune both servo motors so that they don't spin while the photoresistor is uncovered.