I am using OpenCV and Python for Video Processing. The code detects a binary thresholded object in the camera frame. If the particular colored object is in front of the cam, python sends char '"1" using pyserial. As a while loop is used to go through each frame of the video, RX is active most of the time on arduino. Now if i receive char "1", i want to set Servo to 90 degrees else every time I want to set Servo to 0 degrees.
Python Code -
import opencv
import serial
ser = serial.Serial("/dev/ttyACM0", 115200)
cap = cv2.VideoCapture(0)
while(cap.issOpened()):
ret, img = cap.read()
#Thresolding Code
if(object_detected):
serial.write("1")
Arduino Code -
#include <Servo.h>
int incomingByte = 0;
Servo myservo;
void setup() {
Serial.begin(115200);
myservo.attach(9);
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
if(incomingByte == 49){
myservo.write(90);
}
else{
myservo.write(0);
}
}
}
The RX receives the byte on time but the loop stucks while transmitting. How can I optimize my code to solve the issue? i tried Serial.flush() function which waits for transmission to complete but it was of no help.
Thanks in advance.