Hi
I am working on an RC car project where I enter a string of three commands into the serial monitor. It would be the direction, speed out of 255, and the duration.
forward, 150, 4000,
I am having a problem with the direction though. I am not sure how to have a reverse command. I already have it set up to go forward. I want to be able to enter either forward or reverse, then the speed, then duration. How would I set up a reverse?
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
int rightLEDs = 9;
int leftLEDs = 10;
byte index = 0;
char inData[] = "forward, 12, 9876";
char *token = NULL;
byte count = 0;
void setup()
{
pinMode(rightLEDs, OUTPUT);
pinMode(leftLEDs, OUTPUT);
digitalWrite(rightLEDs, HIGH);
digitalWrite(leftLEDs, HIGH);
Serial.begin(9600);
Serial.println("RC Car Project Mark I connected.");
}
void loop()
{
while(Serial.available() > 0)
{
token = strtok(inData, ",");
while(token)
{
count++;
if(count == 2)
{
int inputSpeed = atoi(token);
motor1.setSpeed(inputSpeed);
motor2.setSpeed(inputSpeed);
motor3.setSpeed(inputSpeed);
motor4.setSpeed(inputSpeed);
Serial.print("Running forward at ");
Serial.print(inputSpeed);
Serial.print(" / 255 speed for ") ;
}
else if(count == 3)
{
int duration = atoi(token);
Serial.print(duration / 10);
Serial.println(" seconds.");
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(duration);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
token = strtok(NULL, ",");
}
}
}