Need assitance with code

Good Day I have written a program and everything is working as expected, the problem is I need to streamline the code as I am running out of space on the Arduino ethernet board. I need to add more functionality as well.I am relatively new to Arduino programing and I think my code can be shortened considerably.I would appreciate any suggestions in reducing the code.

I only posted the main sections of concern, because code wont fit in post

Many thanks

                //UDP OUTPUT START 
                if (packBuff[0] == 'B' && packBuff[2] == 'O')
                    {
                    // READ BOARD NO
                    if (packBuff[1] == '1'){BoardNo = 1;Serial.print("B");Serial.print(BoardNo);}
                    else if (packBuff[1] == '2'){BoardNo = 2;Serial.print("B");Serial.print(BoardNo);}
                    else if (packBuff[1] == '3'){BoardNo = 3;Serial.print("B");Serial.print(BoardNo);}
                    else if (packBuff[1] =='4'){BoardNo = 4;Serial.print("B");Serial.print(BoardNo);}
                    // READ PIN NO
                    if (packBuff[3] == '1'){PinNo = 1;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '2'){PinNo = 2;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '3'){PinNo = 3;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '4'){PinNo = 4;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '5'){PinNo = 5;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '6'){PinNo = 6;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '7'){PinNo = 7;Serial.print("0");Serial.print(PinNo);}
                    else if (packBuff[3] == '8'){PinNo = 8;Serial.print("0");Serial.print(PinNo);}
                    // SET PIN MODE
                    if (packBuff[4] == 'H'){pwmNo = 255; pwmLabel = 1; Serial.print("H");UdpOutput();}
                    else if (packBuff[4] == 'L'){pwmNo = 000; pwmLabel = 2; Serial.print("L");UdpOutput();}
                    else if (packBuff[4] == 'P'){pwmNo = pwmVal; pwmLabel = 3; Serial.print("P"); Serial.print(pwmNo); UdpOutput();}   
                }
                // UDP OUTPUT END
              
                //IDP IR TRIGER = IR01 - IR99 
                if (packBuff[0] == 'I' && packBuff[1] == 'R')
                {//Serial.print("IR");
                    // SER 1ST CARACTER 0 - 9
                         if (packBuff[2] == '0'){IR1 = 0;}
                    else if (packBuff[2] == '1'){IR1 = 1;}
                    else if (packBuff[2] == '2'){IR1 = 2;}
                    else if (packBuff[2] == '3'){IR1 = 3;}
                    else if (packBuff[2] == '4'){IR1 = 4;}
                    else if (packBuff[2] == '5'){IR1 = 5;}
                    else if (packBuff[2] == '6'){IR1 = 6;}
                    else if (packBuff[2] == '7'){IR1 = 7;}
                    else if (packBuff[2] == '8'){IR1 = 8;}
                    else if (packBuff[2] == '9'){IR1 = 9;}
                    
                    // SER 2D CARACTER 0 - 9
                         if (packBuff[3] == '0'){IR2 = 0;}
                    else if (packBuff[3] == '1'){IR2 = 1;}
                    else if (packBuff[3] == '2'){IR2 = 2;}
                    else if (packBuff[3] == '3'){IR2 = 3;}
                    else if (packBuff[3] == '4'){IR2 = 4;}
                    else if (packBuff[3] == '5'){IR2 = 5;}
                    else if (packBuff[3] == '6'){IR2 = 6;}
                    else if (packBuff[3] == '7'){IR2 = 7;}
                    else if (packBuff[3] == '8'){IR2 = 8;}
                    else if (packBuff[3] == '9'){IR2 = 9;}
                    UdpIR();
                }
void UdpOutput()
{
    if (BoardNo == 1){ioNo = PinNo - 1 ;}
    else if (BoardNo == 2){ioNo = PinNo + 7 ;}
    else if (BoardNo == 3){ioNo = PinNo + 15 ;}
    else if (BoardNo == 4){ioNo = PinNo + 23 ;}
    // output code
    ShiftPWM.SetOne(ioNo, pwmNo);
    //stringMsg = "hello";
       // Serial.print("Output Sent");//Serial.print(pwmlbl);//Write notification
        Udp.beginPacket(iPhoneIP, iPhonePort);
        Udp.write("Output Sent"); // Send Message back to iPhone
        Udp.endPacket();
void inputprint() 
{
 if (inPut == 0 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I1OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 0 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I1CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 1 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I2OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 1 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I2CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 2 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I3OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 2 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I3CL"); Udp.endPacket();} 
 delay(10);
 if (inPut == 3 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I4OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 3 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I4CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 4 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I5OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 4 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I5CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 5 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I6OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 5 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I6CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 6 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I7OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 6 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I7CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 7 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I8OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 7 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I8CL"); Udp.endPacket();}
}
if (IR1 == 0 && IR2 == 1){IR01();}
if (IR1 == 0 && IR2 == 2){IR02();}
if (IR1 == 0 && IR2 == 3){IR03();}
if (IR1 == 0 && IR2 == 4){IR04();}
if (IR1 == 0 && IR2 == 5){IR05();}
if (IR1 == 0 && IR2 == 6){IR06();}
if (IR1 == 0 && IR2 == 7){IR07();}
if (IR1 == 0 && IR2 == 8){IR08();}
}// 40 codes
void IR05(){cli();for (int i = 0; i < 1; i++) {irsend.sendRaw(raw1,100,38);delay(80);}sei();Udp.beginPacket(iPhoneIP, iPhonePort);Udp.write("IR05S");Udp.endPacket();Serial.write("IR05S");irrecv.enableIRIn();}//sinoteck TV ON/Off
void IR06(){cli();for (int i = 0; i < 1; i++) {irsend.sendRaw(raw1,100,38);delay(80);}sei();Udp.beginPacket(iPhoneIP, iPhonePort);Udp.write("IR06S");Udp.endPacket();Serial.write("IR06S");irrecv.enableIRIn();}//sinoteck TV ON/Off

It would be better to see the whole program. Include it as an attachment to a post - see Additional options when creating a reply.

Better to zip up the whole sketch and post it as an attachment to a post. Looking very quickly, I don't see any huge red flags in what you did post.

What kind of space are you running out of exactly? There are at least a couple different kinds. If there are a lot of literals, e.g.

Udp.write("I1CL");

Those can use a lot of SRAM. Move them to flash with

Udp.write( F("I1CL") );

This

                    // SER 2D CARACTER 0 - 9
                         if (packBuff[3] == '0'){IR2 = 0;}
                    else if (packBuff[3] == '1'){IR2 = 1;}
                    else if (packBuff[3] == '2'){IR2 = 2;}
                    else if (packBuff[3] == '3'){IR2 = 3;}
                    else if (packBuff[3] == '4'){IR2 = 4;}
                    else if (packBuff[3] == '5'){IR2 = 5;}
                    else if (packBuff[3] == '6'){IR2 = 6;}
                    else if (packBuff[3] == '7'){IR2 = 7;}
                    else if (packBuff[3] == '8'){IR2 = 8;}
                    else if (packBuff[3] == '9'){IR2 = 9;}

can be replaced by

IR2= packBuff[3]-'0';

subtracting '0' from a char in the range '0'..'9' gets you the number.

Also

this lot

void inputprint() 
{
 if (inPut == 0 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I1OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 0 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I1CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 1 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I2OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 1 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I2CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 2 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I3OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 2 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I3CL"); Udp.endPacket();} 
 delay(10);
 if (inPut == 3 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I4OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 3 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I4CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 4 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I5OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 4 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I5CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 5 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I6OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 5 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I6CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 6 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I7OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 6 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I7CL"); Udp.endPacket();}
 delay(10);
 if (inPut == 7 && instate == 0){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I8OP"); Udp.endPacket();}
 delay(10);
 if (inPut == 7 && instate == 1){Udp.beginPacket(iPhoneIP, iPhonePort); Udp.write("I8CL"); Udp.endPacket();}
}

Can become

void inputprint() 
{
    Udp.beginPacket(iPhoneIP, iPhonePort);
    Udp.write("I");
    Udp.write(input+1+'0');// well if subtracting '0' gets you the number then adding it to the number gets you?
    if (istate == 0)
        Udp.write("OP")
    else
       Udp.write("CL");
    Udp.endPacket()
}

Mark

Thanks for the reply,s so far.

I have attached the sketch, I am planing to try and move the IR codes to the sdcard if I can get the sdfat library to read a specific line in a file.
I am running out of flash memory The program is currently 35K in size.I would realy like to use the arduino ethernet, but might need to use the mega.

Thanks

test.ino (42.6 KB)

First try what holmes4 just posted. I'm curious why is there a delay after every line even if the part inside the condition was never executed in inputprint. Anyways, inputprint and UdpIR could both be restructured so that the first one takes inPut and instate as input parameters, and the second would take IR1 and IR2. No need for 40 functions that do practically the same thing. Only one call is different in each, and that's the part you parametrize. I also don't see the point of the for loop in each of the 40 functions unless you meant to repeat it more than once later in development. You can save several kilobytes. Serial library seems to take several kilobytes alone though.

Huge duplications of code as already pointed out. Need to learn some programming techniques; let the machine do the work for you. The forty rawXX arrays account for 8000 bytes alone, they will easily cause the SRAM on even a Mega2560 to be exceeded.

The functions below go on and on and on (I didn't count how many, but just copied the first few) -- these can probably be replaced by a single function. They appear to be identical except for "IR05S", "IR06S", etc. Learn to pass parameters to functions. Pass the argument either as a string "IR05S" or as an integer (5) and build the needed string in the function. So the call would be like IR(5).

void IR05(){
    cli();
    for (int i = 0; i < 1; i++) {
        irsend.sendRaw(raw1,100,38);
        delay(80);
    }
    sei();
    Udp.beginPacket(iPhoneIP, iPhonePort);
    Udp.write("IR05S");
    Udp.endPacket();
    Serial.write("IR05S");
    irrecv.enableIRIn();
}//sinoteck TV ON/Off
void IR06(){
    cli();
    for (int i = 0; i < 1; i++) {
        irsend.sendRaw(raw1,100,38);
        delay(80);
    }
    sei();
    Udp.beginPacket(iPhoneIP, iPhonePort);
    Udp.write("IR06S");
    Udp.endPacket();
    Serial.write("IR06S");
    irrecv.enableIRIn();
}//sinoteck TV ON/Off
void IR07(){
    cli();
    for (int i = 0; i < 1; i++) {
        irsend.sendRaw(raw1,100,38);
        delay(80);
    }
    sei();
    Udp.beginPacket(iPhoneIP, iPhonePort);
    Udp.write("IR07S");
    Udp.endPacket();
    Serial.write("IR07S");
    irrecv.enableIRIn();
}//sinoteck TV ON/Off

Thanks for the reply's so far. Still have a lot to learn.

I will go through my code and start making changes.

I have just another question related to UDP

Udp accept char's is there a way to join values together to for one char ie.

a = "hello"
b = "there"

and then joining them together to form 1 message
a + b = "hellothere"

I know it can be done with strings but how do I convert that to a char for input as a udp message.?

If I can do that I might be able to move all my udp and serial messaging to one function

thanks again

Udp accept char's is there a way to join values together to for one char ie.

No. A char variable holds ONE character.

I know it can be done with strings but how do I convert that to a char for input as a udp message.?

You don't. You pay attention to the fact that a UDP message contains a string, not a char (the type is probably pointer to char, not char). A pointer to char is nearly identical to an array. And a string is simply another name for a NULL terminated array of chars. So, string, pointer to char, and char array are nearly always interchangeable.

Strings can be combined, using strcat().