8 relais start after move key switch

Hello,

I will make a program to switch 8 relais.

I start with a key switch and then the first relais must start and after a delay the second , and so further.
After switch off the key switch the relais must swich off from relais 8 till relais one with induvidual delay
I make a program but it is not good working. After starting the relais start but it runs till everything is switched on and then all relays switches off.

Who can help my to fix this problem ?

Relais_met_knop-20-6-2015.ino (3.34 KB)

Is "buttonState" what you are using to check the state of the key switch? And does HIGH or LOW turn the relay on?

It looks as if the code to switch the relays on/off should be within the "if" statement, not below it. As it is the relays will turn ON then turn OFF.

Too many curly brackets doesn't help!

  if (buttonState == HIGH) { 
    // turn LED on:
    digitalWrite(buttonPin, LOW);
    
    // code for relays ON here

  }
  else {
    // turn LED off:
    digitalWrite(buttonPin, HIGH);

   // code for relays off here

    }

I can not make it as i want.

I want to turn a key switch to on,
then the relais must walk after delay to go on,
It must stay till the key is switching to off,
now must the relais goes off with delay and stay off after new key switch to on.

Who can help me?
At first i must learing how the commands work.
I'm thinking in PLC logic and this is a little bit different.

gert848:
Who can help me?

No problem.

Just let me know what do you want to happen, if you 'switch off' using your key switch before the last relay is in 'ON' state?

I see two possibilities:

  • the 'ON' sequence continues until all relays are all 'ON', then the relays are turned off one by one
  • the 'ON' sequence is aborted, and the relays are turned off one by one

Which is the preferred programming logic?

jurs:
No problem.

Just let me know what do you want to happen, if you ‘switch off’ using your key switch before the last relay is in ‘ON’ state?

I see two possibilities:

  • the ‘ON’ sequence continues until all relays are all ‘ON’, then the relays are turned off one by one
  • the ‘ON’ sequence is aborted, and the relays are turned off one by one

Which is the preferred programming logic?

  • the ‘ON’ sequence is aborted, and the relays are turned off one by one

I turn a key switch on.
All the relais must go on after the delays.
When i turn off the keyswitch the relais must go off after the delays.
After that nothing must go on or off.

Only when i turn the keyswitch on the cyclus start.
When i switch off the keyswitch when the relais are starting up the relais must stop.
Then all relais must switch off.

Conclusion: Key switch on : R1,R2,R3,R4 Key switch off : R4,R3,R2,R1

gert848:

  • the ‘ON’ sequence is aborted, and the relays are turned off one by one

OK, here is the code (not fully tested, I just used serial debugging in the serial monitor):

const byte keyPin=2; // pin number for key switch
#define keyPinMode INPUT_PULLUP  // INPUT or INPUT_PULLUP
#define BOUNCETIME 20  // bounce time of keyswitch in milliceconds

const byte relayPins[]={6,7,8,9,10,11,12,13};
#define NUMRELAYS sizeof(relayPins)
#define RELAY_ACTIVE LOW  // pin state for active/switched relay
#define INTERVAL 1000  // time interval between switching relays


byte readKeySwitch()
{ // read and debounce key switch pin
  static byte onState=false;
  static unsigned long lastRuntime;
  if (millis()-lastRuntime<BOUNCETIME) return onState;
  lastRuntime=millis();
  onState=digitalRead(keyPin);
  if (keyPinMode==INPUT_PULLUP) onState=!onState;
  return onState;
}

void switchRelays(byte howMany)
{ // switch "howMany" relays to on
  for (int i=0;i<NUMRELAYS;i++)
  {
    if (i<howMany)
    {
      digitalWrite(relayPins[i],RELAY_ACTIVE);
      Serial.print('1'); 
    }
    else
    {
      digitalWrite(relayPins[i],!RELAY_ACTIVE);
      Serial.print('0');
    }
  }
  Serial.println();
}

void setup() {
  Serial.begin(9600);
  Serial.println("Good night and good luck!");
  pinMode(keyPin, keyPinMode);
  for (int i=0;i<NUMRELAYS;i++)
  {
    digitalWrite(relayPins[i], !RELAY_ACTIVE);
    pinMode(relayPins[i], OUTPUT);
  }
}

byte lastSwitchState;
unsigned long switchTime;
byte relayCount;

void loop() {
  byte switchState=readKeySwitch();
  if (switchState!=lastSwitchState)
  {
    switchTime=millis();
    lastSwitchState=switchState;
  }
  if (switchState && relayCount<NUMRELAYS)
  { // key switch is on, but not all relays are on
    if (millis()- switchTime >= INTERVAL)
    {
      switchTime+=INTERVAL;
      relayCount++;
    }
  }
  else if (!switchState && relayCount>0)
  { // key switch is off, but there is at least one relay on
    if (millis()- switchTime >= INTERVAL)
    {
      switchTime+=INTERVAL;
      relayCount--;
    }
  }
  switchRelays(relayCount);
}

As I don’t know whether you are using pull-up or pull-down resistors with your key swith and whether your relays are switching on “active HIGH” or “active LOW”, I have prepared the sketch for all combinations.

Just set accordingly in the source code:

#define keyPinMode INPUT // if key switch is connected to a pull-down resistor

or

#define keyPinMode INPUT_PULLUP // if key switch shall use the "internal pull-up" resistor in the controller

And depending on the type of relays:

#define RELAY_ACTIVE LOW  // relay switches on LOW signal

or

#define RELAY_ACTIVE HIGH  // relay switches on HIGH signal

If everything works as expected, you can remove all the “Serial” debug code from the sketch.

Have fun!

jurs:
OK, here is the code (not fully tested, I just used serial debugging in the serial monitor):

const byte keyPin=2; // pin number for key switch

#define keyPinMode INPUT_PULLUP  // INPUT or INPUT_PULLUP
#define BOUNCETIME 20  // bounce time of keyswitch in milliceconds

const byte relayPins={6,7,8,9,10,11,12,13};
#define NUMRELAYS sizeof(relayPins)
#define RELAY_ACTIVE LOW  // pin state for active/switched relay
#define INTERVAL 1000  // time interval between switching relays

byte readKeySwitch()
{ // read and debounce key switch pin
  static byte onState=false;
  static unsigned long lastRuntime;
  if (millis()-lastRuntime<BOUNCETIME) return onState;
  lastRuntime=millis();
  onState=digitalRead(keyPin);
  if (keyPinMode==INPUT_PULLUP) onState=!onState;
  return onState;
}

void switchRelays(byte howMany)
{ // switch “howMany” relays to on
  for (int i=0;i<NUMRELAYS;i++)
  {
    if (i<howMany)
    {
      digitalWrite(relayPins[i],RELAY_ACTIVE);
      Serial.print(‘1’);
    }
    else
    {
      digitalWrite(relayPins[i],!RELAY_ACTIVE);
      Serial.print(‘0’);
    }
  }
  Serial.println();
}

void setup() {
  Serial.begin(9600);
  Serial.println(“Good night and good luck!”);
  pinMode(keyPin, keyPinMode);
  for (int i=0;i<NUMRELAYS;i++)
  {
    digitalWrite(relayPins[i], !RELAY_ACTIVE);
    pinMode(relayPins[i], OUTPUT);
  }
}

byte lastSwitchState;
unsigned long switchTime;
byte relayCount;

void loop() {
  byte switchState=readKeySwitch();
  if (switchState!=lastSwitchState)
  {
    switchTime=millis();
    lastSwitchState=switchState;
  }
  if (switchState && relayCount<NUMRELAYS)
  { // key switch is on, but not all relays are on
    if (millis()- switchTime >= INTERVAL)
    {
      switchTime+=INTERVAL;
      relayCount++;
    }
  }
  else if (!switchState && relayCount>0)
  { // key switch is off, but there is at least one relay on
    if (millis()- switchTime >= INTERVAL)
    {
      switchTime+=INTERVAL;
      relayCount–;
    }
  }
  switchRelays(relayCount);
}




As I don't know whether you are using pull-up or pull-down resistors with your key swith and whether your relays are switching on "active HIGH" or "active LOW", I have prepared the sketch for all combinations.

Just set accordingly in the source code:


#define keyPinMode INPUT // if key switch is connected to a pull-down resistor



or


#define keyPinMode INPUT_PULLUP // if key switch shall use the “internal pull-up” resistor in the controller




And depending on the type of relays:


#define RELAY_ACTIVE LOW  // relay switches on LOW signal



or


#define RELAY_ACTIVE HIGH  // relay switches on HIGH signal




If everything works as expected, you can remove all the "Serial" debug code from the sketch.

Have fun!

Thanks for the program,

I will test it but i have one question,

I must start the relais after a individual setting delay because not all the relais have the same delay.

I think this is now one delay for all relays,

Is that possible to make it with the individual delays for startup and for switching down?

jurs:
OK, here is the code (not fully tested, I just used serial debugging in the serial monitor):

const byte keyPin=2; // pin number for key switch

#define keyPinMode INPUT_PULLUP  // INPUT or INPUT_PULLUP
#define BOUNCETIME 20  // bounce time of keyswitch in milliceconds

const byte relayPins={6,7,8,9,10,11,12,13};
#define NUMRELAYS sizeof(relayPins)
#define RELAY_ACTIVE LOW  // pin state for active/switched relay
#define INTERVAL 1000  // time interval between switching relays

byte readKeySwitch()
{ // read and debounce key switch pin
 static byte onState=false;
 static unsigned long lastRuntime;
 if (millis()-lastRuntime<BOUNCETIME) return onState;
 lastRuntime=millis();
 onState=digitalRead(keyPin);
 if (keyPinMode==INPUT_PULLUP) onState=!onState;
 return onState;
}

void switchRelays(byte howMany)
{ // switch “howMany” relays to on
 for (int i=0;i<NUMRELAYS;i++)
 {
   if (i<howMany)
   {
     digitalWrite(relayPins[i],RELAY_ACTIVE);
     Serial.print(‘1’);
   }
   else
   {
     digitalWrite(relayPins[i],!RELAY_ACTIVE);
     Serial.print(‘0’);
   }
 }
 Serial.println();
}

void setup() {
 Serial.begin(9600);
 Serial.println(“Good night and good luck!”);
 pinMode(keyPin, keyPinMode);
 for (int i=0;i<NUMRELAYS;i++)
 {
   digitalWrite(relayPins[i], !RELAY_ACTIVE);
   pinMode(relayPins[i], OUTPUT);
 }
}

byte lastSwitchState;
unsigned long switchTime;
byte relayCount;

void loop() {
 byte switchState=readKeySwitch();
 if (switchState!=lastSwitchState)
 {
   switchTime=millis();
   lastSwitchState=switchState;
 }
 if (switchState && relayCount<NUMRELAYS)
 { // key switch is on, but not all relays are on
   if (millis()- switchTime >= INTERVAL)
   {
     switchTime+=INTERVAL;
     relayCount++;
   }
 }
 else if (!switchState && relayCount>0)
 { // key switch is off, but there is at least one relay on
   if (millis()- switchTime >= INTERVAL)
   {
     switchTime+=INTERVAL;
     relayCount–;
   }
 }
 switchRelays(relayCount);
}




As I don't know whether you are using pull-up or pull-down resistors with your key swith and whether your relays are switching on "active HIGH" or "active LOW", I have prepared the sketch for all combinations.

Just set accordingly in the source code:


#define keyPinMode INPUT // if key switch is connected to a pull-down resistor



or


#define keyPinMode INPUT_PULLUP // if key switch shall use the “internal pull-up” resistor in the controller




And depending on the type of relays:


#define RELAY_ACTIVE LOW  // relay switches on LOW signal



or


#define RELAY_ACTIVE HIGH  // relay switches on HIGH signal




If everything works as expected, you can remove all the "Serial" debug code from the sketch.

Have fun!

Thanks for the program,

I tested the program, it works better as my program.

I have two questions:

  1. By switching power on is it possible that the relais not does start up?

  2. I must start the relais after a individual setting delay because not all the relais have the same delay.

I think this is now one delay for all relays,

Is that possible to make it with the individual delays for startup and for switching down? (such as in my program)

gert848:

  1. By switching power on is it possible that the relais not does start up?

As I already wrote in reply #6, the code I posted can be easily adapted to the hardware and circuit you are using.

If you have problems to find the correct settings for "keyPinMode" and for "RELAY_ACTIVE", just show detailed information about:

  • type of button used
  • schematic of button connected to the Arduino (including pull-up or pull-down resistors, if used)
  • type and schematics of relays used

gert848:
2. I must start the relais after a individual setting delay because not all the relais have the same delay.

If you can define exactly the timing to use while switching on, while switching off, while aborting switching on and while aboirting switching off, you can have everything different as you want it. Will require some changes in the code, though. Depends on what you actually want.