Cyclic Relay2A & 2B

Hi

I have no clue how to upgrade my code to make it so relays 2A&2B cycle when they are triggered..

at the moment they both turn on together.
how do is cycle them when turned on?

void setup() {
  // put your setup code here, to run once:
//serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
int value = digitalRead(2);

//RELAY1
if(value == 1)
{
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}

//RELAY2A
if(value == 0)
{
digitalWrite(7, HIGH);
} else {
digitalWrite(7, LOW);
}


//RELAY2B
if(value == 0)
{
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}



}

Kind Regards
Shaun

What’s the cycle pattern?
Alternating… a pause between each, how many times ?
There’s a lot of info missing.

Thanks for using code tags…
Perhaps if you explain the application, we can offer a better solution.

Or simply
digitalWrite(3, digitalRead(2);
Leo..

Hi

I have an input which sends Pulsed High and Low

I want relay1 to turn on in sync with input

but relay 2A&2B to turn on cyclic mode when in put is off

so

  1. input on = relay1 on
  2. input off = relay2A on
  3. input on = relay1 on
  4. input off = relay2B on
    repeat.....

hope it helps

s.

bool pinState; // pin 2

void setup() {
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop() {
  pinState = digitalRead(2);
  digitalWrite(3, pinState);
  digitalWrite(7, !pinState);
}

Leo..

sorry, that does not work for me

its only cycle 2 relays

i want it like this
1, input on = relay1 on (other relays off)
2, input off = relay2A on (other relays off)
3, input on = relay1 on (other relays off)
4, input off = relay2B on (other relays off)

repeat......

Thanks
s.

Hello
It seems ro be a piece of home work, isn´t it?

consider

#undef  MyHW
#ifdef MyHW
#define pinInp     A1
#define pinRly1    10
#define pinRly2A   11
#define pinRly2B   12

#else
#define pinInp     2
#define pinRly1    3
#define pinRly2A   7
#define pinRly2B   8
#endif

int valLst;
int state;

void setup () {
    Serial.begin (9600);

    pinMode (pinInp,   INPUT_PULLUP);
    pinMode (pinRly1,  OUTPUT);
    pinMode (pinRly2A, OUTPUT);
    pinMode (pinRly2B, OUTPUT);
}

void loop () {
    int value = digitalRead (pinInp);

    digitalWrite (pinRly1, value);

    if (valLst != value)  {
        valLst = value;

        if (value == 1) {
            digitalWrite (pinRly2A, LOW);
            digitalWrite (pinRly2B, LOW);
        }
        else  {
            Serial.println (state);

            if (state)
                digitalWrite (pinRly2A, HIGH);
            else
                digitalWrite (pinRly2B, HIGH);
            state = ! state;
        }
    }

    delay (10);         // debounce
}

Absolute Masterful Work gcjr

I looking into how you did it..
#undef MyHW
#ifdef MyHW
#define pinInp A1
#define pinRly1 10
#define pinRly2A 11
#define pinRly2B 12

these items confuse me
how are you making the 2a & 2b cycle?

is it the complex if statement or are you using serial monitor?

Hello
I have a Q&D made sketch that fits to your requierements.

//BLOCK COMMENT
//https://forum.arduino.cc/t/cyclic-relay2a-2b/889080/10
#define ProjectName "Cyclic Relay2A & 2B"
// CONSTANT DEFINITION
// you need to change these constants to your hardware
const byte Relays[] {2, 3, 4};
const byte Buttons[] {A0};
// VARIABLE DECLARATION
enum {One, TwoA, TwoB};
struct BUTTON {
  byte pin;
  bool state;
  int counter;
  unsigned long stamp;
  unsigned long duration;
} button {Buttons[One], 1, 0, 0, 20};
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  for (auto Relay : Relays) pinMode ( Relay, OUTPUT);
  for (auto Button : Buttons) pinMode ( Button, INPUT_PULLUP);
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  if (currentTime - button.stamp >= button.duration) {
    button.stamp = currentTime;
    bool stateNew = digitalRead(button.pin);
    if (button.state != stateNew) {
      button.state = stateNew;
      for (auto Relay : Relays) digitalWrite ( Relay, LOW);
      switch (button.counter) {
        case 0: digitalWrite ( Relays[One] , HIGH); break;
        case 1: digitalWrite ( Relays[TwoA] , HIGH); break;
        case 2: digitalWrite ( Relays[One] , HIGH); break;
        case 3: digitalWrite ( Relays[TwoB] , HIGH); break;
      }
      button.counter++;
      button.counter = button.counter % 4;
    }
  }
}

Enjoy the day and have fun.

i tested the code on hardware i have with buttons and LEDs. the ifdef allows changing constants for my hardware vs your hardware

by keep track of which relay is turned on last and the change in state of the input. change the relay 2 state only once per change in input state

Hello,
Thank you for the support thus far.

It truly means the world to me.

Okay I have got both PaulPaulSon code and gcjr code to work,
But I am currently working with Paulpaulson's code as I was able to implement a 4th relay in the mix by adding TwoC (a 4th relay) and this code..
case 4: digitalWrite ( Relays[One] , HIGH); break;
case 5: digitalWrite ( Relays[TwoC] , HIGH); break;

I am hoping I can get some further assistance to configure some additional code,

I am wanting to advance the circuit system to be able to:

Detect Input Signal.

Depend on how slow or quick the pulse goes, to be able to run 3(all) (TwoA, TwoB, TwoC) at the same time.

The INPUT pulse rate can vary from (once every 2 seconds), up to 10 times per second.
When the Pulse is SLOW (like every 2 Seconds) I would like all Relays (TwoA, B & C) same time
When the Pulse is faster (every 1 second) to have cycle between TwoA,B then TwoB,C
When the Pulse is Fastest (faster than 1 second) Standard Cycle TwoA, then TwoB, then TwoC.

Thanks again so much for the support you guys have given.
Code as it stands Now..

//BLOCK COMMENT
//https://forum.arduino.cc/t/cyclic-relay2a-2b/889080/10
#define ProjectName "Cyclic Relay2A & 2B"
// CONSTANT DEFINITION
// you need to change these constants to your hardware
const byte Relays[] {3, 7, 8, 4};
const byte Buttons[] {2};
// VARIABLE DECLARATION
enum {One, TwoA, TwoB, TwoC};
struct BUTTON {
  byte pin;
  bool state;
  int counter;
  unsigned long stamp;
  unsigned long duration;
} button {Buttons[One], 1, 0, 0, 20};
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  for (auto Relay : Relays) pinMode ( Relay, OUTPUT);
  for (auto Button : Buttons) pinMode ( Button, INPUT_PULLUP);
}
void loop () {
  unsigned long currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  if (currentTime - button.stamp >= button.duration) {
    button.stamp = currentTime;
    bool stateNew = digitalRead(button.pin);
    if (button.state != stateNew) {
      button.state = stateNew;
      for (auto Relay : Relays) digitalWrite ( Relay, LOW);
      switch (button.counter) {
        case 0: digitalWrite ( Relays[One] , HIGH); break;
        case 1: digitalWrite ( Relays[TwoA] , HIGH); break;
        case 2: digitalWrite ( Relays[One] , HIGH); break;
        case 3: digitalWrite ( Relays[TwoB] , HIGH); break;
        case 4: digitalWrite ( Relays[One] , HIGH); break;
        case 5: digitalWrite ( Relays[TwoC] , HIGH); break;
      }
      button.counter++;
      button.counter = button.counter % 6;
    }
  }
}

change to

button.counter = button.counter % 6;
1 Like