Hi. I didn't want to start a whole new post and cover ground that previous topics have already covered, but when I tried to use an old topic, noone replied. Anyways, I'm working on being able to control an rc car using an arduino mega 2560 using my computer. I'm using an adafruit motor control shield V1. I have managed to get processing to control the car to move forward, but when I release the 'W' key the motor continues to run forward rather then stop like I would like it to. I eventually added an "else" statement to the code which allowed me hold the 'w' key to move the motor forward, and then when I release the key, it stops the motor. But when I try to use this method to do the rest of the controls, it doesn't want to work. Any suggestions?
Here's my Arduino IDE code:
#include <AFMotor.h>
// Select which 'port' M1, M2, M3 or M4. In this case, M4
AF_DCMotor ThrottleMotor(4);
// You can also make another motor on port M1
AF_DCMotor SteeringMotor(1);
char val; // Data received from serial port
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v1 - DC Motor test!");
//AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
ThrottleMotor.setSpeed(150);
ThrottleMotor.run(FORWARD);
// turn on motor
ThrottleMotor.run(RELEASE);
//OTHER MOTOR
SteeringMotor.setSpeed(150);
SteeringMotor.run(FORWARD);
// turn on motor
SteeringMotor.run(RELEASE);
}
void loop() {
uint8_t i;
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'W' || incomingByte == 'w') {
ThrottleMotor.run(FORWARD);
ThrottleMotor.setSpeed(100);
}
else {
ThrottleMotor.run(RELEASE);
}
if (incomingByte == 'A' || incomingByte == 'a') { //Forward Left Turn
ThrottleMotor.run(FORWARD);
ThrottleMotor.setSpeed(100);
SteeringMotor.run(FORWARD);
SteeringMotor.setSpeed(100);
delay(1000);
SteeringMotor.setSpeed(130);
delay(1000);
SteeringMotor.setSpeed(140);
delay(2000);
SteeringMotor.setSpeed(160);
delay(2000);
}
//else {
//ThrottleMotor.run(RELEASE);
//}
if (incomingByte == 'D' || incomingByte == 'd') {
//Forward Right Turn
ThrottleMotor.run(FORWARD);
ThrottleMotor.setSpeed(100);
SteeringMotor.run(BACKWARD);
SteeringMotor.setSpeed(140);
delay(1000);
}
//else {
//ThrottleMotor.run(RELEASE);
//SteeringMotor.run(RELEASE);
//}
if (incomingByte == 'C' || incomingByte == 'c') {
SteeringMotor.run(RELEASE);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'X' || incomingByte == 'x') {
ThrottleMotor.run(BACKWARD);
for (i=0; i<255; i++) {
ThrottleMotor.setSpeed(i);
delay(10);
}
}
else {
ThrottleMotor.run(RELEASE);
}
//
if (incomingByte == 'S' || incomingByte == 's') {
ThrottleMotor.run(RELEASE);
SteeringMotor.run(RELEASE);
}
}
}
NOTE: These codes weren't written only by me, as I found them online and modified them to fit my purpose.
If there's any questions for clarity, please ask.