I'm unfortintly very very new to programming so of course i have 9000 syntax errors but this issue has me completely puzzled.
What i want it to do.
input of 1 (over Serial.read) flips the motor on for 1 second forward sending a Serial.println of "moving forward" and "stopped moving" after 1 second and the Hbridge is shut off.
input of 2 (Serial.read again) flips the motor on for 1 second reverse sending the same information and same time interval.
input of anything else causes a Serial.println of "huh?"
What is happening...
on an input of 1 the motor turns on and off just fine but the Serial.println of
"moving forward
stopped moving
huh?"
i have absolutely no clue why its giving me "huh?" at the end... heres the code (i removed asimov's laws from the // hopefully all of you know them already...)
int reversePin1A =2;
int reversePin2A =3;
int forwardPin1A =4;
int forwardPin2A =5;
void setup()
{
Serial.begin(9600);
pinMode(forwardPin1A, OUTPUT);
pinMode(forwardPin2A, OUTPUT);
pinMode(reversePin1A, OUTPUT);
pinMode(reversePin2A, OUTPUT);
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
if (val == 1)//input of 1 makes it move forward 1 second
{
Serial.println("moving forward");
digitalWrite(forwardPin1A, HIGH);
digitalWrite(forwardPin2A, HIGH);
delay(1000);
digitalWrite(forwardPin1A, LOW);
digitalWrite(forwardPin2A, LOW);
Serial.println("stopped moving");
}
if (val == 2)//input of 2 makes it move backwards for 1 second
{
Serial.println("moving reverse");
digitalWrite(reversePin1A, HIGH);
digitalWrite(reversePin2A, HIGH);
delay(1000);
digitalWrite(reversePin1A, LOW);
digitalWrite(reversePin2A, LOW);
Serial.println("stopped moving");
}
else
{
Serial.println("huh?");// unrecognized command confusses it
}
}
thanks in advance for helping a newb