Clearing or initializing the string

Hello !!

I am connecting two arduino MEGA2560 with help of HC-12 for communication, i do communicate between two arduinos.

But now i am trying to glow up led (which is connected to second arduino) when it gets a command from first arduino. It also gets glows up but when i give another command for another led it does not get glowed up.

But if i Reset the arduino than give command then it glows up. So i thought of to clear the string but i guess string is not been cleared up.

Please help me out !!

Let me look in my crystal ball... I think the problem is in your code!

Let me upload the code :

hc-12_recieve_basic_string.ino (976 Bytes)

If you post code in code tags as described in the “how to use this forum-please read” (#7) more people will be able to see it and offer help.

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
char i;
String j;
void setup()
   Serial.begin(9600);             // Serial port to computer
   HC12.begin(9600);               // Serial port to HC12
   pinMode(22, OUTPUT);
   pinMode(23, OUTPUT);
   pinMode(24, OUTPUT);
   pinMode(25, OUTPUT);
   pinMode(26, OUTPUT);
   pinMode(27, OUTPUT);

void loop()
   while (HC12.available())
      i =;
      j += i;
   if ( j == "22")
      digitalWrite(22, HIGH);
      digitalWrite(23, LOW);
      HC12.print("22 DONE");
      j = "";
   else if (j == "AZY")
      digitalWrite(24, HIGH);
      digitalWrite(25, LOW);
      HC12.print("azy recieve");
      j = "";
   else if (j == "123")
      digitalWrite(26, HIGH);
      digitalWrite(27, LOW);
      HC12.print("123 recieve");
      j = "";
   while (Serial.available())
   j = "";

ok Got it

PLease help me out.

Perhaps you should try digitalWrite(, LOW) in void setup() for all the digital pins that you have an LED attached.

So that all the LEDs have an explicit state when the Arduino reboots.

After a reset (of a mega) all pins are input with pull up disabled. If you set the pins to OUTPUT after that the pins are always LOW.

But I do spot an error, you clear the String (not string) every pass through loop(). And loop() is faster than receiving serial (from BT). So if only a part of the string is in, it might only load "AZ", does all the checks and clears the String. And the next loop you only receive "Y".

Also, are you 100% sure you ONLY send "AZY" etc and no stuff like line breaks?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.


I agree


Robin2: char arrays terminated with 0 NULL / '\0'.

Although the same it's a bit neater :)

Your problem lies with your use of while (xxxx.available()) to read input. The Arduinos are so fast then you will only ever have a maximum of 1 char available at any time!

So you MUST get rid of the while loops and rethink your progam.


I don't really see a problem in the while (xxxx.available()). Only don't expect the complete string to be in after leaving it. :D You could change it to a if() but why not read multiple chars if they happen to be there. If they are not, it's the same as using if().

why not read multiple chars if they happen to be there

  1. KISS - Keep it Simple Stupid Why do 2 things when you only need to do 1!

  2. The ops thinking is that the whole string will always be there and I suspect he/she/it has got that from reading poor code written using loops to read from serial.


Thank you friends it helped me to complete. :slight_smile: