Hello all,
I am building a remote control unit for my robot using Sabretooth H-bridge to drive the robot and an 8 port relay DTDP board to control 4 linear actuators. The receiver and controller both use the Arduino Mega with Xbees for wireless communication. The analog input on its own works perfectly to drive the robot. However when combine with multiple digital inputs, analog reading give random results.
Attached are serial output with and without digital inputs.
Any help is appreciated. Thanks.
#include <Bounce2.h>
boolean XbeeData = false;
int packets[5]={0,0,0,0,0},speedKnobPin=3,ledPin=13,ledState=LOW,joyPin0=0,joyPin1=2;
int relay[8][4]={{1,2,0,0},{2,3,0,0},{4,4,0,0},{8,5,0,0},{16,6,0,0},{32,7,0,0},{64,8,0,0},{128,9,0,0}};
unsigned long relayTimer[8][2];
Bounce bounce[8]={
Bounce(2,5),
Bounce(3,5),
Bounce(4,5),
Bounce(5,5),
Bounce(6,5),
Bounce(7,5),
Bounce(8,5),
Bounce(9,5),
};
void setup() {
pinMode(ledPin,OUTPUT);
for (int i=0; i<9; i++){
pinMode(relay[i][1],INPUT);
}
Serial.begin(9600);
Serial3.begin(115200);
Serial.print("Remote Control v1");Serial.print('\n');
Serial.print("Initializing...");
delay(2000);
}
void loop() {
ReadControls();
SendCommand();
}
void SendCommand() {
packets[0]=packets[1]+packets[2]+packets[3]+packets[4]; //Checksum value
Serial.print(packets[0]);Serial.print(",");Serial.print(packets[1]);Serial.print(",");Serial.print(packets[2]);Serial.print(",");Serial.print(packets[3]);Serial.print('\n');
Serial3.write(255); //Send starting marker
for (int i=0; i<6; i++)
Serial3.write(packets[i]);
Serial3.write(254); //Send ending marker
}
void ReadControls() {
int readState,speedKnob;
packets[3]=0;
speedKnob=map(analogRead(speedKnobPin),1024,0,0,60);
packets[2]=map(analogRead(joyPin0),1024,0,60-speedKnob,60+speedKnob);
packets[1]=map(analogRead(joyPin1),1024,0,60-speedKnob,60+speedKnob);
/* //Analog above works fine when the code below are commented
for (int i=0;i<9;i++){
bounce[i].update();
readState=bounce[i].read();
if (readState != relay[i][2]){
relay[i][2]=readState;
if (readState == HIGH){
//packets[3]+=relay[i][0];
} else {
//packets[3]-=relay[i][0];
}
}
} */
}
void GetCommand() { // Data received from Xbee is in byte 0-256. Sending a string requires multiple bytes even for 1 character and requires processing power
static boolean recvInProgress = false; // One complete packet of data contains 6 byte begining with a startMarker and ends with an endMarker. The 4 remaining bytes is then stored
static int ndx = 0; // in an array called packets.
byte startMarker = 255; // packets[0] = Checksum which is the sum of packets[1] + packets[2] + packets[3], this is used to verify if received data is good.
byte endMarker = 254; // packets[1] = packets range from 0 to 120 which drives the y-axis (Throttle). 1-59 is reverse, 61 to 120 is forward, 60 is stop
byte receivedByte; // packets[2] = x-axis (Steering) which is the same as y. X and Y is then converted using Map function to give it a range of -127 to 127, 0 is stop
while (Serial3.available() > 0 && XbeeData == false) { // packets[3] = Misc packetsues to control things such as relays, lights, servos and stepper motor.
receivedByte = Serial3.read();
Serial.print(receivedByte);
if (recvInProgress) {
if (receivedByte = endMarker){
XbeeData=true;
ndx=0;
recvInProgress = false;
break;
} else {
packets[ndx]=receivedByte;
ndx++;
}
}
else if (receivedByte == startMarker){
recvInProgress = true;
}
}
}
void ParseData(){
if (XbeeData) {
Serial.print('\n');
Serial.print(packets[0]);Serial.print(" , ");
Serial.print(packets[1]);Serial.print(" , ");
Serial.print(packets[2]);Serial.print(" , ");
Serial.print(packets[3]);Serial.print(" , ");
XbeeData=false;
}
}

