Serial monitor and integers

My aim is to send a number from an MIT ai2 Android app to an Arduino via Bluetooth and an HC05. (The binary value will then be used to populate a boolean array.)
In the short term I'm trying to test my Arduino code and external circuit by using a text input from the Serial Monitor.

I'm aware that the Serial Monitor sends characters, rather than numbers.
It appears to be possible to send numbers (1, 2 or 4 byte) from the ai2 Bluetooth client.
But is there an way to get the serial monitor to do the same?

Serial monitor will only send ASCII. You can use a function like atoi to convert the ASCII to an integer.

Thanks.
I'll look it up.
Will it be problematic to use atoi if it is fed with an integer from the bluetooth client, or is it best to remove that function when I move to using the HC05?

The input to the atoi function is an ASCII string (null terminated character array) that represents a number.

Another way is to use the parseInt function.. That function takes a string, too.

What is being sent from the MIT ai2 Android app? I am not familiar with that. I use the Serial Bluetooth Terminal app.. If I send a number from that app, it comes in through the HC05 as ASCII and must be converted before it is a number that can be use in math functions.

Here is a demo using methods from the serial input basics tutorial that receives a number as a string from Bluetooth terminal via a HC05 Bluetooth module connected to an Uno. The Bluetooth app must send a line feed (\n) as a terminator. Connect the HC05 Tx to Uno pin 4 and HC05 RX connected to Uno pin 7 through a 5V to 3.3V voltage divider or modify the code with your connection.


#include <SoftwareSerial.h>
SoftwareSerial bt(4, 7); // RX | TX

const byte numChars = 16;
char receivedChars[numChars];   // an array to store the received data

const byte ledPin = 13;

boolean newData = false;

void setup()
{
   Serial.begin(9600);
   bt.begin(9600);
   Serial.println("<Arduino is ready>");
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, LOW);
}

void loop()
{
   recvWithEndMarker();
   if (newData == true)
   {
      Serial.print("The received string = ");
      Serial.println(receivedChars);
      Serial.print("The string converted to a number = ");
      Serial.println(atoi(receivedChars));      
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (bt.available() > 0 && newData == false)
   {
      rc = bt.read();
      //Serial.print(rc);
      if (rc == '\r')
      {
         return;
      }
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

Thanks for the help @groundFungus
Using the atoi() finction, I've got something that (so far) does what I want. :grinning:

As I like to write my own code, rather than using other people's, I've ended up with something that works, albeit without testing for the escape characters. (I know you're going to ask to see my code here, but bear with me.)
If the incoming data is always (eg) 2 bytes, is there a need to check for \n etc?
(FWIW, it looks simple enough to suffix a 1 or 2 or 4 byte number from the MIT ai2 bluetooth client with an escape character if necessary, but I've not got that far yet.)

No, as said the Serial Monitor can only send Ascii.
Instead of the Serial Monitor, you will have to use a Terminal Program like CoolTerm or RealTerm in order to send the values of the numbers instead of their text representations.

You can choose to work with your custom app in either ascii or numerical values.

The line feed character is necessary to terminate the serial input when using the posted code. The serial Bluetooth terminal app has a setting that automatically adds the line feed when anything is sent.

In general, you need a sync mechanism. Let's say, your android application sends 12 and the next time 34 . Your android application is not aware of the fact that the Arduino was not power up yet. So if you power up the Arduino, it might only see the 2 followed by the 3 and you get the wrong number. Sending 12\n and scanning up till '\n' allows to sync; the first data that the Arduino receives might be wrong but after that it should be OK.

You might also want to look at the example with start-and-endmarker that will prevent that you react on partial data.

I most always use start and end markers. It makes for a pretty robust communication.

Thanks all.
Back to the lab!

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