Hi guys
What I'm trying to do is pass 3 characters through the serial and have these interpreted as an angle. This angle is then passed to the servo to move it.
/*
Communicate */
#include <Servo.h>
Servo myservo; // create servo object to control my servo
char byteread; //Char to hold the incoming byte from serial
int angle; //int to hold servo angle
int i = 0; //int to hold a counter variable
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); //begins serial communication
}
void loop()
{
if (Serial.available()){
delay(100);
while(Serial.available()>0)
{
i=i++; // counter to determine which character is incoming from serial (looks for 3 in total)
byteread = Serial.read() - '0'; // read current character
// if statements to determine the byte
if (i == 3){ //final counter variable the angle
angle = angle + byteread; //integer = byte * 1
angle = constrain(angle,0,180); // constrain movement angle to prevent inputs out of range for servo
Serial.println(angle);
Serial.println("Servo about to move");
myservo.write(angle); // move servo
Serial.print("Servo Moved");
delay(15);
angle = 0; // reset angle for next 3 character input
i = 0; // reset counter for next 3 character input
Serial.print(" : Loop Finished : ");
}
else if (i == 2){
angle = angle + (byteread * 10); // integer = byte x 10 ( 2nd character)
}
else if (i == 1){
angle = angle + (byteread * 100); // integer = byte x 100 (1st character)
}
}
}
}
If i comment out the servo code and upload it the angle return works fine (again and again), however if i move the servo it seems to cause instability. It works but eventually crashes with the following error.
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)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I'm scratching my head to this one as i cant see why its unstable, however i am fairly amateurish at this. Any help or guidance that you guys could provide would be very welcome. That may be a more robust approach or improving the above code.
Thanks
Steve