Hi all!
Im about to do a re-write (yet again) for my current electronics-project, to control a number of different lights (including detatchable trailer), using arduino as controller.
I know, its a waste of processing power, i could have solved the project much simpler, faster and cheaper had i gone with the motorcycle-approach with turn indicator relays and standard handlebar buttons and switches.
But that had´nt felt "right"..
Instead, i wish to prepare for the future, to be able to integrate my light system with other future bike upgrades, like a e-bike conversion or dedicated mp3-player (with separate, permanently attatched speakers).
But since im getting tired of re-writing the code, i figured i should ask the internet for the best approach given the current set of "problems" to solve..
There are a number of different inputs on the project, about 7 digital (on/off) pins are used for buttons and switches, aditional 2 analog pins are used for brake detection.
The brake detectors will be utilizing hall effect sensors and tiny magnets, fixed in the brakehandle pivot point (to detect when the brake lever is moved, and roughly how much).
The output channels will be turn indicator left/right, positional lights front/rear, side lights left/right, extra light (front), brakelight (rear), along with 4 aditional digital channels (3 for power control, 1 for extra light mode selector)
The power control mentioned above will be a 4 digital pin output, to 3 mosfets, that allows the system to disconnect currently un-used external components (like the extra light(front) and power output thru the trailer socket (when trailer is not present)).
A number of buttons will offer 2 separate clicks, long and short, to activate different sets of states for the coresponding function.
For instance, when the right indicator button is pressed, depending if its a short or long click, the system responds with ether a fast 3 blink right signal that then selfterminates, or activates right indicator lights, to blink until next time the button is pressed (and released).
The same goes for the extra light (front) button, short click turns on/off the extra light, long click changes to the next mode, to increase the light output.
The extra light (front) function will be a dual high power LED bicycle front light, that offers 4 different modes, you select the next mode by clicking the single button on the housing, off>lights low>lights mid > lights HIGH! > off.
I aim to disconnect the led light with a mosFET when i turn off the extra lights from the handlebar button, and when i re-connect it electronically, it will also activate the transistor connected over the mode-select button inside the extra light housing.
This way i can both turn off/on the extra light, and also keep the current led modes.
So as you can see, there is a lot that will be happening in software, and i need a neat way to store all this info..
And since i dont want to be addressing each pin, each button, debounce each instance and so on, i figured i try to use for-loops instead, in functions that are called each x program loops, to avoid a timer per input to debounce.
I havent decided if i will be using arrays or bitWrite/bitRead int´s, but the idea is to copy currentRead to lastRead, set currentRead to digitalRead(thisPin), and then compare currentRead[thisPin] with lastRead[thisPin], if both entries are same, set buttonState[thisPin] = 1, else 0.
if buttonState[thisPin] = 1, buttonCount[thisPin]++
to count number of program loops while the button is pressed
if buttonState[thisPin] == 0 && buttonCount[thisPin] >longpressLimit, execute longPress[thisPin] and set buttonCount[thisPin] = 0;
And from what i understand of programming (witch is not much), i have then read and debounced each button and got a array of each buttons corresponding function, and i can then read if the last buttonpress is long, short or "currently pressed".
I do apologize for the above mess of a attempt to explain what i mean, i face two challenges, not my main language and im no programmer, just a average joe with learning difficulties..
But what im asking here is primarily, does that last part of the above piece sound reasonable or is this a uneccisarily complicated way of doing things?
Im not asking anyone to write the code for me, im just asking for hints, suggestions and constructive chriticism.. (and yes, im missing a spelling checker.. sorry..