Hi there wonderful brains trust.
My C19 project is working well in all regards except on initial power up.
I apologise in advance for any untidy or redundant code, this is my first design project.
Basically a circuit comprising or:
A-Micro
2 x 4 digit 7 seg displays
2 dc motors through a dual H bridge
6 control switches for on/off, direction, coupled speed +/-, and split amount +/-
Basic operations is
in setup I set motors to off, direction to "Spin" and display "OFF" ( this is the desired setup, the code currently inverts this for fault finding)
3 main functions
fn1_setInputFlags(); // this (borrowed & modified) function checks the 6 switches and sets a flag after a debounce time
fn2_resolveInputFlags(); // this (borrowed & modified) function starts with an 'if' and should run only if a flag was set
fn3_runTheMachine(); // this function runs the machine based on conditions set in setup or modified by button presses. Both motors start up at 100 and M2 can then be driven to drop below M1 speed.
The fault only occurs on initial power up.
What should occur - in setup I set 'OFF' condition, Fn1 should not set a flag as no buttons were pressed therefore Fn2 should not operate. Fn3 should set the motors to the setup values of OFF.
What does happen - somehow the OFF condition from setup is inverted to On by Fn2 and at power up the motors run and the displays show the running values.
I think there is an issue in Fn1 where a false flag is being set or in Fn2 where the 'if' statement is being ignored and the first switch function inverts the OFF back to an on condition.
Your switch debounce code is 100x more complicated than it needs to be. Look around for some examples. For one thing, you don't need to implement debounce timing for each switch individually. Just read them all, look for any change and register it, then ignore all switches for the debounce interval.
You are using INPUT_PULLUP inputs but you initialize inputState and lastInputState to LOW. When you read the HIGH from the un-pressed button you detect that as a state change and act on it.
For INPUT_PULLUP inputs I like to use boolean states and translate from LOW/HIGH to true/false when reading the pin: