This is why I’m not really interested in Arduino. I have no desire to write or debug 100s of lines of code.
BMMENYC
It’s one thing to make it work. It’s another to make it maintainable.
By maintainable, I mean easy to understand, debug and enhance. It applies to hardware and mechanical design, as well as software. Duplicated information in software is a problem. This includes duplicating code (i.e. cut-and-paste). Any flaw in the original version of the code now needs to be fixed in every place the code was duplicated. Maintainable also means for the developer to be able to look at the code in 6 months and understand how it works.
There are common techniques that improve the maintainability of code. Sub-functions are fundamental. Tables capture behavior in data instead of logic.
A Signaling Example
Signaling is a good example. because the basic concept is simple but it applies to a large number of outputs and interrelated inputs. This example starts with a cut-and-paste approach and moves to more structured and scalable approaches.
The following diagram illustrates just a few block and corresponding signals of a layout. There are three blocks numbered 1-4. The corresponding analog inputs, 1-4, are used to read an active low occupancy detector for each block. Six signals are shown, B-D in the west direction and a-c in the east direction.
<B <C <D
____1_____|____2_____|____3_____|____4_____
a> b> c>
The table indicates the digital output pins controlling the red, yellow and green LEDs of each signal. Setting the pin low turns on the LED. The signal displays red, if the next block is occupied. It displays yellow, if the block after the next is occupied. It displays green, if the next two blocks are unoccupied.
Good post. Those of us used to programming should already know this, but those new to the whole thing may think the only way to do it is the initial example with all that code, and hundreds of lines of code, even if it IS just repetition, is daunting. Using functions like this, especially when you can get them canned from someone else who has done the heavy lifting, turns the job into mostly just a matter of assigning the correct address (variable) to each signal.
I’ve spent my entire life in electronics from the early age of 8 until now at 80 and never did anything in programming. I’m stuck doing it the hard way, there is no way I could take on learning to write code nor do I want to do so.
I search the Internet for sketches to modify to my needs and ask for help on the Forum. And by the way Randy and the rest of you guys thanks a bunch for all your help in the past!
I did have success at making my own 14 block signal controller using a MEGA. I tried a lot of sketches searching the Internet but they either wouldn’t compile for me or weren’t what I was looking for. Most were far too complex for my needs. I settled for a simple truth table format and it works great for me. There is probably a much cleaner way of writing the code but it works for me.
I overhauled reworked and reinstalled my entire 14 block signal system for under $100. The $100 includes the cost of fabrication of 28 three color LED search light signal heads, a pair of MEGAs and all of the components needed. The $10
Well done Greg. On an even broader scale many Arduino users incorporate pre-written libraries so we don’t have to “sweat the details” for things like servos and LCD displays. Years ago, when I started programming in Visual Basic, I wrote many VB functions to emulate some of the MUMPS operators that I was so familar with in even earlier years. I feel like a caveman even talking about MUMPS. Plus, VB is no longer supported by Microsoft but remains in many legacy apps.