Otimizar codigo


#1

Caros amigos gostaria otimizar esse codigo.

if ((now.day() ==31 )&& (now.month() ==11)){{printStringWithShift("     Monday    ", 50);};};
if ((now.day() == 1)&& (now.month() ==11)){{printStringWithShift("      Tuesday", 50);};};
if ((now.day() == 2)&& (now.month() ==11)){{printStringWithShift("      Wednesday", 50);};};
if ((now.day() == 3)&& (now.month() ==11)){{printStringWithShift("     Thursday", 50);};};
if ((now.day() ==4 )&& (now.month() ==11)){{printStringWithShift("      Friday", 20);};};
if ((now.day() == 5)&& (now.month() ==11)){{printStringWithShift("      Saturday", 50);};};
if ((now.day() ==6 )&& (now.month() ==11)){{printStringWithShift("     Sunday     ", 50);};};
if ((now.day() ==7 )&& (now.month() ==11)){{printStringWithShift("    Monday     ", 50);};};
if ((now.day() == 8)&& (now.month() ==11)){{printStringWithShift("    Tuesday    ", 50);};};
if ((now.day() ==9 )&& (now.month() ==11)){{printStringWithShift("    Wednesday   ", 50);};};
if ((now.day() ==10 )&& (now.month() ==11)){{printStringWithShift("    Friday      ", 50);};};
if ((now.day() ==11 )&& (now.month() ==11)){{printStringWithShift("    Saturday    ", 50);};};
if ((now.day() == 12)&& (now.month() ==11)){{printStringWithShift("    Sunday      ", 50);};};
if ((now.day() == 13)&& (now.month() ==11)){{printStringWithShift("    Sunday     ", 50);};};
if ((now.day() == 14)&& (now.month() ==11)){{printStringWithShift("     Monday      ", 50);};};
if ((now.day() == 15)&& (now.month() ==11)){{printStringWithShift("    Tuesday        ", 50);};};
if ((now.day() == 16)&& (now.month() ==11)){{printStringWithShift("    Wednesday      ", 50);};};
if ((now.day() ==17 )&& (now.month() ==11)){{printStringWithShift("     Thursday    ", 50);};};
if ((now.day() ==18 )&& (now.month() ==11)){{printStringWithShift("    Friday         ", 50);};};
if ((now.day() ==19 )&& (now.month() ==11)){{printStringWithShift("    Saturday    ", 50);};};
if ((now.day() ==20 )&& (now.month() ==11)){{printStringWithShift("    Sunday      ", 50);};};
if ((now.day() ==21 )&& (now.month() ==11)){{printStringWithShift("    Monday     ", 50);};};
if ((now.day() ==22 )&& (now.month() ==11)){{printStringWithShift("    Tuesday     ", 50);};};
if ((now.day() ==23 )&& (now.month() ==11)){{printStringWithShift("    Wednesday     ", 50);};};
if ((now.day() ==24 )&& (now.month() ==11)){{printStringWithShift("    Thursday   ", 50);};};
if ((now.day() ==25 )&& (now.month() ==11)){{printStringWithShift("    Friday    ", 50);};};
if ((now.day() ==26 )&& (now.month() ==11)){{printStringWithShift("    Saturday   ", 50);};};
if ((now.day() ==27 )&& (now.month() ==11)){{printStringWithShift("     Sunday    ", 50);};};
if ((now.day() ==28 )&& (now.month() ==11)){{printStringWithShift("    Monday     ", 50);};};
if ((now.day() ==29 )&& (now.month() ==11)){{printStringWithShift("    Tuesday     ", 50);};};
if ((now.day() ==30 )&& (now.month() ==11)){{printStringWithShift("    Wednesday     ", 50);};};

if (now.hour() ==6){printStringWithShift("       Good morning!!    ", 50);}
if (now.hour() ==7){printStringWithShift("       Good morning!!    ", 50);}
if (now.hour() ==8){printStringWithShift("       Good morning!!    ", 50);}
if (now.hour() ==9){printStringWithShift("       Good morning!!     ", 50);}
if (now.hour() ==10){printStringWithShift("      Good morning!!     ", 50);}
if (now.hour() ==11){printStringWithShift("      Good morning!!     ", 50);}
if (now.hour() ==12){printStringWithShift("      Good morning!!     ", 50);}
if (now.hour() == 13){printStringWithShift("     Good afternoon!!    ", 50);}
if (now.hour() == 14){printStringWithShift("     Good afternoon!!    ", 50);}
if (now.hour() == 15){printStringWithShift("     Good afternoon!!    ", 50);}
if (now.hour() == 16){printStringWithShift("     Good afternoon!!    ", 50);}
if (now.hour() == 17){printStringWithShift("     Good afternoon!!    ", 50);}
if (now.hour() ==18){printStringWithShift("      Good night!!!!    ", 50);}
if (now.hour() ==19){printStringWithShift("      Good night!! !!    ", 50);}
if (now.hour() ==20){printStringWithShift("      Good night!!!!    ", 50);}
if (now.hour()==21){printStringWithShift("       Good night!!    ", 50);}
if (now.hour() ==22){printStringWithShift("      Good night!!"    ,50);}
if (now.hour() ==23){printStringWithShift("      Good night!!    ", 50);}
if (now.hour() ==00){printStringWithShift("      Good night!!    ", 50);}

#2

Olá @carlos1!

Segue a minha idéia para otimizar este código seu…

String get_day_of_week(uint8_t dow) {
    String dows=" ";
    switch(dow) {
        case 0:dows="Sunday";
        break;
        case 1:dows="Monday";
        break;
        case 2:dows="Tuesday";
        break;
        case 3:dows="Wednesday";
        break;
        case 4:dows="Thursday";
        break;
        case 5:dows="Friday";
        break;
        case 6:dows="Saturday";
        break;
    }
    return dows;
}

printStringWithShift(get_day_of_week(now.dayOfWeek()), 50);

String saudacao = " ";
if (now.hour() >=0 && now.hour() <=12) {
    saudacao = "Good morning!!";
} else if(now.hour() >=13 && now.hour()<=17) {
    saudacao = "Good afternoon!!";
} else if(now.hour() >= 19 && now.hour() <=23) {
    saudacao = "Good night!!";
}

printStringWithShift(saudacao, 50);

#3

Apenas considere usar for para evitar essa poluição visual.


#4

deu certo obrigado:+1: