switch input and relay output

Hello

I’m relatively new to the Arduino and its coding.

I am creating an interface between two pieces of equipment and thought an Arduino would be a perfect solution.

Here’s my problem. I have 16 switched inputs which are operated externally. These switches connect to an Arduino Mega. Once the switch has been triggered, the Arduino, stops taking an input from that switch for the next few seconds. The output is 16 relays which only need to be pulsed.

The code I have sort of put together with help from books and the web works great.
BUT… Relay 16 needs to be pulsed 20 seconds AFTER the switch has been pressed not immediately like the others.
How do I do this? Where does the code fit? I know I can’t use the delay functoiun!

Any help will be gratefully received.
Thanks

Bob

const int inputPins[] = {38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53}; // create an array of pins for switch inputs
const int relayPins[] = {22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37}; // create array of output pins for relays

int butpress[16];
int timnow[16];
int timthen[16];
int deltim;
void setup()
{  
  deltim=5000;
for(int index = 0; index < 16; index++)
{
pinMode(relayPins[index], OUTPUT); // relay as output
pinMode(inputPins[index], INPUT); // switch as input
digitalWrite(inputPins[index],HIGH);

butpress[index]= 0;
timnow[index]=0;
timthen[index]=0;
}
}
void loop(){
for(int index = 0; index < 16; index++)
{
  timnow[index]=millis();
 
   if (index ==0)
  deltim = 3000;   //release time for relay 1
   if (index ==1)
  deltim = 3000;   //release time for relay 2
   if (index ==2)
  deltim = 3000;   //release time for relay 3
   if (index ==3)
  deltim = 3000;   //release time for relay 4
   if (index ==4)
  deltim = 3000;   //release time for relay 5
   if (index ==5)
  deltim = 3000;   //release time for relay 6
   if (index ==6)
  deltim = 3000;   //release time for relay 7
   if (index ==7)
  deltim = 3000;   //release time for relay 8
   if (index ==8)
  deltim = 3000;   //release time for relay 9
   if (index ==9)
  deltim = 3000;   //release time for relay 10
   if (index ==10)
  deltim = 3000;   //release time for relay 11
   if (index ==11)
  deltim = 3000;   //release time for relay 12
   if (index ==12)
  deltim = 3000;   //release time for relay 13
   if (index ==13)
  deltim = 3000;   //release time for relay 14
   if (index ==14)
  deltim = 3000;   //release time for relay 15
   if (index ==15)
  deltim = 3000;   //release time for relay 16
  
  if (timnow[index]-timthen[index]>deltim)
  {
  butpress[index]=0;
  timthen[index]=timnow[index]; 
  }
  int val = digitalRead(inputPins[index]); // read input value
if (val == LOW) // check if the switch is pressed
{
  if (butpress[index]==0) 
  {
  digitalWrite(relayPins[index], LOW); // turn relay on if switch is pressed
  butpress[index]=1;
  timthen[index]=timnow[index];
  }
}
else
{
digitalWrite(relayPins[index], HIGH); // turn relay off
}
}

}

The example sketch "BlinkWithoutDelay" will show you the way to go.

Also please reformat your code (ALT-T in the IDE) to make it a lot more readable.

I see you have lots of delttime=3000 - actually all relays get the same delay, so all that code appears obsolete.

You're definitely on the right path by using millis() and checking elapsed time, but the code is a formatting mess making it too hard to figure out what you're really trying to do.

Here is a solution I came up with that works well. You simply create as many objects of nbTimer you need.

// ************************** class nbTimer **********************************
class nbTimer 
{
public:
  nbTimer();                                         // constructor prototype
  unsigned char Time(unsigned long dly);
  unsigned long Counter;                             //  for debugging
private:
  unsigned long Count;
  unsigned char Init = LOW;
  unsigned char Timeout = LOW;
};

nbTimer::nbTimer()                                   // constructor definition
{}

// ***********
unsigned char nbTimer::Time(unsigned long dly)
{
  if (Init == LOW)
  { // Run Once Code
    Timeout = LOW;            // Reset Flag
    Count = millis ();        // Snapshot of MILLIS count
    Init = HIGH;
  }
  Counter=((millis ()) - Count);
  if (((millis ()) - Count) >= dly)
  {
    Timeout = HIGH;              // set Flag
    Init = LOW;
  }
  return Timeout;
}//------------------------------------------------------

Example:

myTimer nbTimer;

unsigned char Timeout=myTimer.Time(1000L);

It will NOT keep initializing the timer if it has not timed out. If you want to watch the time, simply say

unsigned long myCounter=myTimer.Count;

Hope this is useful.

'Thank you for your help and support.

I have bought some books and busy reading them - its a bit of a learning curve.
I think that I may be on the way to resolving most of the problems I have encountered.
I might have bitten off more than I can chew for the project however!
On a plus note I have a DMX module working now and its going well.

Thank you