0
votes

I've tried to send mouse position to Arduino to move a servo and it works correctly.

Unfortunately after a while it looks like the serial port disconected and I don't know why: could it be because I send too many data or the problem lies with the cable?

Here is sender code:

void draw(){
  String posx = ""+mouseX%360+'\n';
  if(cam.available()){
    cam.read();
  }
  image(cam,0,0);
  port.write(posx);
}

and here is Arduino code (I use Arduino Uno):

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
int x;
String y;
void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
void loop() 
{  
  x = Serial.parseInt();
  y = Serial.readStringUntil('\n');
  myservo.write(x);
} 

Thank you!

1

1 Answers

0
votes

Into your loop() function don't forget to put 'if (Serial.available())' statement: https://www.arduino.cc/en/Serial/Available .

If your problem persists, this guide will really help you: https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing .