Hello,
I have an arduino mega 2560, an 18V15 simple motor controller, and a 37d brushed DC motor. I am using an xbee series 1 plus a parallax 2 axis pot to transmit (using only one side), and an Xbee series 1 connected to the Mega to receive. There is a volage logic level converter between the xbee and Mega. This is my 1st 4X4 robot attempt.
Ultimately, if there is an error, I would like to reset the error wirelessly.
Everything works great ( FWD, STOP, BKWD) until I put in the following code in just after the void loop. When this code is in the program, the motor stops working and the Motor Control Center states I have a Serial and Format errors which keeps counting up. I have the ERR pin on the motor controller connected to pin 3 on the Mega.
val = digitalRead(errPin);
Serial.println(val);
delay(1);
-
I guess the question is why when this is in the program does it cause serial errors? and
-
Any ideas on what can I do to correct it?
Thank you,
Scott
Here is the entire code:
//4X4 ROBOT using Pololu motor controller and Pololu motors...
unsigned int loByte = 0; // low byte not counting 7E, byte 11
unsigned int hiByte = 0; // hi byte not counting 7E, byte 12
unsigned int data = 0; // raw data gathered from serial read command
int mapvalueFwd = 0; // map 0-1023 to 0-100: Motor controller 100% = full speed 0 = stop
int mapvalueRev = 0; //map 1023-0 to 0-100: Motor controller 100% = full speed 0 = stop
int lohiByte = 0; // low and hi byte combined, max 1023
int errPin = 3; //pin #3 to be set as input to read either high(error) or low (no error)
int val = 0;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
pinMode(errPin, INPUT);
}
void loop()
{
// This is the problem, the next three lines:
// val = digitalRead(errPin);
//Serial.println(val);
// delay(1);
if(Serial1.available() > 0)
{
data = Serial1.read(); //reads data from serial port on the mega
if (data==0x7E) // start delimeter. read byte and throws it away
{
for(int i = 0; i < 12; i++) // counter
{
Serial1.read();
delay(1);
}
hiByte = Serial1.read(); //byte #11
delay(1);
loByte = Serial1.read(); // byhte #12
delay(1);
}
/*
if(hiByte <= 0x0F) // debug starts here. This section places a 0 in front of the hex value,
// otherwise the hex would show only one number ie A not two numbers ie 0A.
{
Serial.print("0");
}
Serial.print(hiByte,HEX);
Serial.print("\t");
if(loByte <= 0x0F)
{
Serial.print("0");
}
Serial.print(loByte,HEX);
*/
lohiByte = word(hiByte,loByte); //combines hi and lo bytes ie 0A + 16. word command = 0A16
// Serial.print("\t");
// Serial.print(lohiByte); //bebug ends here.
//Serial.println();
if(lohiByte >=500 && lohiByte <=530)
{
Serial1.write(134);
Serial1.write(0);
Serial1.write(0);
}
if(lohiByte >530)
{
mapvalueFwd = map(lohiByte,0,1023,0,100);
Serial1.write(134);
Serial1.write(0);
Serial1.write(mapvalueFwd);
}
if(lohiByte < 500)
{
mapvalueRev = map(lohiByte,1023,0 ,0,100);
Serial1.write(133);
Serial1.write(0);
Serial1.write(mapvalueRev);
}
}
}