I’m pretty new to this stuff, so I assume I’ve made a mistake somewhere. Using Processing I’m sending game controller inputs to the Maple via XBee S1 transceivers. I have a simple protocol that uses ‘$’ as begin of command and ‘Z’ as end of command.
For testing I’m mapping the received input to a PWM output for an LED. The LED brightness changes erratically to input changes, making me think I’m not reading all the bytes in the command.
Here’s what I send from the PC in Processing:
XBeePort.write("$"+(int)lUpDown+"Z");
Where, lUpDown ranges from 0 to 510.
Here’s the Maple code:
'#include <stdlib.h>
char commandBegin = '$';
char commandEnd = 'Z';
char InputCommand[100];
int maxIn = 510;
int minIn = 0;
char currentChar = ' ';
void setup() {
Serial1.begin(9600);
delay(500);
Serial1.flush();
pinMode(0, PWM);
}
void loop() {
while (Serial1.available()>0){
currentChar = Serial1.read();
if(currentChar == commandBegin){
clearCommand(InputCommand);
int i = 0;
while(currentChar != commandEnd){
currentChar = Serial1.read();
delay(50);
InputCommand[i] = currentChar;
i++;
}
}
if (currentChar == commandEnd){
handleCommand(InputCommand);
clearCommand(InputCommand);
}
}
}
void handleCommand(char InputCommand[]){
int pwmValue=map(atoi(InputCommand),minIn,maxIn,0,65535);
pwmWrite(0,pwmValue);
}
void clearCommand(char InputCommand[]){
int commandSize = sizeof(InputCommand);
for (int i = 0; i < commandSize - 1; i++) {
InputCommand[i]=' ';
}
} '
Can anyone suggest why I might not be reading all of the command bytes?
Thanks