[SOLVED ]Reading From Serial

My code.

// Motor Control Script

// Define Motor A Pins
#define MotorAE 5 // Enable
#define MotorA1 6 // Input 1
#define MotorA2 7 // Input 2

// Define Motor B Pins
#define MotorBE 8   // Enable
#define MotorB1 9   // Input 1
#define MotorB2 10  // Input 2

// Serial
#define MySerial 9600

void setup() {
  // put your setup code here, to run once:

  // Start the serial
  Serial.begin(MySerial);

  // Configure pin modes
  // Motor A
  pinMode(MotorAE, OUTPUT);
  pinMode(MotorA1, OUTPUT);
  pinMode(MotorA2, OUTPUT);

  // Motor B
  pinMode(MotorBE, OUTPUT);
  pinMode(MotorB1, OUTPUT);
  pinMode(MotorB2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()) {
    int c = Serial.read();
    switch(c) {
      case (8): // Forward
        Serial.println("Forward");
        digitalWrite(MotorA1, HIGH);
        digitalWrite(MotorA2, LOW);
        digitalWrite(MotorAE, HIGH);
        digitalWrite(MotorB1, HIGH);
        digitalWrite(MotorB2, LOW);
        digitalWrite(MotorBE, HIGH);
        break;
      case (5): // Stop
        Serial.println("Stop");
        digitalWrite(MotorAE, LOW);
        digitalWrite(MotorBE, LOW);
        break;
      case (2): // Back
        Serial.println("Back");
        digitalWrite(MotorA1, LOW);
        digitalWrite(MotorA2, HIGH);
        digitalWrite(MotorAE, HIGH);
        digitalWrite(MotorB1, LOW);
        digitalWrite(MotorB2, HIGH);
        digitalWrite(MotorBE, HIGH);
        break;
    }
  }  
}

The problem is, its not reading properly from the serial... I don't know why... Help please

let me guess sending 4 will turn left and 6 will turn right? :slight_smile: - are you using the number keypad of your PC to send those?

try   case ('8'): // Forwardinstead of   case (8): // Forward (same for the others if what you type on Serial are the symbol 8 which is sent as an ASCII value, represented by '8' with the quotes)

J-M-L:
let me guess sending 4 will turn left and 6 will turn right? :slight_smile: - are you using the number keypad of your PC to send those?

try   case ('8'): // Forwardinstead of   case (8): // Forward (same for the others if what you type on Serial are the symbol 8 which is sent as an ASCII value, represented by '8' with the quotes)

Yes I'm going to do that :P.

I've tried what you said, using case ('8') instead of case (8) and that worked. Thanks - I feel stupid because it was that simple hahaha.

Thanks!

Good! we all made that error once. Now you won't. :slight_smile:

So you need to remember that characters are represented by a code. This code is the ASCII code.

so either you test against '8' with the quotes or you test against its code which is 56 in decimal or 0x38 in hexadecimal. when you were testing against the code value 8, you were testing against the backspace key which you can't send from the console :slight_smile:

Maybe have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R