-1

I am trying to create a program when the camera capture the servo will rotate in 90 degrees continously, but my code will stop in print output 2 any idea to run the servo continously?

Here's the Code

import numpy as np
import cv2
import serial
import struct
import time


ser = serial.Serial('COM11',9600)

x=0

while(True):


 # Capture frame-by-frame
    cap = cv2.VideoCapture(0)
    #framerate = cap.get(5)
    ret, frame = cap.read()
    cap.release()
    # Our operations on the frame come here



    filename = 'C:/Users/DELVO/Desktop/tryTest/bruises/main/images' +  str(int(x)) + ".png"
    x=x+1
    cv2.imwrite(filename, frame)
    print(ser.write(x))
    time.sleep(5)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Arduino Code

#include <Servo.h>
int data_x = 0;
int data_y = 0;
int data[1];
Servo myservo_x;
Servo myservo_y;// create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  Serial.begin(9600);
  myservo_x.attach(7);  // attaches the servo on pin 9 to the servo object
  myservo_y.attach(10);
  myservo_x.write(90);
  myservo_y.write(90);
}

void loop() {
  while (Serial.available() >= 2) {
    //    data_x=Serial.read();
    //    data_y=Serial.read();
    for (int i = 0; i < 2; i++) {
      data[i] = Serial.read();
    }

    myservo_x.write(data[0]);
    myservo_y.write(data[1]);

    Serial.println(data[0]);
    Serial.println(data[1]);
  }

}

1 Answers1

0

is the arduino code giving error because you have initialized the array data with length of 1 (int data[1];) and in the for loop the length is exceeding the allocated length of the string: for (int i = 0; i < 2; i++) { data[i] = Serial.read(); }