I am using the standard Adafruit BLE example to receive user-typed characters from a phone app via BLE UART. The incoming data is assigned to "ch" and it is readable by the serial-monitor on the COM port. I would like to be able to use the incoming data represented by "ch" as an integer, but it seems to be in ASCII format.
For example, if I type from the phone a "2" then "ch" value of "2" shows up in the serial-monitor via serial.write. But if I want to serial.print(ch) I get the corresponding ASCII value of 50 instead. How can I convert this ASCII reading into an integer with the value of 2?
Here is the simplified code:
#include <Arduino.h>
#include <bluefruit.h>
void loop() {
while ( bleuart.available() )
{
uint32_t ch;
ch = (uint32_t) bleuart.read();
Serial.write(ch); Serial.print(" "); Serial.println(ch);
}
}
RESULTS in serial-monitor: 2 50
============================================================
Then if I add print char(ch) to convert into an integer:
Serial.write(ch); Serial.print(" "); Serial.println(ch);
Serial.print(char(ch)); Serial.println(" <= char(ch) ");
RESULTS in serial monitor:
2 50
2 <= char(ch)
10
<= char(ch)
===========================================================
Then I try to assign chr(ch) to an integer variable, called offTime2:
Serial.write(ch); Serial.print(" "); Serial.println(ch);
Serial.print(char(ch)); Serial.println(" <= char(ch) ");
offTime2=(char(ch));
Serial.print(offTime2);Serial.println(" <= this is offTime2 ");
RESULTS in serial monitor:
2 50
2 <= char(ch)
50 <= this is offTime2
10
<= char(ch)
10 <= this is offTime2

