i am trying to control a 2 wheeled robot with the arduino and the L298 solarbotics motor controller through the serial port. here is the code
/*
Buffalo Serial Control Program
started writing May 19 2009
*/
int oMCEL = 2; // Pin 2 connected to Motor Control Enable Left
int oMC2 = 3; // Pin 3 connected to Motor Control input 2
int oMC1 = 4; // Pin 4 connected to Motor Control input 1
int oMCER = 5; // Pin 5 connected to Motor Control Enable Right
int oMC4 = 6; // Pin 6 connected to Motor Control Input 4
int oMC3 = 7; // Pin 7 connected to Motor Control Input 3
int incomingByte = 0;
void setup() // setup
{
Serial.begin (9600); // setup serial
pinMode(oMCEL, OUTPUT); // Sets pin for Motor Control Enable Left to out
pinMode(oMCER, OUTPUT); // Sets pin for Motor Control Enable Right to out
pinMode(oMC1, OUTPUT); // Sets pin for Motor Control 1 to out
pinMode(oMC2, OUTPUT); // Sets pin for Motor Control 2 to out
pinMode(oMC3, OUTPUT); // Sets pin for Motor Control 3 to out
pinMode(oMC4, OUTPUT); // Sets pin for Motor Control 4 to out
}
void loop() // Repeat Inside Loop
{
digitalWrite(oMCEL, HIGH);
digitalWrite(oMCER, HIGH);
if (Serial.available()>0) {
incomingByte = Serial.read();
//up w = 119
if (incomingByte == 119) {
digitalWrite (oMC1,HIGH);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,HIGH);
digitalWrite (oMC4,LOW);
delay (500);
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,LOW);
}
//left a = 97
if (incomingByte == 97) {
digitalWrite (oMC1,HIGH);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,HIGH);
delay (100);
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,LOW);
}
//down s = 115
if (incomingByte == 115) {
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,HIGH);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,HIGH);
delay (500);
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,LOW);
}
//right d = 100
if (incomingByte == 100) {
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,HIGH);
digitalWrite (oMC3,HIGH);
digitalWrite (oMC4,LOW);
delay (100);
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,LOW);
}
else {
digitalWrite (oMC1,LOW);
digitalWrite (oMC2,LOW);
digitalWrite (oMC3,LOW);
digitalWrite (oMC4,LOW);
}
Serial.print ("I recieved: ");
Serial.println (incomingByte, DEC);
}
}
while using the keyboard and the serial monitor in the arduino IDE i get the following error:
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:206)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
this error does not just come up once but repeatedly until i unplug the arduino. the error does not occur right away, but happens seemingly randomly after 1 command or 50 or anywhere in between. i have not been able to do anything to duplicate the error (ie: specific commands in a specific order) but it always happens eventually.
while using PuTTY to control the serial port i get:
PuTTY Fatal Error
Error writing to serial device
again, this occurs randomly after an uncertain number of commands
would adding some kind of buffer to my program on the arduino solve this? and if so, how would i go about constructing this buffer?
if a buffer would not help, then does anyone have any idea why these errors are occuring?