The whole code with the for loop commented out following the four individual lines in the format -
"wheel[n] = wheels[dir][m];
It uses example 5 from a great tutorial I found for parsing serial input -
Serial input is in the format <speed1, speed2, speed3, speed4, dir>
// Adafruit Motor Shield
#include <Adafruit_MotorShield.h>
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motRF = AFMS.getMotor(1);
Adafruit_DCMotor *motRR = AFMS.getMotor(2);
Adafruit_DCMotor *motLR = AFMS.getMotor(3);
Adafruit_DCMotor *motLF = AFMS.getMotor(4);
int speed[4] = {0, 0, 0, 0}; // 0 <= n <= 255
int wheel[4] = {4, 4, 4, 4}; // fore (1), rev (2), stop (4)
int dir = 0; // compass directions, 1 to 16 cw & 17/18 rot
#define for 1 // FORWARD in Adafruit_MotorShield.cpp
#define rev 2 // BACKWARD in Adafruit_MotorShield.cpp
#define halt 4 // RELEASE in Adafruit_MotorShield.cpp
int wheels[19][4] {
{halt, halt, halt, halt}, // All Stop
{for, for, for, for }, // N
{for, for, for, for }, // NNE
{halt, for, halt, for }, // NE
{rev, for, rev, for }, // ENE
{rev, for, rev, for }, // E
{rev, for, rev, for }, // ESE
{rev, halt, rev, halt}, // SE
{rev, rev, rev, rev }, // SSE
{rev, rev, rev, rev }, // S
{rev, rev, rev, rev }, // SSW
{halt, rev, halt, rev }, // SW
{for, rev, for, rev }, // WSW
{for, rev, for, rev }, // W
{for, rev, for, rev }, // WNW
{for, halt, for, halt}, // NW
{for, for, for, for }, // NNW
{rev, rev, for, for }, // CW
{for, for, rev, rev } // CCW
};
// Handle data input via serial
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
boolean newData = false;
void setup() {
Serial.begin(9600); // Set up Serial library at 9600 bps
AFMS.begin(1000); // PWM Frequency 1.0KHz
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
wheel[0] = wheels[dir][0];
wheel[1] = wheels[dir][1];
wheel[2] = wheels[dir][2];
wheel[3] = wheels[dir][3];
// for (int i = 0; i < 4; i++) {
// wheel[i] = wheels[dir][i];
// }
motLFgo();
motLRgo();
motRRgo();
motRFgo();
}
// Functions ================================
void motLFgo() {
motLF ->setSpeed(speed[0]);
motLF ->run(wheel[0]);}
void motLRgo() {
motLR ->setSpeed(speed[1]);
motLR ->run(wheel[1]);}
void motRRgo() {
motRR ->setSpeed(speed[2]);
motRR ->run(wheel[2]);}
void motRFgo() {
motRF ->setSpeed(speed[3]);
motRF ->run(wheel[3]);}
// Data input
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
speed[0] = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // continue where the previous call left off
speed[1] = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
speed[2] = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
speed[3] = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
dir = atoi(strtokIndx); // convert this part to an integer
}
void showParsedData() {
Serial.print("speed [0]:");
Serial.print(speed[0]);
Serial.print(" [1]:");
Serial.print(speed[1]);
Serial.print(" [2]:");
Serial.print(speed[2]);
Serial.print(" [3]:");
Serial.print(speed[3]);
Serial.print(" dir:");
Serial.println(dir);
}