code cleanup to control 4 discrete count-up display timers

This is my 2nd foray into Arduino after a 3 year absence. I'm having trouble with the following sketch-

/*
Program for Arduino Micro to monitor 4 dry contact inputs (switches)
and signal an external countup timer to track the time each input is
"on". Timers are paused when the switch is "off".
When the switch turns back on, a reset signal is cycled, and
the timer pause is removed again.
c 2014 Bluewater Services, LLC.
*/

int wait = 10;                 // the reset and pause delay

int timerA = 1;                // thess are the pins that will pause the timers when HIGH
int timerB = 2;
int timerC = 3;
int timerD = 4;
int resetApin = 5;            // these are the pins that when HIGH will reset timers to "0" and hold
int resetBpin = 6;
int resetCpin = 7;
int resetDpin = 8;
int SW1 = 9;                   // these are the dry contacts from the relays
int SW2 = 10;
int SW3 = 11;
int SW4 = 12;

int now1 = 0;        // current state of switch1
int then1 = 0;       // previous state of switch1
int now2 = 0;        // current state of switch2
int then2 = 0;       // previous state of switch2
int now3 = 0;        // current state of switch3
int then3 = 0;       // previous state of switch3
int now4 = 0;        // current state of switch4
int then4 = 0;       // previous state of switch4



void setup () 
{
  int outMin = 5; // Lowest output pin 
  int outMax = 8; // Highest output pin
  for(int i=outMin; i<=outMax; i++)  // set resets pins as outputs
  {pinMode(i, OUTPUT);}
  for(int i=outMin; i<=outMax; i++)   // set reset pins High to reset the timers
  {digitalWrite(i, HIGH);}
  
  int outMinT = 1; // Lowest output pin
  int outMaxT = 4; // Highest output pins
  for(int i=outMinT; i<=outMaxT; i++)  // set timer pins as Output
  {pinMode(i, OUTPUT);}
  for(int i=outMinT; i<=outMaxT; i++)  // set timer pins high to hold the timers at 0
  {digitalWrite(i, HIGH);}
  
  int inMin = 9; // Lowest input pin
  int inMax = 12; // Highest input pin
  for(int i=inMin; i<=inMax; i++)  // set SW pins as Input
  {pinMode(i, INPUT_PULLUP);}
  
  for(int i=outMin; i<=outMax; i++)  // set reset pins LOW to allow for counting to begin
  {digitalWrite(i, LOW);}
}

void loop ()
{
  // switch 1 code

    int testRead = digitalRead (SW1);    // place to store a comparitive reading
  delay (wait);                          // allow the switch state to settle
  now1 = digitalRead(SW1);               // read switch state again

  if (now1 == testRead)                 // check for switch bounce
    {if (now1 != then1)                  // if no bounce, check for state change since last loop
      {if (now1 == HIGH)                 // if SW1 is OFF:
        {digitalWrite (timerA, HIGH);}     // stop timer
                      
      else                           // if SW1 is ON:
       {digitalWrite (resetApin, HIGH);  // resets the timer
        delay(wait);
        digitalWrite (resetApin, LOW);   // remove the reset signal
        digitalWrite (timerA, LOW);}      // start the timer
                  then1 = now1;                      // record the state of the switch just acted upon
    }
  }
}

problems:

  1. I tried to setup some arrays to initialize all the I/O pins in a more concise manner, but the pin states weren't being set right.
    How should I clean up this section?
  2. the "void loop" section: when I include all 4 iterations the circuit gets buggy- some timers don't reset, some keep counting after the relay contact has re-opened...
    Is there a better way to do this? I don't understand interrupts well enough- seems like they add an unnecessary step programmaticly. This sketch is the whole program- I won't be adding extra functionality later. It needs to work and be very robust (no overflows, or used-up memory), so hopefully it won't need to have the Micro reset at all.

Thanks,
Nathan

Look at the use of functions and struct's

Mark

holmes4:
Look at the use of functions and struct's

Mark

Or arrays. Don't just say that you tried. Show what you tried and explain exactly what happened - no hand-waving, "that didn't work" crap.

You could use the auto-format tool to get a better idea of what your code is doing.

After nearly 30 hours into this, and it's related circuit, it seems there are some other mechanical unknows, ie. my breadboard was not consistently making connections, etc. As soon as I know the hardware is rock solid, I'll get back with SPECIFIC issues that arise.

As for the comment about auto format- being such an inexperienced programmer, the curly braces are taking some time to get used to- It would be sweet if there was a formatting option in the environment which would highlight the code block in a given color, based on which statements were within a given brace. It is relatively easy to see where a brace system starts, but multiple stacked closing braces give me fits at this point in my education.

Your code, after auto format - easier to read, surely?

/*
Program for Arduino Micro to monitor 4 dry contact inputs (switches)
 and signal an external countup timer to track the time each input is
 "on". Timers are paused when the switch is "off".
 When the switch turns back on, a reset signal is cycled, and
 the timer pause is removed again.
 c 2014 Bluewater Services, LLC.
 */

int wait = 10;                 // the reset and pause delay

int timerA = 1;                // thess are the pins that will pause the timers when HIGH
int timerB = 2;
int timerC = 3;
int timerD = 4;
int resetApin = 5;            // these are the pins that when HIGH will reset timers to "0" and hold
int resetBpin = 6;
int resetCpin = 7;
int resetDpin = 8;
int SW1 = 9;                   // these are the dry contacts from the relays
int SW2 = 10;
int SW3 = 11;
int SW4 = 12;

int now1 = 0;        // current state of switch1
int then1 = 0;       // previous state of switch1
int now2 = 0;        // current state of switch2
int then2 = 0;       // previous state of switch2
int now3 = 0;        // current state of switch3
int then3 = 0;       // previous state of switch3
int now4 = 0;        // current state of switch4
int then4 = 0;       // previous state of switch4



void setup () 
{
  int outMin = 5; // Lowest output pin 
  int outMax = 8; // Highest output pin
  for(int i=outMin; i<=outMax; i++)  // set resets pins as outputs
  {
    pinMode(i, OUTPUT);
  }
  for(int i=outMin; i<=outMax; i++)   // set reset pins High to reset the timers
  {
    digitalWrite(i, HIGH);
  }

  int outMinT = 1; // Lowest output pin
  int outMaxT = 4; // Highest output pins
  for(int i=outMinT; i<=outMaxT; i++)  // set timer pins as Output
  {
    pinMode(i, OUTPUT);
  }
  for(int i=outMinT; i<=outMaxT; i++)  // set timer pins high to hold the timers at 0
  {
    digitalWrite(i, HIGH);
  }

  int inMin = 9; // Lowest input pin
  int inMax = 12; // Highest input pin
  for(int i=inMin; i<=inMax; i++)  // set SW pins as Input
  {
    pinMode(i, INPUT_PULLUP);
  }

  for(int i=outMin; i<=outMax; i++)  // set reset pins LOW to allow for counting to begin
  {
    digitalWrite(i, LOW);
  }
}

void loop ()
{
  // switch 1 code

  int testRead = digitalRead (SW1);    // place to store a comparitive reading
  delay (wait);                          // allow the switch state to settle
  now1 = digitalRead(SW1);               // read switch state again

  if (now1 == testRead)                 // check for switch bounce
  {
    if (now1 != then1)                  // if no bounce, check for state change since last loop
    {
      if (now1 == HIGH)                 // if SW1 is OFF:
      {
        digitalWrite (timerA, HIGH);
      }     // stop timer

      else                           // if SW1 is ON:
      {
        digitalWrite (resetApin, HIGH);  // resets the timer
        delay(wait);
        digitalWrite (resetApin, LOW);   // remove the reset signal
        digitalWrite (timerA, LOW);
      }      // start the timer
      then1 = now1;                      // record the state of the switch just acted upon
    }
  }
}

Yes, thanks, AWOL.

I was just about to re-post the code as such, and you beat me to it. Thanks so much - it will be easier to get used to seeing it consistently formatted.