Arduino mega freezes

Hi, i am having issues with my arduino mega and my code. I am uploading my code on it and if i try to control it from my android app that i have built from app inventor it is getting frozen. Do you think that i could do something in order to use less of its physical memory or from the dynamic memory?

xeirismos_meso_ethernet.ino (22 KB)

Do you think that i could do something in order to use less of its physical memory

Yes. All of your pin numbers are ints, but they hold values that would fit in a byte.

Using floats for pin numbers is just silly. You can't connect a device to pin 3.14159, so why pretend that that is possible?

String readString = String(52);

That is equivalent to:

String readString = "52";

How is that remotely a reasonable initial value? Why are you pissing away resources on Strings anyway?

You can keep string literals out of SRAM by using the F() macro:
client.println (F("HTTP/1.1 200 OK"));

so i should use float instead of ints?

I didn't understand what you mean about the string(52)

PaulS:
Yes. All of your pin numbers are ints, but they hold values that would fit in a byte.

Using floats for pin numbers is just silly. You can't connect a device to pin 3.14159, so why pretend that that is possible?

String readString = String(52);

That is equivalent to:

String readString = "52";

How is that remotely a reasonable initial value? Why are you pissing away resources on Strings anyway?

You can keep string literals out of SRAM by using the F() macro:
client.println (F("HTTP/1.1 200 OK"));

so i should use float instead of ints?

Now you want to use 4 byte sized variables to hold values that a single byte sized.

No, I think you should use long long, so you waste 7 bytes per variable.

I didn’t understand what you mean about the string(52)

What, EXACTLY, does String(52) do? Why do you want to do that?

It is NOT reserving space for 52 characters…

after doing what you said with the F() macro i am getting this error

C:\Users\Ξ Ξ­Ο„Ο?ΞΏΟ‚\Desktop\arduino\xeirismos_meso_ethernet\xeirismos_meso_ethernet.ino:615:24: note: in expansion of macro ‘F’

C:\Users\Πέτρος\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.13\cores\arduino/WString.h:38:74: error: array must be initialized with a brace-enclosed initializer

#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

so please, could you give me an example so that i will continue writing??

I am trying to finish my thesis....

after doing what you said with the F() macro i am getting this error

You had some code already that used the F() macro:

          client.println(F("<div style=\"float:left; width:400px; border:ridge;   margin-right:10px ; margin-bottom:10px;  background-color:#E6E13E       \">"));

So, whatever mysterious changes you made are not the source of the error, IF you made the changes correctly.

Since there IS an error, on some unspecified line of code in some file you didn’t post, I can only surmise that you did not make the changes correctly.

here it is!!! :slight_smile: :slight_smile:

xeirismos_meso_ethernet.ino (22.5 KB)

          client.println (F());

The F() macro needs an argument. If you are just printing a blank line (as this code does), you don’t have a string literal that you need to keep out of SRAM.

          client.println (F("<button type=\"submit\" style=\"width:200px;\">Fos_Kouzina - "+statusLed+"</button> "));

Here, you have TWO string literals AND two uses of the String class’ concatenation operator. You can NOT operate on string literals that are not in SRAM using the concatenation operator. This horrid waste of resources needs to be two client.print() statements and a client.println() statement.

so could you please write me for the first paragraph how it is supposed to be written?

gibsonlp:
so could you please write me for the first paragraph how it is supposed to be written?

Of how what is supposed to be written?

If you are asking about the first statement, and how it should be written:

          client.println ();

no i am asking for the second statement :slight_smile: as for the

client.println (F("<button type=“submit” style=“width:200px;”>Fos_Kouzina - “+statusLed+” "));

just split that into 3/as many client.print as you need. One per each string, one per each variable.

Don’t forget to do print for the first ones so that there is no carriage return and a println for the last one.

client.print (F("<button type=\"submit\" style=\"width:200px;\">Fos_Kouzina - "));
client.print (statusLed);
client.println(F("</button>"));

so something like that?

client.println (F("<button type=“submit” style=“width:200px;”>))
client.println (F(Fos_Kouzina - “+statusLed+”))
client.println (F( "));

probably not…

F(Fos_Kouzina - "+statusLed+")
F(</button> ")

will make the compiler unhappy.

client.print (F("<button type=\"submit\" style=\"width:200px;\">Fos_Kouzina - "));
client.print (statusLed);
client.println(F("</button>"));

I typed that quickly - so whatever is part of a string, just put that as a string. and print the value where needed

gibsonlp:
so something like that?

client.println (F("<button type=“submit” style=“width:200px;”>))
client.println (F(Fos_Kouzina - “+statusLed+”))
client.println (F( "));

No. J-M-L already showed the correct way (except for forgetting the F() macro in the last statement).

corrected - thx Paul. I had also forgotten some ()

is Fos_Kouzina a variable you want to print? or just the label of the button with a value next to it??

just the label of the button

but that’s what i got after the change that i’ve made for the first button

How does the text in the picture differ from what you expect?