[SOLVED] Seems to skip 'if' statement on initial power up.

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.

Thanks in advance, code attached below;

Spinner_04_add_up_dn_Functions.ino (7.46 KB)

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.

here's some code I just posted in another thread fro monitoring multiple switches

// recognize multiple button presses; tgl LED when pressed

byte butPins [] = { A1, A2, A3 };
byte ledPins [] = { 10, 11, 12 };

#define N_PINS sizeof(butPins)

byte butLst [N_PINS] = {};


// -----------------------------------------------------------------------------
void setup (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        digitalWrite (ledPins [n], OFF);
        pinMode      (ledPins [n], OUTPUT);

        pinMode      (butPins [n], INPUT_PULLUP);
        butLst [n]  = digitalRead (butPins [n]);
    }
}

// -----------------------------------------------------------------------------
void loop (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        byte but = digitalRead (butPins [n]);

        if (butLst [n] != but)  {
            butLst [n] = but;

            if (LOW == but)     // button pressed
                digitalWrite (ledPins [n], ! digitalRead (ledPins [n]));
        }
    }

    delay (10);         // debounce
}

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:

boolean buttonIsPressed = digitalRead(buttonPin) == LOW;

That way 'true' always means 'pressed' and 'false' always means 'not pressed'

Awesome John, you nailed it!
Thank you.