2

I am using pyserial to communicate with my arduino on com5. Everythin works well when I use IDLE, BUT then when I use Spyder the servo does not rotate. I know it connects however, because I get RX blinks on the Arduino uno every 2 seconds, showing that time.sleep is firing, but no rotation. Again, the problem is only on Spyder. It works on IDLE.

This is the sketch for the servo on an arduino uno:

#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(13);
  Serial.begin(9600);
}


void loop()
{
  if (Serial.available() > 0)
  { 
    if (Serial.read() == 's')
    {
      myservo.write(90);
      delay(0);
    }
    if (Serial.read() == 'a')
    {
      myservo.write(45);
      delay(0);
    }
    if (Serial.read() == 't')
    {
      myservo.write(135);
      delay(0);
    }
  }

}

This is the code on python (Spyder)

import serial
import time


ser1 = serial.Serial('COM5', 9600)

ser1.write(b's')
time.sleep(2)
ser1.write(b'a')
time.sleep(2)
ser1.write('s'.encode())
time.sleep(2)
ser1.write('a'.encode())
time.sleep(2)
ser1.write('t'.encode())
time.sleep(2)

as you can see I encoded as bytes and encode, and still nothing. Thanks in advance.

Traviccc
  • 147
  • 1
  • 4

0 Answers0