Well I am trying make an application with arduino leonardo and GSM Shield. There are conditions if, else if, else .. etc
if (smsMetni.indexOf("DURUM") != -1) {
Serial.println("SMS income");
String temp= String(analogRead(LM35_pin) * 0.48828125);
String temp2 = "Hava " + temp + " derece.";
char tempSMS2[] = "";
temp2.toCharArray(tempSMS2, 30);
Serial.println(tempSMS2);
Kapadokya.smsGonder(gonderilecekTelNumarasi, tempSMS2);
}
After the last line "Kapadokya.smsGonder(gonderilecekTelNumarasi, tempSMS2);" program stopping I watching serial monitor there is no notification or error. just stopping.
If I change those lines and remove String(analogRead(LM35_pin) * 0.48828125) :
String temp= String(analogRead(LM35_pin) * 0.48828125);
String temp2 = "Hava " + temp + " derece.";
and just write like this, it works very well.
String temp2 = "Hava derece.";
maybe you think there is a problem with LM35, even if I change it String(analogRead(LM35_pin) * 0.48828125); with String(30);, it doesn't work again. after the last line it is not continue.
only one sollution I've found if add delay(500) after each line it works but my codes looks ugly with this. are there any solution.
-
2Without seeing the whole program code I suspect you are running out of memory because you are using the String class extensively. Try NOT to combine strings into a new string object as you need twice the space and then only copy its content into a TOO SMALL tempSMS2 array that has the size of 1 (the terminating null character). Just serial print the string literals as you have them at hand. Also use the L-Macro to keep the string literals in program memory and not in RAM. Serial.print is also perfectly capable to print numbers. No need to convert them into String first.Kwasmich– Kwasmich07/28/2020 06:28:37Commented Jul 28, 2020 at 6:28
1 Answer 1
You should avoid String on the Arduino. It will cause random crashes. However that's not your issue. Your issue is trying to copy the data into an array that is too small.
char tempSMS2[] = "";
That creates a one byte array which contains just a 0円
. You then try and squeeze your String data into it and it of course overflows all over the rest of your variables and data.
A better solution is to work directly in char arrays. It is a little trickier though:
First convert your float to a string:
char tempFloat[8];
dtostrf(analogRead(LM35_pin) * 0.48828125, -7, 3, tempFloat);
Then build up your string:
char tempSMS2[30];
strcpy_P(tempSMS2, PSTR("Hava "));
strcat(tempSMS2, tempFloat);
strcat_P(tempSMS2, PSTR(" derece."));
-
Wow. it looks amazing. but if add Serial.println(tempSMS2); at the last line only printing " derece". can you fix it.mehmet– mehmet07/28/2020 10:49:06Commented Jul 28, 2020 at 10:49
-
Oops, that last strcpy_P should have been a strcat_P.Majenko– Majenko07/28/2020 10:49:46Commented Jul 28, 2020 at 10:49
-
(PGM_P)F("foo")
can be written more simplyPSTR("foo")
. Actually, theF()
macro usesPSTR()
.Edgar Bonet– Edgar Bonet07/28/2020 11:04:49Commented Jul 28, 2020 at 11:04 -
@EdgarBonet Yeah, I can never remember which is which of these macros. TBH I never use them, but then I never use AVR unless the client specifically requires it.Majenko– Majenko07/28/2020 11:51:43Commented Jul 28, 2020 at 11:51
Explore related questions
See similar questions with these tags.