I am trying to interface my Raspberry pi, an UNO and a servo. The Pi calculates to needed pulse width from a PID controller, sends that calculated number to the UNO via serial and makes some plots. Almost got everything working but for some reason at the end of every loop the servo indexes back to zero. To try and make life simple I went back to the uno (with the Pi script not running) and put some servo command in a loop. I was expected to see the servo index back and forth repeatedly which it did but after the last move it went back to zero. A few posts on the forums said this could happen if the serial monitor settings are not configured correctly but I am not using the serial monitor so I'm a bit confused why its doing this. Does anyone have any ideas? below is the code I was using
void setup() {
//put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(5); // attaches the servo on pin 9 to the servo object
Serial.println("Reattach");
}
void loop() {
recvChar();
myservo.writeMicroseconds(1000);
delay(1000);
myservo.writeMicroseconds(1500);
delay(1000);
myservo.writeMicroseconds(1000);
delay(1000);
myservo.writeMicroseconds(1500);
if (newData == true){
pos = receivedChar.toInt();
//Serial.println(pos);
myservo.writeMicroseconds(pos);
newData = false;
}
}
void recvChar() {
if (Serial.available()>=8) {
receivedChar = "";
receivedChar_1 = Serial.read();
receivedChar.concat(receivedChar_1);
receivedChar_2 = Serial.read();
receivedChar.concat(receivedChar_2);
receivedChar_3 = Serial.read();
receivedChar.concat(receivedChar_3);
receivedChar_4 = Serial.read();
receivedChar.concat(receivedChar_4);
//Serial.println(receivedChar);
newData = true;
}
delay(200);
}
The whole thing is there, to see it all you have to scroll down. One thing I just noticed if the servo index's from 1000ms to 1500ms to 1200ms it does not reset. Is there something weird about the servo trying to reset itself if it is around 1500ms? I would think that would ne kind of weird but this is the first time I have messed with servo's so maybe I'm way off
Arduino's website has a good example of a servo where they suggested using a 100uF capacitor between the power and ground on the power supply going to the servo. There was a few posts about the power being supplied to the servo being the issue. I'm using a 5v supply separate from the Arduino but at this point do not have a capacitor going from power to ground.
#include <Servo.h>
boolean newData = false;
String receivedChar = "";
char receivedChar_1 = {};
char receivedChar_2 = {};
char receivedChar_3 = {};
char receivedChar_4 = {};
int pos = 1000;
Servo myservo; // create servo object to control a servo
void setup() {
//put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.println("Reattach");
}
void loop() {
recvChar();
Serial.println(myservo.attached());
myservo.writeMicroseconds(1000);
delay(1000);
myservo.writeMicroseconds(1500);
delay(1000);
myservo.writeMicroseconds(1200);
delay(1000);
if (newData == true){
pos = receivedChar.toInt();
//Serial.println(pos);
myservo.writeMicroseconds(pos);
newData = false;
}
}
void recvChar() {
if (Serial.available()>=8) {
receivedChar = "";
receivedChar_1 = Serial.read();
receivedChar.concat(receivedChar_1);
receivedChar_2 = Serial.read();
receivedChar.concat(receivedChar_2);
receivedChar_3 = Serial.read();
receivedChar.concat(receivedChar_3);
receivedChar_4 = Serial.read();
receivedChar.concat(receivedChar_4);
//Serial.println(receivedChar);
newData = true;
}
delay(200);
}
@dustin_s
I have yet to see a schematic. Perhaps a refresher is required, as you haven't posted in two years? In case,
Without a schematic, we're reduced to speculating about your exact wiring arrangement. Please, no fritzing diagrams, unless excessively simple, yet accurage.
So, do you have a solid ground between power supply, Arduino, and servos?
Let's look at this:
if we received something, convert a value, write it to Servo, clear flag,
and immediately go to top, where we repeat the get,print,move3x with wait.
First observation. There's no time for the last positioning to take effect, the servo may not even twitch before it's back into the first of the 3 move with wait actions; at least, you need a delay(1000) at the end of the conditional, to ensure you'll see that move to pos
Second observation. Why do the moves between the get and the check if received? It's Illogical to do so.
Now, about that get code:
Firstly, you check to see if there are 8 characters, then extract 4. all else aside, you're always behind by 4 characters. Why?
Secondly what, exactly, are you sending from Serial Monitor - do you type 1234 and hit enter? Then it matters what the Serial Monitor is configured as - does it send CR, CR/LF, or nothing when you hit ?
Thirdly, lose the delay(200). I won't even ask for your reason, it does not belong.
I can attach a picture of the wiring here in a bit, but yes the ground from the power supply is tied to the uno. To simplify things I stoped the script running in the rasp pi so there was nothing be entered in the serial monitor. I also tried using the four different configurations in the serial monitor and still had the same problem. With the script not running in the pi the if statement in the void loop is never true so that part of the script doesn’t run. For some reason if I index the servo back and forth 3 times (the third script posted) it works fine but if it it tries indexing back and forth 4 times (the first script) the servo returns to zero before starting the loop again. That’s a good point about not having the delay after the last servo.write command. If the servo can’t get to position in time could it cause it to go back to where it started from?
Made the suggested edits to my code and it seems to be working fine by keying in a number in the serial monitor. Its still indexing back to zero when the Pi sends a number over serial but if keying a number in via the serial monitor works I'm assuming its something with the Pi and not the UNO