Hi all,
I've got a servo connected up to an arduino which is being used to power the servo. I'm controlling this via a gui designed in PyCharm/python which talks to the arduino via serial comm port (PySerial). The idea is that if the arduino receives an 'o' character through the serial port, it moves the servo 90 degrees, and if it receives 'c' through the serial port, it rotates it back the way to the original position. It's working fine but I'm getting this jitter/twitching from the servo and I'm asking how to get rid of it. I've read some posts which diagnose a lack of power as being the issue, I'm not convinced firstly as I uploaded a near empty script to the arduino and it stopped twitching, secondly, I've seen instances of it being done before without jitter. - or at least I thought I'd convinced myself of that. I've attached my arduino sketch below and any help to figure out why it's jittering would be greatly appreciated - thanks in advance!
#include <Servo.h>
Servo myservo;
byte serialByte;
void setup() {
myservo.attach(8);
myservo.write(0);
Serial.begin(115200);
Serial.println("<Arduino is ready>");
myservo.write(0);
}
void loop() {
if(Serial.available()>0){
serialByte = Serial.read();}
if(serialByte == 'o'){
myservo.write(90);
Serial.println("Servo set to 90");
if(Serial.available()>0){
serialByte = Serial.read();}
if (serialByte == 'c') {
myservo.write(0);
Serial.println("Servo set to 0");
}
}
}
I've also attached my pycharm code for the variables that send the 'o' and 'c' characters respectively. It's also worth noting that if I change time.sleep(2) to time.sleep(1) the servo stops moving and I don't quite understand why...
def Signalo(self):
import serial
import time
arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=.1)
arduino.isOpen()
commando = str('o')
print(commando)
time.sleep(2)
arduino.write(commando.encode())
print("turning...")
arduino.close()
def Signalc(self):
import serial
import time
arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=.1)
arduino.isOpen()
commandc = str('c')
print(commandc)
arduino.write(commandc.encode()) #encodes string to bytes before writing to serial
print("turning back...")
arduino.close()