Hello.
This issue is more of a slight bother than a problem, though I would like to be sure I am doing things properly.
I am new to Arduino, though my first little projects have been extremely fun and educational. I am now moving on to a more complicated project and am in the early stages of development. I am now working with a mobile platform and have cobbled together a little program to assist in the testing of motors. The code changes the platform's direction using a keyboard, and it has been enormously helpful.
My little, tiny, eeny weeny issue is that when I open the Serial Monitor and send my first command, the instruction seems to be ignored. All successive commands work flawlessly, though this initial hiccup is causing me worry that I am making a mistake somewhere.
This is the program as it stands (I have used two pieces of publicly available code that were fiddled, nibbled, and expanded into something that works for me):
// Control a USB connected 4WD mobile platform from a keyboard via the Serial Monitor in the Arduino IDE.
// w - forward
// z - backwards
// a - left
// s - right
// q - stop
const int speed = 200; // Speed setting from 1 (slow) to 255 (fast)
int incomingByte; // A variable to read incoming serial data into
void setup() {
// Initialize serial communication:
Serial.begin(9600);
// Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
// Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
}
void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'w') {
// Forward
digitalWrite(13, HIGH);
digitalWrite(8, LOW);
analogWrite(11, speed);
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, speed);
}
if (incomingByte == 'z') {
// Backward
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, speed);
digitalWrite(12, HIGH);
digitalWrite(9, LOW);
analogWrite(3, speed);
}
if (incomingByte == 's') {
// Hard right
digitalWrite(13, LOW);
digitalWrite(8, LOW);
analogWrite(11, speed);
digitalWrite(12, LOW);
digitalWrite(9, LOW);
analogWrite(3, speed);
}
if (incomingByte == 'a') {
// Hard left
digitalWrite(13, HIGH);
digitalWrite(8, LOW);
analogWrite(11, speed);
digitalWrite(12, HIGH);
digitalWrite(9, LOW);
analogWrite(3, speed);
}
if (incomingByte == 'q') {
// Stop
digitalWrite(8, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
}
}
}
Does anyone spot an error I have made that would be causing this little glitch? (Any other criticisms of this code would be welcome as well, I love learning from my mistakes.)
Thank you so much for any help, suggestions, or advice.