Tuesday 15 May 2012

optimization - Optimisation of Arduino sketch -> using for or while loops instead of switch -



optimization - Optimisation of Arduino sketch -> using for or while loops instead of switch -

i created arduino sketch, utilize switch on or off 2 sets of leds. thought of programme run in cycles number of steps in each cycle. far, have written sketch in way can define 12 different steps (see code below), adding farther steps complicated , needs many lines of code. wondering, if of help in coming way replace switch cases loop iterating on individual steps. since programming skills basic, thankful suggestions improving code.

parameter definition each step:

unsigned long cycles = 1000; // number of cycles byte steps = 4; // number of steps repeat in cycles unsigned long timebasis = 1000; // factor multiply ms set time basis -> 1 results in 1 ms time basis, 1000 results in 1 s etc //step 1 #define s1_intensity_ledcolor1 1000 // intensity 660 nm @ step 1 #define s1_intensity_ledcolor2 0 // intensity 740 nm @ step 1 #define s1_duration 5 //length of step 1 //step 2 #define s2_intensity_ledcolor1 0 // intensity 660 nm @ step 2 #define s2_intensity_ledcolor2 0 // intensity 740 nm @ step 2 #define s2_duration 2 // length of step 2 //step 3 #define s3_intensity_ledcolor1 0 // intensity 660 nm @ step 3 #define s3_intensity_ledcolor2 1000 // intensity 740 nm @ step 3 #define s3_duration 1 // length of step 3 //step 4 #define s4_intensity_ledcolor1 0 // intensity 660 nm @ step 4 #define s4_intensity_ledcolor2 0 // intensity 740 nm @ step 4 #define s4_duration 4 // length of step 4 //step 5 #define s5_intensity_ledcolor1 100 // intensity 660 nm @ step 5 #define s5_intensity_ledcolor2 1000 // intensity 740 nm @ step 5 #define s5_duration 50 // length of step 5 //step 6 #define s6_intensity_ledcolor1 0 // intensity 660 nm @ step 6 #define s6_intensity_ledcolor2 1000 // intensity 740 nm @ step 6 #define s6_duration 80 // length of step 6 //step 7 #define s7_intensity_ledcolor1 300 // intensity 660 nm @ step 7 #define s7_intensity_ledcolor2 0 // intensity 740 nm @ step 7 #define s7_duration 50 // length of step 7 //step 8 #define s8_intensity_ledcolor1 0 // intensity 660 nm @ step 8 #define s8_intensity_ledcolor2 0 // intensity 740 nm @ step 8 #define s8_duration 60 // length of step 8 //step 9 #define s9_intensity_ledcolor1 0 // intensity 660 nm @ step 9 #define s9_intensity_ledcolor2 1000 // intensity 740 nm @ step 9 #define s9_duration 70 // length of step 9 //step 10 #define s10_intensity_ledcolor1 700 // intensity 660 nm @ step 10 #define s10_intensity_ledcolor2 0 // intensity 740 nm @ step 10 #define s10_duration 30 // length of step 10 //step 11 #define s11_intensity_ledcolor1 0 // intensity 660 nm @ step 11 #define s11_intensity_ledcolor2 0 // intensity 740 nm @ step 11 #define s11_duration 80 // length of step 11 //step 12 #define s12_intensity_ledcolor1 1000 // intensity 660 nm @ step 12 #define s12_intensity_ledcolor2 1000 // intensity 740 nm @ step 12 #define s12_duration 90 // length of step 12 timer1.initialize(timebasis*1000); // setting time base of operations timer1.attachinterrupt(dosomething); // execute function "dosomething" every x µs based on time base of operations void dosomething() { counter++; }

sum individual step lengths:

#define s1_dur s1_duration #define s2_dur (s1_duration + s2_duration) #define s3_dur (s1_duration + s2_duration + s3_duration) #define s4_dur (s1_duration + s2_duration + s3_duration + s4_duration) #define s5_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration) #define s6_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration) #define s7_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration + s7_duration) #define s8_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration + s7_duration + s8_duration) #define s9_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration + s7_duration + s8_duration + s9_duration) #define s10_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration + s7_duration + s8_duration + s9_duration + s10_duration) #define s11_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration + s7_duration + s8_duration + s9_duration + s10_duration + s11_duration) #define s12_dur (s1_duration + s2_duration + s3_duration + s4_duration + s5_duration + s6_duration + s7_duration + s8_duration + s9_duration + s10_duration + s11_duration + s12_duration)

step functions:

void step1() { if(debug == 1) { time_unit(s1_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s1_intensity_ledcolor1); lcd.print(s1_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s1_intensity_ledcolor2); lcd.print(s1_intensity_ledcolor2); } setcolor(color1, s1_intensity_ledcolor1); setcolor(color2, s1_intensity_ledcolor2); } void step2() { if(debug == 1) { time_unit(s2_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s2_intensity_ledcolor1); lcd.print(s2_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s2_intensity_ledcolor2); lcd.print(s2_intensity_ledcolor2); } setcolor(color1, s2_intensity_ledcolor1); setcolor(color2, s2_intensity_ledcolor2); } void step3() { if(debug == 1) { time_unit(s3_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s3_intensity_ledcolor1); lcd.print(s3_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s3_intensity_ledcolor2); lcd.print(s3_intensity_ledcolor2); } setcolor(color1, s3_intensity_ledcolor1); setcolor(color2, s3_intensity_ledcolor2); } void step4() { if(debug == 1) { time_unit(s4_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s4_intensity_ledcolor1); lcd.print(s4_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s4_intensity_ledcolor2); lcd.print(s4_intensity_ledcolor2); } setcolor(color1, s4_intensity_ledcolor1); setcolor(color2, s4_intensity_ledcolor2); } void step5() { if(debug == 1) { time_unit(s5_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s5_intensity_ledcolor1); lcd.print(s5_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s5_intensity_ledcolor2); lcd.print(s5_intensity_ledcolor2); } setcolor(color1, s5_intensity_ledcolor1); setcolor(color2, s5_intensity_ledcolor2); } void step6() { if(debug == 1) { time_unit(s6_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s6_intensity_ledcolor1); lcd.print(s6_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s6_intensity_ledcolor2); lcd.print(s6_intensity_ledcolor2); } setcolor(color1, s6_intensity_ledcolor1); setcolor(color2, s6_intensity_ledcolor2); } void step7() { if(debug == 1) { time_unit(s7_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s7_intensity_ledcolor1); lcd.print(s7_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s7_intensity_ledcolor2); lcd.print(s7_intensity_ledcolor2); } setcolor(color1, s7_intensity_ledcolor1); setcolor(color2, s7_intensity_ledcolor2); } void step8() { if(debug == 1) { time_unit(s8_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s8_intensity_ledcolor1); lcd.print(s8_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s8_intensity_ledcolor2); lcd.print(s8_intensity_ledcolor2); } setcolor(color1, s8_intensity_ledcolor1); setcolor(color2, s8_intensity_ledcolor2); } void step9() { if(debug == 1) { time_unit(s9_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s9_intensity_ledcolor1); lcd.print(s9_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s9_intensity_ledcolor2); lcd.print(s9_intensity_ledcolor2); } setcolor(color1, s9_intensity_ledcolor1); setcolor(color2, s9_intensity_ledcolor2); } void step10() { if(debug == 1) { time_unit(s10_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s10_intensity_ledcolor1); lcd.print(s10_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s10_intensity_ledcolor2); lcd.print(s10_intensity_ledcolor2); } setcolor(color1, s10_intensity_ledcolor1); setcolor(color2, s10_intensity_ledcolor2); } void step11() { if(debug == 1) { time_unit(s11_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s11_intensity_ledcolor1); lcd.print(s11_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s11_intensity_ledcolor2); lcd.print(s11_intensity_ledcolor2); } setcolor(color1, s11_intensity_ledcolor1); setcolor(color2, s11_intensity_ledcolor2); } void step12() { if(debug == 1) { time_unit(s12_duration); lcd.setcursor(0,1); lcd.print("c1:"); fill_up_4(s12_intensity_ledcolor1); lcd.print(s12_intensity_ledcolor1); lcd.print("|c2:"); fill_up_4(s12_intensity_ledcolor2); lcd.print(s12_intensity_ledcolor2); } setcolor(color1, s12_intensity_ledcolor1); setcolor(color2, s12_intensity_ledcolor2); } void laststep() { setcolor(color1, 0); setcolor(color2, 0); counter = 0; cycle_counter++; }

accessory functions:

//----------------------- checks digits of value , fills total of 4 digits spaces ------ void fill_up_4(int value_length) { if(value_length < 10) // value smaller 10, write 3 spaces lcd { lcd.print(" "); } else if(value_length < 100) // value smaller 100, write 2 spaces lcd { lcd.print(" "); } else if(value_length < 1000) // value smaller 1000, write 1 spaces lcd { lcd.print(" "); } } //----------------------- checks time value , sets time unit accordingly ------------------------ void time_unit(unsigned long time_value) { unsigned long basecorrectedtime = time_value * timebasis; if(basecorrectedtime < 1000) // value smaller 1 sec -> display in ms { lcd.print(basecorrectedtime/1000); lcd.print("ms"); } else if(basecorrectedtime < 60000) // value smaller 1 min -> display in sec { lcd.print(basecorrectedtime/(float)1000); // (float) results in decimal numbers lcd.print(" s"); } else if(basecorrectedtime < 3600000) // value smaller 1 hr -> display in min { lcd.print(basecorrectedtime/(float)60000); lcd.print(" m"); } else // value bigger 1 hour, display in hours { lcd.print(basecorrectedtime/(float)3600000); lcd.print(" h"); } }

main switch case:

switch(steps) { case 1: switch(counter) // 1 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/1| "); } step1(); break; case s1_dur: laststep(); break; } case 2: switch(counter) // 2 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/2| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/2| "); } step2(); break; case s2_dur: laststep(); break; } break; case 3: switch(counter) // 3 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/3| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/3| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/3| "); } step3(); break; case s3_dur: laststep(); break; } break; case 4: switch(counter) // 4 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/4| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/4| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/4| "); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/4| "); } step4(); break; case s4_dur: laststep(); break; } break; case 5: switch(counter) // 5 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/5| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/5| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/5| "); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/5| "); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/5| "); } step5(); break; case s5_dur: laststep(); break; } break; case 6: switch(counter) // 6 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/6| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/6| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/6| "); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/6| "); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/6| "); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/6| "); } step6(); break; case s6_dur: laststep(); break; } break; case 7: switch(counter) // 7 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/7| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/7| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/7| "); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/7| "); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/7| "); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/7| "); } step6(); break; case s6_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 7/7| "); } step7(); break; case s7_dur: laststep(); break; } break; case 8: switch(counter) // 8 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/8| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/8| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/8| "); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/8| "); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/8| "); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/8| "); } step6(); break; case s6_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 7/8| "); } step7(); break; case s7_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 8/8| "); } step8(); break; case s8_dur: laststep(); break; } break; case 9: switch(counter) // 9 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/9| "); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/9| "); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/9| "); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/9| "); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/9| "); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/9| "); } step6(); break; case s6_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 7/9| "); } step7(); break; case s7_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 8/9| "); } step8(); break; case s8_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 9/9| "); } step9(); break; case s9_dur: laststep(); break; } break; case 10: switch(counter) // 10 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/10|"); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/10|"); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/10|"); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/10|"); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/10|"); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/10|"); } step6(); break; case s6_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 7/10|"); } step7(); break; case s7_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 8/10|"); } step8(); break; case s8_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 9/10|"); } step9(); break; case s9_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st.10/10|"); } step10(); break; case s10_dur: laststep(); break; } break; case 11: switch(counter) // 11 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/11|"); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/11|"); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/11|"); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/11|"); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/11|"); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/11|"); } step6(); break; case s6_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 7/11|"); } step7(); break; case s7_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 8/11|"); } step8(); break; case s8_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 9/11|"); } step9(); break; case s9_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st.10/11|"); } step10(); break; case s10_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st.11/11|"); } step11(); break; case s11_dur: laststep(); break; } break; case 12: switch(counter) // 12 step programme { case 0: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 1/12|"); } step1(); break; case s1_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 2/12|"); } step2(); break; case s2_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 3/12|"); } step3(); break; case s3_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 4/12|"); } step4(); break; case s4_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 5/12|"); } step5(); break; case s5_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 6/12|"); } step6(); break; case s6_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 7/12|"); } step7(); break; case s7_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 8/12|"); } step8(); break; case s8_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st. 9/12|"); } step9(); break; case s9_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st.10/12|"); } step10(); break; case s10_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st.11/12|"); } step11(); break; case s11_dur: if(debug == 1) { lcd.setcursor(0,0); lcd.print("st.12/12|"); } step12(); break; case s12_dur: laststep(); break; } break; }

thanks lot in advance!

ozy

ok, won't go through code, i'll seek give ideas.

if want utilize loops should utilize arrays instead of defines. like:

#define number_of_steps 12; const int duration[number_of_steps] = { 5, 2, 1, … }; const int color1[number_of_steps] = {1000, 0, 0, … }; const int color2[number_of_steps] = { 0, 0, 1000, … };

a sample loop through steps this:

for(int i=0; i<number_of_steps; i++){ //do whatever… //set colors using array setcolor(color1, color1[i] ); setcolor(color2, color2[i] ); }

see arduino reference on for statements. there illustration on loops.

now go , shrink code :)

loops optimization for-loop arduino

No comments:

Post a Comment