KEY_ESC never reached in else if

Check for ESC key never reached in de code below.
Using TerraTerm to send key strokes to the Arduino are showing me that the ESC HEX-code =0x27 but this key is never reconized. Why not??

#include <Arduino.h>
#include <Streaming.h>  /* http://arduiniana.org/libraries/streaming/ */

#define   KEY_ESC         0x1B

boolean   recvCommand  =  false;

void setup() {
  Serial.begin(9600);
  Serial.println("Enter Command: ");
  Serial << "BIN\tOCT\tDEC\tHEX" << endl;
}

void loop() {
  char c = Serial.read(); // note read() returns an int, char c = converts it to a char
  if (c != -1) {  // read() return -1 if there is nothing to be read.
    // got a char show it
    Serial.print(c, BIN);
    Serial.print("\t");
    Serial.print(c, OCT);
    Serial.print("\t");
    Serial.print(c, DEC);
    Serial.print("\t");
    Serial.print(c, HEX);
    Serial.print("\t");
    Serial.println(c);
    recvCommand = true;
  }
  if (recvCommand == true) {
    Serial.println("Execute command");
    recvCommand = false;
  } else if (recvCommand == true && c == KEY_ESC) {
    Serial.println("Execute num-key");
    recvCommand = false;
  }
}

gharryh:
ESC HEX-code =0x27

No. It's 0x1B.

1:  if (recvCommand == true) {
2:   Serial.println("Execute command");
3:    recvCommand = false;
4:  } else if (recvCommand == true && c == KEY_ESC) {
5:    Serial.println("Execute num-key");
6:    recvCommand = false;

The "else if" in line 4 is only executed when recvCommand is false. But then it checks if recvCommand is true (and it never is), so nothing else will happen!

Thanx correcting the if flow works

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.