working with incoming bytes

Hi, I have an issue trying to use a switch statement with a serial read. (I know just one read is not usually enough bit I am keeping it down to brass tacks).

Basically, when I send a byte (like 5) through the PC serial port, the switch statement always goes to default. Why wont it recognise the 5. It will echo 5 back through default, but not recognise it as an int.

case -1 when nothing read, of course works.

define STEP_UP 3

define STEP_DOWN 4

define HALT 5

define MOVE_UP 6

define MOVE_DOWN 7

void loop() { s_incoming = Serial.read (); if (s_incoming == 5) Serial.println ("5 received");

switch (s_incoming) { case STEP_UP: s_incoming = 0; // delayMicroseconds(STEP_DELAY); Serial.println ("went up a step"); //DEBUG ONLY break;;

case STEP_DOWN: s_incoming = 0; //delayMicroseconds(STEP_DELAY); Serial.println ("went down a step"); //DEBUG ONLY break;

case HALT: s_incoming = 0; Serial.println("STOPPED"); break;

case MOVE_UP: //do these on a for loop Serial.println("Moving UP X steps"); s_incoming = 0; break;

case MOVE_DOWN: // do these on a for loop Serial.println ("moving DOWN X steps"); s_incoming = 0; break;

case -1: //Serial.println("no action"); break; //do nothing

default: Serial.write(s_incoming); s_incoming = 0; } //Serial.write(s_incoming); }

5 the byte (0x05) or 5 the char ('5')?

like this:

byte s_incoming;


#define STEP_UP '3'
#define STEP_DOWN '4'
#define HALT '5'
#define MOVE_UP '6'
#define MOVE_DOWN '7'

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  {
    s_incoming = Serial.read ();
    if (s_incoming == '5')
      Serial.println ("5 received");
    switch (s_incoming) {
      case STEP_UP:
        s_incoming = 0; // delayMicroseconds(STEP_DELAY);
        Serial.println ("went up a step"); //DEBUG ONLY
        break;;

      case STEP_DOWN:
        s_incoming = 0;  //delayMicroseconds(STEP_DELAY);
        Serial.println ("went down a step"); //DEBUG ONLY
        break;

      case HALT:
        s_incoming = 0;
        Serial.println("STOPPED");
        break;

      case MOVE_UP: //do these on a for loop
        Serial.println("Moving UP X steps");
        s_incoming = 0;
        break;

      case MOVE_DOWN: // do these on a for loop
        Serial.println ("moving DOWN X steps");
        s_incoming = 0;
        break;

      case -1:
        //Serial.println("no action");
        break; //do nothing

      default:
        Serial.write(s_incoming);
        s_incoming = 0;
    }
  }
}

the "echo" was the char you sent... not an echo.

‘5’ is not the same as 5.

5 is the code for control-E in ASCII, character constants are represented by integer values (char is actually an integer type).

For instance 'A' == 65, 'B' == 66, etc, '!' == 33.

You have to know the difference between a character and its integer code. Normally with a serial terminal application you are handling characters so typing a '5' is character '5', not integer value 5.

Serial.read() returns the character value, which you can treat as a character or an integer, so you need to be clear which at all times.

GUYS GUYS!! You all ort of got it.. a Syntax error in the switch. It schould of course be '5' not 5 .. nasically I have to make the int a char. I can put a variable in there by declaring the variables as const char or as MArkT says #define 65 for "A" etc. Simple DOH!!

Thanks to everyone.

MikeDutton: ...declaring the variables as const char or as MArkT says #define 65 for "A" etc. Simple DOH!!

careful careful

"A" != 'A'