Ajuda com Modulo 4 reles

Olá a todos. Estou precisando de uma ajuda sobre como controlar um modulo de 4 reles separadamente. Exemplo

ReleA - vai ficar ativo por 10s
ReleB - vai ficar ativo após o ReleA desativar e ficara ativo por 10s
ReleC - vai ficar ativo somente após o ReleB desativar e também ficara ativo por 10s

int releA = 2; // rele 1
int releB = 3; // rele 2
int releC = 4; // rele 3
// int releD = 5;

void setup() {
pinMode(releA, OUTPUT);
pinMode(releB, OUTPUT);
pinMode(releC, OUTPUT);
// pinMode(releD, OUTPUT);
}
void loop() {
if (digitalRead(releC)==LOW) {
digitalWrite(releA,HIGH);
delay (6000);
digitalWrite(releA,LOW);
}
if (digitalRead(releA)==LOW) {
digitalWrite(releB,HIGH);
delay (5000);
digitalWrite(releB,LOW);
}
if (digitalRead(releB)==LOW) {
digitalWrite(releC,HIGH);
delay (1000);
digitalWrite(releC,LOW);
}
}

Após isso começar o ciclo novamente.

This is the English section of forum. Please write in English or post in Your native section.

Your program code works. Are you asking for something different?
Seu código de programa funciona. Você está pedindo algo diferente?

int releA = 2; // rele 1
int releB = 3; // rele 2
int releC = 4; // rele 3
int releD = 5; // rele 4

int onTime = 10 * 1000;

void setup() {
  pinMode(releA, OUTPUT);
  pinMode(releB, OUTPUT);
  pinMode(releC, OUTPUT);
  pinMode(releD, OUTPUT);
}
void loop() {
  if (digitalRead(releD) == LOW) {
    digitalWrite(releA, HIGH);
    delay (onTime);
    digitalWrite(releA, LOW);
  }
  if (digitalRead(releA) == LOW) {
    digitalWrite(releB, HIGH);
    delay (onTime);
    digitalWrite(releB, LOW);
  }
  if (digitalRead(releB) == LOW) {
    digitalWrite(releC, HIGH);
    delay (onTime);
    digitalWrite(releC, LOW);
  }
  if (digitalRead(releC) == LOW) {
    digitalWrite(releD, HIGH);
    delay (onTime);
    digitalWrite(releD, LOW);
  }
}

Another method is using arrays.
Outro método é usar arrays.

int relePino[] = {2, 3, 4, 5}; // array
int ligarTempo = 10 * 1000; // milliseconds

void setup() {
  for (int i = 0; i < 4; i++) { // pino 0, 1, 2, 3
    pinMode(relePino[i], OUTPUT);
  }
}

void loop() {
  for (int i = 0; i < 4; i++) { // pino 0, 1, 2, 3
    if (digitalRead(!relePino[3 - i % 4])) // leia rele 3, 2, 1, 0
      digitalWrite(relePino[i], HIGH);
    delay(ligarTempo);
    digitalWrite(relePino[i], LOW);
  }
}
Meu primeiro exemplo é ruim. Ele lê o pino de LED errado. Este lê o pino de LED correto.
int relePino[] = {2, 3, 4, 5}; // pino array
int ligarTempo = 10 * 1000UL; // milliseconds ** MUST USE "UL" after 1000

void setup() {
  for (int i = 0; i < 4; i++) { // pino 0, 1, 2, 3
    pinMode(relePino[i], OUTPUT);
    digitalWrite(relePino[i], LOW);
  }
}

void loop() {
  int pino;
  for (int i = 0; i < 4; i++) { // array pino 0, 1, 2, 3
    if (i == 0) // no pino 0...
      pino = 3; // ... leia o array pino 3
    else
      pino = i - 1; // ... caso contrário, leia o PIN anterior
    if (digitalRead(!relePino[pino])) {
      digitalWrite(relePino[i], HIGH);
      delay(ligarTempo);
      digitalWrite(relePino[i], LOW);
    }
  }
}

Thank you for taking the time to help me with this project. I will test this code tomorrow when I arrive at the company. But thank you very much in advance.

I understood the code line. Set each relay to be active for 10s. That would be more or less what I needed. I'll try to explain better.

The machine we are trying to automate is a pin drill. It has 3 pistons activated via electrical pulse and has a motor.

The first relay needed to be active for 30s. After that he hung up. and the 2nd relay activated for 10s (time for the piston to work) then the 3rd relay would also activate 10s and the 4th same time. After this loop returns to 1 with its time.

You can practice using this simulator...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.