I wanted to do serial communication between computer and arduino uno via serial cable.
The serial cable that I mentioned is usually used for unloading programs to arduino.
I thought using this cable, I could achieve connection between my laptop(visual studio) and arduino uno.
And it seemed to work fine but time to time it malfunctioned. And I just don't know why.
The purpose of doing this is to make my servos turn according to
the cartesian coordinates sent by my lap top in form of
(x coordinate number) , (y coordinate number) .
ex) 123,234.345,234.123,232. ............................
The "coordinates" are coordinates of a ball in the video taken by my camera attached to my laptop
The coodinates of the ball are calculated by using OpenCV
my arduino gets this "string" and saves it until comma(",") or period(".") is met.
when it meets comma, it converts the string it has saved so far into a number and saves the value
in the "int" variable named "DrawX". And erases(clears) the saved string.
when it meets period, it converts the string it has saved so far into a number and saves the value
in the "int" variable named "DrawY". And erases(clears) the saved string.
And then the arduino turns servo to 180 degrees if value in the "DrawX"is over 200.
if the value is below 200 or same as 200, it makes the servo to go 0 degree.
When I actually ran the code, It sometimes worked and sometimes didn't.
So I checked arduino part alone. Putting in coordinates by using serial monitor
(while not running any programs using serial communication on the computer.)
And found out that it show errors when ever I entered same coordinate twice.
And also my arduino got disconnected, making it no longer able to upload programs by using arduino app.
The error messages and codes are shown below. And attached file is the video showing the results
=============================================================================================
The code
#include <Servo.h>
String inString = ""; // string to hold input
int DrawX, DrawY= 0;
Servo myservo;
Servo myservo2;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
myservo.attach(9);
myservo2.attach(10);
// send an intro:
}
void loop() {
int inChar;
// Read serial input:
if (Serial.available() > 0) {
inChar = Serial.read();
}
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
\ if (inChar == ',') {
// do something different for each value of currentColor:
DrawX = inString.toInt();
// clear the string for new input:
inString = "";
}
// if you get a "." you know you've got
// the last color, i.e. blue:
if (inChar == '.') {
DrawY = inString.toInt();
// print the colors:
Serial.print("DrawX: ");
Serial.print(DrawX);
Serial.print(", DrawY: ");
Serial.print(DrawY);
Serial.print("\n");
if(DrawX>200)
{
myservo.write(0);
myservo2.write(0);
delay(2000);
}
else
{
myservo.write(180);
myservo2.write(180);
delay(2000);
}
// clear the string for new input:
inString = "";
}
}
=============================================================================================
=============================================================================================
error messages
???? ??? ???: 6,272 ??? (?? 32,256 ???)
java.io.IOException: Input/output error in writeArray
at gnu.io.RXTXPort.writeArray(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1124)
at processing.app.Serial.write(Serial.java:517)
at processing.app.Serial.write(Serial.java:540)
at processing.app.SerialMonitor.send(SerialMonitor.java:200)
at processing.app.SerialMonitor.access$100(SerialMonitor.java:32)
at processing.app.SerialMonitor$3.actionPerformed(SerialMonitor.java:89)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:492)
at javax.swing.JTextField.postActionEvent(JTextField.java:705)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:820)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2851)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2886)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2814)
at java.awt.Component.processEvent(Component.java:6040)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)