Optimizing code to reduce memory consumption

Hey!

I wrote the following code to control my TV (and a few other things) using the internet but the sketch was too big!
So i omitted plenty of features i would have loved to have,yet the sketch is still too big.
This is my code,(attached)
How do I optimize it? I think that the StrContain function uses alot of memory but i cant find another way to read a GET Request.
Can I store my code on the SD card i'm using?
BTW a huge chunk of memory is consumed by the libraries, is there anyway to reduce that?

Thx

homeauto.ino (15 KB)

Sketch is too large how? Too large for program memory? Or uses too much SRAM?

unsigned int powerOn[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 550, 1700, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 550, 550, 600, 1650, 550, 1650, 600, 1650, 550, 1650, 550, 1650, 600, 1650, 550}; 
unsigned int b1[68] = {4450, 4450, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 600, 500, 600, 1650, 550, 1650, 600, 1650, 550, 1650, 600, 1650, 550}; 
unsigned int b2[68] = {4450, 4500, 550, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 550, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 1650, 600, 500, 600, 1650, 550, 1650, 600, 1650, 550, 1650, 550, 1700, 550};
unsigned int b3[68] = {4500, 4450, 550, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 600, 550, 550, 550, 550, 550, 550, 1650, 600, 1650, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 550, 550, 1650, 550, 550, 600, 500, 600, 1650, 550, 1650, 600, 1650, 550, 1650, 600, 1650, 550};
unsigned int b4[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 550, 550, 550, 600, 500, 600, 1650, 550, 550, 550, 550, 600, 550, 550, 550, 550, 1650, 600, 1650, 550, 1650, 550, 550, 600, 1650, 550, 1650, 550, 1700, 550, 1650, 550};
unsigned int b5[68] = {4500, 4450, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 600, 1600, 600, 1650, 550, 550, 600, 500, 600, 550, 550, 550, 550, 550, 600, 1600, 600, 550, 550, 550, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 500, 600, 1650, 550, 1650, 600, 550, 550, 1650, 550, 1650, 600, 1650, 550, 1650, 600}; 
unsigned int b6[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 550, 550, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 550, 1700, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 550, 550, 1650, 550, 550, 550, 550, 600, 550, 550, 550, 550, 1650, 600, 550, 550, 1650, 550, 550, 600, 1650, 550, 1650, 550, 1650, 600, 1650, 550};
unsigned int b7[68] = {4500, 4450, 600, 1650, 550, 1650, 550, 1700, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 550, 1700, 550, 1650, 550, 550, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 550, 550, 1650, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 1650, 550, 1650, 600, 1650, 550, 1650, 600};
unsigned int b8[68] = {4500, 4450, 600, 1600, 600, 1650, 550, 1650, 600, 500, 600, 500, 600, 550, 550, 550, 550, 550, 600, 1650, 600, 1600, 550, 1650, 600, 550, 550, 550, 550, 550, 600, 500, 600, 550, 600, 1600, 550, 550, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 550, 550, 550, 600, 1650, 550, 1650, 550, 1650, 600, 1650, 550};
unsigned int b9[68] = {4500, 4450, 600, 1650, 550, 1650, 550, 1650, 600, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 550, 1650, 600, 1650, 550, 550, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 550, 550, 600, 500, 600, 1650, 550, 550, 600, 500, 600, 550, 550, 1650, 550, 1650, 600, 1650, 550, 1650, 600};
unsigned int b0[68] = {4500, 4450, 600, 1600, 600, 1650, 600, 1600, 550, 550, 600, 500, 600, 550, 600, 500, 600, 500, 600, 1650, 550, 1650, 600, 1600, 600, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 600, 500, 600, 500, 600, 550, 600, 1600, 550, 550, 600, 550, 550, 550, 550, 550, 550, 1650, 600, 1650, 600, 1600, 600, 500, 600, 1650, 550, 1650, 600, 1650, 550};
unsigned int vlUp[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 550, 550, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 1650, 600, 1650, 550, 1650, 550, 550, 600, 550, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 550, 550, 1650, 600, 1650, 550, 1650, 600, 1650, 550, 1650, 550}; 
unsigned int vlDwn[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 500, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 1650, 550, 550, 550, 1650, 600, 550, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 1650, 600, 500, 600, 1650, 550, 1650, 600, 1650, 550, 1650, 600};
unsigned int chUp[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 550, 550, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 550, 1700, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 550, 550, 550, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 1650, 600, 550, 550, 1650, 550, 1650, 600, 550, 550, 1650, 550, 1700, 550, 1650, 550};
unsigned int chDwn[68] = {4500, 4450, 550, 1650, 600, 1650, 550, 1650, 600, 500, 600, 550, 550, 550, 550, 550, 550, 550, 600, 1650, 550, 1650, 600, 1650, 550, 550, 550, 550, 550, 550, 600, 550, 550, 550, 550, 550, 550, 550, 600, 500, 600, 550, 550, 1650, 550, 550, 600, 550, 550, 550, 550, 1650, 550, 1700, 550, 1650, 550, 1650, 600, 550, 550, 1650, 550, 1700, 550, 1650, 550};

This stuff all belongs in PROGMEM.

Learn to use the F() macro to keep string literals out of SRAM:
Serial.println(F("Initializing SD card..."));

Can I store my code on the SD card i'm using?

No. Code is compiled. What actually runs looks nothing like what you write, and the processor can not access machine instructions from anywhere else.

BTW a huge chunk of memory is consumed by the libraries, is there anyway to reduce that?

You are wrong about that. Even if you were right, only the functions that are actually used are linked in. You can only reduce program memory be not using functions.

The arrays you use, use ints
however looking at the values they are all multiple of 50.

making arrays of bytes (1/50 of the current value) and multiply by 50 when needed
will reduce array size by half at the cost of some performance.

Your tables would be half the size if you factored-out the multiplication constant of 50, but yes, flash is where they belong.

Edit: Ninja'ed

Hey!
From: PaulS

This stuff all belongs in PROGMEM.

Learn to use the F() macro to keep string literals out of SRAM:
Serial.println(F("Initializing SD card..."));

First of all, what is PROGMEM?
From what i understood Arduino has "functional" and "Static" memory and PROGMEM is a part of its' functional memory(?).And btw i can only use F() macro with strings which I don't plenty of.
And also if i compile my Sketch with only the libraries included (nothing else) it takes about 62% of global variables!

From: robtillaart

making arrays of bytes (1/50 of the current value) and multiply by 50 when needed
will reduce array size by half at the cost of some performance.

I divided all the elements in my array by 50, but when i change the format from "unsigned int" to "unsigned byte" the compiler throws an error "expected initializer before byte".
how do i change the arrays into a byte format?

Thank you all for replying!

byte is already unsigned, so remove the latter.

From what i understood Arduino has "functional" and "Static" memory

It has Flash and SRAM. Code goes in Flash. All data goes in Flash, too. Some of it is copied to SRAM at run time, like all those arrays. You can stop that by using the PROGMEM directive, but you then need to change how you access the data.

First of all, what is PROGMEM?

Look at the reference pages.

And also if i compile my Sketch with only the libraries included (nothing else) it takes about 62% of global variables!

If you use HardwareSerial, there are input and output buffers for each instance. If you use the SD library, there is a buffer (512 bytes) for each file. You can not use the libraries and not use the memory.

Hey!

Thx alot for your advice and info everybody,but even after modifying my code it is still too big for the arduino.
So i was wondering how can i use the F() to cram more data(different formats) into PROGRAMEM?
Here's my code:(attached)
As you can see,I have omitted lot of features including the LCD.

homeauto.ino (13.9 KB)

byte pwrOn[68] = {90, 89, 50, 33, 12, 33, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 11, 34, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 11, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 11, 11, 12, 33, 11, 33, 12, 33, 11, 33, 11, 33, 12, 33, 11}; 
byte b1[68] = {89, 89, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 12, 10, 12, 11, 11, 11, 11, 33, 12, 33, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 12, 10, 12, 33, 11, 33, 12, 33, 11, 33, 12, 33, 11};
byte b2[68] = {89, 8.2, 11, 33, 11, 33, 12, 33, 11, 11, 11, 11, 12, 10, 12, 11, 11, 11, 11, 33, 12, 33, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 11, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 33, 12, 10, 12, 33, 11, 33, 12, 33, 11, 33, 11, 34, 11};
byte b3[68] = {8.2, 89, 11, 33, 11, 33, 12, 33, 11, 11, 11, 11, 12, 11, 11, 11, 11, 11, 11, 33, 12, 33, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 33, 11, 11, 12, 10, 12, 33, 11, 33, 12, 33, 11, 33, 12, 33, 11};
byte b4[68] = {8.2, 89, 11, 33, 12, 33, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 12, 10, 12, 33, 11, 11, 11, 11, 12, 11, 11, 11, 11, 33, 12, 33, 11, 33, 11, 11, 12, 33, 11, 33, 11, 34, 11, 33, 11};
byte b5[68] = {8.2, 89, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 12, 112, 12, 33, 11, 11, 12, 10, 12, 11, 11, 11, 11, 11, 12, 112, 12, 11, 11, 11, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 10, 12, 33, 11, 33, 12, 11, 11, 33, 11, 33, 12, 33, 11, 33, 12}; 
byte b6[68] = {8.2, 89, 11, 33, 12, 33, 11, 33, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 11, 34, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 11, 11, 33, 12, 11, 11, 33, 11, 11, 11, 11, 12, 11, 11, 11, 11, 33, 12, 11, 11, 33, 11, 11, 12, 33, 11, 33, 11, 33, 12, 33, 11};
byte b7[68] = {8.2, 89, 12, 33, 11, 33, 11, 34, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 11, 34, 11, 33, 11, 11, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 33, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 12, 10, 12, 11, 11, 33, 11, 33, 12, 33, 11, 33, 12};
byte b8[68] = {8.2, 89, 12, 112, 12, 33, 11, 33, 12, 10, 12, 10, 12, 11, 11, 11, 11, 11, 12, 33, 12, 112, 11, 33, 12, 11, 11, 11, 11, 11, 12, 10, 12, 11, 12, 112, 11, 11, 12, 33, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 11, 11, 11, 12, 33, 11, 33, 11, 33, 12, 33, 11};
byte b9[68] = {8.2, 89, 12, 33, 11, 33, 11, 33, 12, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 11, 33, 12, 33, 11, 11, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 11, 11, 12, 10, 12, 33, 11, 11, 12, 10, 12, 11, 11, 33, 11, 33, 12, 33, 11, 33, 12};
byte b0[68] = {8.2, 89, 12, 112, 12, 33, 12, 112, 11, 11, 12, 10, 12, 11, 12, 10, 12, 10, 12, 33, 11, 33, 12, 112, 12, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 12, 10, 12, 10, 12, 11, 12, 112, 11, 11, 12, 11, 11, 11, 11, 11, 11, 33, 12, 33, 12, 112, 12, 10, 12, 33, 11, 33, 12, 33, 11};
byte vlUp[68] = {8.2, 89, 11, 33, 12, 33, 11, 33, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 33, 12, 33, 11, 33, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 11, 11, 33, 12, 33, 11, 33, 12, 33, 11, 33, 11}; 
byte vlDwn[68] = {8.2, 89, 11, 33, 12, 33, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 10, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 12, 10, 12, 11, 11, 11, 11, 33, 12, 33, 11, 11, 11, 33, 12, 11, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 33, 12, 10, 12, 33, 11, 33, 12, 33, 11, 33, 12};
byte chUp[68] = {8.2, 89, 11, 33, 12, 33, 11, 33, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 11, 34, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 11, 11, 33, 12, 11, 11, 11, 11, 33, 12, 10, 12, 11, 11, 11, 11, 33, 12, 11, 11, 33, 11, 33, 12, 11, 11, 33, 11, 34, 11, 33, 11};
byte chDwn[68] = {8.2, 89, 11, 33, 12, 33, 11, 33, 12, 10, 12, 11, 11, 11, 11, 11, 11, 11, 12, 33, 11, 33, 12, 33, 11, 11, 11, 11, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 12, 10, 12, 11, 11, 33, 11, 11, 12, 11, 11, 11, 11, 33, 11, 34, 11, 33, 11, 33, 12, 11, 11, 33, 11, 34, 11, 33, 11};

15 * 68 bytes of precious RAM wasted.
Almost exactly half of it.

byte b3[68] = {8.2,

"byte" is an integer datatype.

client.println F(("Content-Type: text/html"));

What?

Try this for help:

http://www.ebay.com/itm/MEGA-2560-R3-Board-ATmega2560-16AU-CH340G-USB-Cable-for-Arduino-/121639277014?hash=item1c524411d6