BLE Serial Communication [char vs int]

hey,

im having a little disparity of my code, in that on the serial monitor i can see the output, but the program is not responding to it!

i have a small app on the phone, that when you push buttons 1, 2, 3, then the numbers appear in the serial monitor as 1, 2, 3. but when assigning these outputs to a variable (in this case program), the switch just isn’t responding.

i think its something to do with the Serial.write(inByte) as opposed to the usual Serial.print(inByte).

write will give me responses as 1,2,3
print will give me responses as 4810, 4910, 5010

also, if i change my case to 4810, then it still doesn’t work :-/

/************************************************************
  BLE CC41A Bluetooth
  This is a clone of the HM10 BLE board

  In the Serial Monitor ensure that 'Both NL and CR' is selected
  Select a Baud Rate of 9600
********************************************************/
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(7, 8); // RX, TX

int program;

void setup() {
  // Start the hardware serial port
  Serial.begin(9600);
  bluetooth.begin(9600);
  Serial.println("ready");

}

void loop() {
  bluetooth.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (bluetooth.available() > 0) {

    char inByte = bluetooth.read();
    //int inByte = bluetooth.read();

    program = inByte;
    Serial.write(inByte);

    switch (program) {
      case 1:
        Serial.println(" program 1 ");
        break;
      case 2:
        Serial.println(" program 2 ");
        break;
      case 3:
        Serial.println(" program 3 ");
        break;
      case 4:
        Serial.println(" program 4 ");
        break;
    }

  }

  // Read user input if available.
  if (Serial.available()) {
    bluetooth.write(Serial.read());
  }

}

Why are you storing a character in an int? There is NO point to that. You CAN switch() on inByte (thought that is a stupid name for a variable whose type is char).

   char c = bluetooth.read();
   switch(c)
   {
      case '1':
         Serial.println("Got a ONE");
         break;
   }

neither char, nor int, allow the code to run.

and thats whats stumping me. it should be working. when i send 1, i get 1, and switch is assigned to 1, so it should run switch case 1.

except it doesn't.

changing char to int, produces figures 0>4810, 1>4910, 2>5010

"4810" is two chars : "1" and "new line"
let your code filter away the newline char.

knut_ny:
"4810" is two chars : "1" and "new line"
let your code filter away the newline char.

no idea how to do that.

im not even putting in the "newline" and i don't understand how 1 equates to 49

but... if i change my case numbers to 48, 49, 50, then they are working...

which is a crappy-not-understanding-what-is-going-on-but-working

...kinda like a microsoft beta release.

http://www.theasciicode.com.ar/ascii-printable-characters/number-zero-ascii-code-48.html

ha! so its ascii... the correlation between the random numbers and numbers!