Advanced methods in a simple sketch
a.k.a. "there no kill overkill."
i'm quite dissatisfied many of examples included arduino ide. in particular, blink without delay referenced example of "nonblocking" coding technique. while works doing 1 thing (toggling led) @ regular intervals, written , hard generalize from. if want different on , off times? or more complex sequence morse code series of short , long flashes? i've wanted quite while write own example more complicated, better shows off flexibility of techniques. i've gone bit overboard ambition. sketch attached in .zip file below.
my sister , build pov fan display based on great scott video here. changes made hardware use 7 leds instead of 5 , adding tp4056 battery charger. went 7 leds in order use same bitmaps 5x7 character lcds. behold! present...the drama queen!

video link. swoon @ sexy voice.
the code posted instructables however, offends delicate sensibilities. delay()? no!!!!!!!!! my eyes!!
so rewrote scratch. result, think, thing of beauty. has 2 legitimate technical improvements on great scott's code: version can dynamically adapt changes in fan speed, display stable no matter setting have fan switch on. reverses letters on bottom appear in right order , right orientation readable.
did overengineer sketch improvements? might so, nay! worth doing worth overdoing, , worth overdoing worth really overdoing. have poured of c++ skills have honed on years crafting sketch, , offer community further education. original code written me, though significant parts of influenced conventions of standard template library. pmemref , pmemptr classes ripped off arduino eeprom library, adapted use progmem variables instead of eemem. (i can hope many of same code) biggest benefit of of libraries i've created here reusable in other projects, , portion of goodies have created in utilslib folder.
underneath apparent obscene complexity, task program performing straight forward. row of leds lit based on timed sequence. magnet detected hall sensor mark start of sequence, , interval between marks measured adjust sequence speed. bottom half of rotation, lot of things had reveresed letters appear in right way. last part might seem easy, needed inspiration come way in elegant way minimum of repeated code.
please forgive stylistic faux pas have commited mixing camelcase , underscore_notation together. , of course, not every sketch needs level of complexity. can provide inspiration how of these techniques can used in other projects. pick , choose please.
this sketch contains examples of following advanced techniques:
non-blocking timing , sequencing
the hall sensor must monitored @ times, , sequence must able reset @ time, if not finished when next trigger comes (such if fan speeding up). precludes use of delay() time led sequence. technique seen in 2 timing objects used time traversal of column's worth of arc , transition top section bottom.
the essence of non-blocking timing , flow control use data control execution of code, , not lists of program statements. study how sequence , timer objects interact in order execute multi-stage sequence instead of toggling single output.
separation of functionality classes
known in jargon encapsulation, seperating independent bits of functionality classes , functions can reduce clutter in loop function. makes easier adjust interaction between different parts minimal effort. while trying track down bug, wanted replace hall sensor trigger timer code move through sequences without spinning fan. code structure, needed substitute in 1 line.
sophisticated class behavior operator overloading
instead of verbose function names, great deal of conciseness can gained providing behavior various operators in language, particularily things arithmetic( + - * /), comparison(== < > != <= >=) , assignment (=). dereference , access operators (* [] ->) can used give type pointer-like behavior, useful types meant iterate on collection of kind (like sequence types).
template functions , classes, specialization of methods
templates allow use same function variety of different types. way use types variable long function code valid once type use subsituted in. there restrictions using tmeplates, powerful tool writing flexible code can adapted many situations.
the pmemref class contains specializations of methods optimize handling of of unique types.
"magic value" avoidance
a "magic number" when literal value used within code no explanation or context why value. i've taken care minimize amount of literal numbers in actual code giving constants meaningful names. beneficial use expressions calculate dependant values. prevents mistakes, , requires less things changed if want adjust code. exmaple, i've defined each letter's worth of arc divided 5 sections display led matrix in, , 2 blank sections provide reasonable amount of space between letters. rather hard coding character_width constant 7, defined sum of previous 2 constants. makes compiler math me, , means have change 1 thing.
because guideline , not rule, open interpretation. 1 thing might not understood high , low magic numbers. true named constants, not meaningful names. high , low not have intrinsic meaning on own, because depends on circuit connected pin. example, leds in circuit activated when high written pin, when hall sensor activated magnet, pulls pin low. giving meaningful names (hallactivelevel , ledactivelevel) replace high , low makes easier understand code's intention.
preprocessor directives
most people know #include , #define directives, can magic preprocesor. #define isn't static symbols, can used define function-like macros, debug #defines toward top of sketch. combined #if-#else-#endif directives let conditionally add or exclude based on conditions, can shift between "debug" version of code (that might use serial prints, different inputs or outputs, or other changes) "production" version test code stripped out.
you can see remains of efforts debug problem in load_leds() function left in sketch.
there's other things in here pretty gratuitous right (like abstract classes inherit from), playing around , learning effects of different parts fun is! include excel sheet used generate font tables, showing how formulas , other labor saving activities can make easy generate pre-computed tables of values.
feel free ask questions. i'm sure there many.
i'm quite dissatisfied many of examples included arduino ide. in particular, blink without delay referenced example of "nonblocking" coding technique. while works doing 1 thing (toggling led) @ regular intervals, written , hard generalize from. if want different on , off times? or more complex sequence morse code series of short , long flashes? i've wanted quite while write own example more complicated, better shows off flexibility of techniques. i've gone bit overboard ambition. sketch attached in .zip file below.
my sister , build pov fan display based on great scott video here. changes made hardware use 7 leds instead of 5 , adding tp4056 battery charger. went 7 leds in order use same bitmaps 5x7 character lcds. behold! present...the drama queen!
video link. swoon @ sexy voice.

the code posted instructables however, offends delicate sensibilities. delay()? no!!!!!!!!! my eyes!!
so rewrote scratch. result, think, thing of beauty. has 2 legitimate technical improvements on great scott's code: version can dynamically adapt changes in fan speed, display stable no matter setting have fan switch on. reverses letters on bottom appear in right order , right orientation readable.
did overengineer sketch improvements? might so, nay! worth doing worth overdoing, , worth overdoing worth really overdoing. have poured of c++ skills have honed on years crafting sketch, , offer community further education. original code written me, though significant parts of influenced conventions of standard template library. pmemref , pmemptr classes ripped off arduino eeprom library, adapted use progmem variables instead of eemem. (i can hope many of same code) biggest benefit of of libraries i've created here reusable in other projects, , portion of goodies have created in utilslib folder.
underneath apparent obscene complexity, task program performing straight forward. row of leds lit based on timed sequence. magnet detected hall sensor mark start of sequence, , interval between marks measured adjust sequence speed. bottom half of rotation, lot of things had reveresed letters appear in right way. last part might seem easy, needed inspiration come way in elegant way minimum of repeated code.
please forgive stylistic faux pas have commited mixing camelcase , underscore_notation together. , of course, not every sketch needs level of complexity. can provide inspiration how of these techniques can used in other projects. pick , choose please.
this sketch contains examples of following advanced techniques:
non-blocking timing , sequencing
the hall sensor must monitored @ times, , sequence must able reset @ time, if not finished when next trigger comes (such if fan speeding up). precludes use of delay() time led sequence. technique seen in 2 timing objects used time traversal of column's worth of arc , transition top section bottom.
the essence of non-blocking timing , flow control use data control execution of code, , not lists of program statements. study how sequence , timer objects interact in order execute multi-stage sequence instead of toggling single output.
separation of functionality classes
known in jargon encapsulation, seperating independent bits of functionality classes , functions can reduce clutter in loop function. makes easier adjust interaction between different parts minimal effort. while trying track down bug, wanted replace hall sensor trigger timer code move through sequences without spinning fan. code structure, needed substitute in 1 line.
sophisticated class behavior operator overloading
instead of verbose function names, great deal of conciseness can gained providing behavior various operators in language, particularily things arithmetic( + - * /), comparison(== < > != <= >=) , assignment (=). dereference , access operators (* [] ->) can used give type pointer-like behavior, useful types meant iterate on collection of kind (like sequence types).
template functions , classes, specialization of methods
templates allow use same function variety of different types. way use types variable long function code valid once type use subsituted in. there restrictions using tmeplates, powerful tool writing flexible code can adapted many situations.
the pmemref class contains specializations of methods optimize handling of of unique types.
"magic value" avoidance
a "magic number" when literal value used within code no explanation or context why value. i've taken care minimize amount of literal numbers in actual code giving constants meaningful names. beneficial use expressions calculate dependant values. prevents mistakes, , requires less things changed if want adjust code. exmaple, i've defined each letter's worth of arc divided 5 sections display led matrix in, , 2 blank sections provide reasonable amount of space between letters. rather hard coding character_width constant 7, defined sum of previous 2 constants. makes compiler math me, , means have change 1 thing.
because guideline , not rule, open interpretation. 1 thing might not understood high , low magic numbers. true named constants, not meaningful names. high , low not have intrinsic meaning on own, because depends on circuit connected pin. example, leds in circuit activated when high written pin, when hall sensor activated magnet, pulls pin low. giving meaningful names (hallactivelevel , ledactivelevel) replace high , low makes easier understand code's intention.
preprocessor directives
most people know #include , #define directives, can magic preprocesor. #define isn't static symbols, can used define function-like macros, debug #defines toward top of sketch. combined #if-#else-#endif directives let conditionally add or exclude based on conditions, can shift between "debug" version of code (that might use serial prints, different inputs or outputs, or other changes) "production" version test code stripped out.
you can see remains of efforts debug problem in load_leds() function left in sketch.
there's other things in here pretty gratuitous right (like abstract classes inherit from), playing around , learning effects of different parts fun is! include excel sheet used generate font tables, showing how formulas , other labor saving activities can make easy generate pre-computed tables of values.
feel free ask questions. i'm sure there many.
feel free ask questions. i'm sure there many.
proprocessor directiveswhy call preprocessor proprocessor?
Arduino Forum > General Category > General Discussion (Moderators: mbanzi, DojoDave, mellis) > Advanced methods in a simple sketch
arduino
Comments
Post a Comment