Receiving a two digit number in arduino UNO via bluetooth serial

Hello !

i am working with a project where i send a number “12” to arduino. When i receive the number and print it , it displays garbage characters with itself

Here is the code :

#include <SoftwareSerial.h>
#include <Servo.h>

Servo servo1;
Servo servo2;
int servo1angle=90;
int servo2angle=90;
String str = “”;
int TX = 12;
int RX = 13;

char bufferr[2] ;
int i=0;

SoftwareSerial HC_06(TX, RX); //Bluetooth TX to 10 and Bluetooth RX to 11.

void setup()
{
servo1.attach(7);
servo2.attach(8);
Serial.begin(9600);
HC_06.begin(9600);
}

void loop()
{
while(HC_06.available()> 0 )
{
char bytee = HC_06.read();

if(bytee >=48 && bytee<= 57)
{
bufferr = bytee;

  • i++;*

  • }*

  • if(bytee == ‘:’)*

  • {*

  • //Serial.println(bufferr);*

  • str = bufferr;*

  • Serial.println(str);*

  • str = ‘\0’;*

  • break;*

  • }*

}

}



            bufferr = bytee;
            i++;

doesn't look right. i think you intended to do

           bufferr  [i++] = bytee;

but bufferr needs to have a terminating NULL character otherwise the print doesn't know where the end of the string is. bufferr needs to be at least 3 bytes long to hold a 2 character string and the 3rd byte needs to be set to NULL before printing.

you should also reinitialize i to 0 after receiving and processing 2 characters