When I am sending hex numbers via computer to the arduino leonardo, it sometimes won't react, but the serial monitor says that it was sent. My code;
void setup()
{
Serial.begin(9600);
pinMode(9, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
delay(100); // Wait for 100 millisecond(s)
Serial.println(Serial.read());
if (Serial.read() == 49) {
Serial.println("left");
analogWrite(9, 1023);
digitalWrite(A0, HIGH);
analogWrite(10, 1023);
digitalWrite(11, LOW);
}
if (Serial.read() == 50) {
Serial.println("right");
analogWrite(9, 1023);
digitalWrite(A0, HIGH);
analogWrite(10, 1023);
digitalWrite(11, LOW);
}
if (Serial.read() == 51) {
Serial.println("backwards");
analogWrite(9, 1023);
digitalWrite(A0, LOW);
analogWrite(10, 1023);
digitalWrite(11, LOW);
}
if (Serial.read() == 52) {
Serial.println("forwards");
analogWrite(9, 1023);
digitalWrite(A0, HIGH);
analogWrite(10, 1023);
digitalWrite(11, HIGH);
}
if (Serial.read() == 48) {
Serial.println("stop");
analogWrite(9, 0);
digitalWrite(A0, LOW);
analogWrite(10, 0);
digitalWrite(11, LOW);
}
}
When I send the numbers 1,2,3,4, and 0, it reads what the command does. Pins A0 and D11 are direction control for the motor controller, and pins D9 and D10 are speed.
