Hello every one, need help with 2x4 relay modules

You will need to learn enough to get by :stuck_out_tongue_winking_eye: I did, am, will still be years from now…

I made this simple example months ago… Coded for Local Server and only 4 relays, so you will need to make those adjustments at least. I haven’t gotten around to posting it on my Example Topic yet, but here it is:

/***************** Library ESP and Blynk *****************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char server[] = "xxx.xxx.xxx.xxx";
int port = 8080;
int count;
int ON = 0;
int OFF = 1;

// Set virtual LEDS to these vPins... but these commands are not required since using direct virtualWrite() commands
//WidgetLED led1(V3);
//WidgetLED led2(V4);
//WidgetLED led3(V5);
//WidgetLED led4(V6);

/*************** Array of Relays *********************/
int relay[] = {5, 4, 14, 12}; // The relay pin order for triggering based on count (GPIO pins, NOT using Dpin labeling)



/*************** Setup *********************/
void setup()
{
  Serial.begin(115200);

  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();

  // Set all array pinmodes and set LOW
  for (count = 0; count <= 3; count++) {
    pinMode(relay[count], OUTPUT);
    digitalWrite(relay[count], OFF);
  }

}



/*************** Forward pattern *********************/
BLYNK_WRITE(V1) {  // Button-Switch Widget
  if (param.asInt() == 1) {
    for (count = 0; count < 4; count++) {  // Count forward
      //Serial.print(count);
      Blynk.virtualWrite(count+3, 255);  // Virtual LED ON (EG count 0 + 3 = V3) 
      digitalWrite(relay[count], ON);  // Toggle relays ON, based on array and count order
      Blynk.virtualWrite(count+3, 0);  // Virtual LED OFF
      digitalWrite(relay[count], OFF);  // Toggle relays OFF
    }
    Blynk.syncVirtual(V1);  // Repeat if switch still ON
  }
  else {  // Stop all relays if switch OFF
    for (count = 0; count <= 3; count++) {
      digitalWrite(relay[count], OFF);
    }
  }
}



/*************** Backward pattern *********************/
BLYNK_WRITE(V2) {  // Button-Switch Widget
  if (param.asInt() == 1) {  // Count backward
    for (count = 3; count >= 0; count--) {
      //Serial.print(count);
      Blynk.virtualWrite(count+3, 255);  // Virtual LED ON (EG count 0 + 3 = V3) 
      digitalWrite(relay[count], ON);  // Toggle relays ON, based on array and count order
      Blynk.virtualWrite(count+3, 0);  // Virtual LED OFF
      digitalWrite(relay[count], OFF);  // Toggle relays OFF
    }
    Blynk.syncVirtual(V2);
  }
  else {  // Stop all relays if switch OFF
    for (count = 0; count < 4; count++) {
      digitalWrite(relay[count], OFF);
    }
  }
}



/*************** Loop *********************/
void loop() {
  Blynk.run();
}
1 Like