Programming relays without delays

sp. "benefit", of course.

And rather than have all those functions the code would be much neater with a single function that takes parameters - for example

void switchRelay(byte relayNum, byte relayState) {
   digitalWrite(relayNum, relayState);
}

which could be called like this

if (relayvar == 1) {
       switchRelay(relay_1, ON);
}

assuming ON is defined as 1 and OFF is defined as 0

...R

chickwolf:
Was this not explained in my first post?

Anyway, I am trying to make a sequence of relays turn on and off speakers and lights at different intervals. Something will pulse the Arduino, which will turn on relay one lets say, on the next pulse, it will turn it off, on the third pulse, relay 4 will turn on, etc etc. I actually have two Arduinos running very similar iterations of the code, one for the first four relays and one four the second for, not that this is relevant though.

Hope this clears things up a bit, not looking to provide any confusion.

Which leads me to a concern about you wanting to "turn on and off speakers". Are you planning on opening up one of the wires going to a speaker? I surely hope not, unless the source of power for the speaker is designed to function into an open circuit. Normally that would be sure death for a device. Switching to an equivalent resistor across the line may work, but will take a SPDT relay.

In any event, the switching will make a rather loud "click" in the speaker

Paul

Robin2:
Remove line 159 so you only read the button once.

Removed, still having the same issue.

Paul_KD7HB:
Which leads me to a concern about you wanting to "turn on and off speakers". Are you planning on opening up one of the wires going to a speaker? I surely hope not, unless the source of power for the speaker is designed to function into an open circuit. Normally that would be sure death for a device. Switching to an equivalent resistor across the line may work, but will take a SPDT relay.

In any event, the switching will make a rather loud "click" in the speaker

Didn't know this would be an issue, was planning to just split a 3.5mm jack output over several speakers, only having one one at a time. The clicking may get annoying.

Thanks for the advice

chickwolf:
Removed, still having the same issue.

Post the latest version of the program.

...R

Robin2:
Post the latest version of the program.

Would be the same as the last version, just with the line you posted missing.

int relay_1 = 4;
int relay_2 = 7;
int relay_3 = 8;
int relay_4 = 12;
int relay_5 = 0;
int relay_6 = 0;
int relay_7 = 0;
int relay_8 = 0;
int relayvar = 0;
int button_State = 0;
int last_Button_State = 0;

void relay_1_high() {
  digitalWrite(relay_1, HIGH);
}
void relay_1_low() {
  digitalWrite(relay_1, LOW);
}
void relay_2_high() {
  digitalWrite(relay_2, HIGH);
}
void relay_2_low() {
  digitalWrite(relay_2, LOW);
}
void relay_3_high() {
  digitalWrite(relay_3, HIGH);
}
void relay_3_low() {
  digitalWrite(relay_3, LOW);
}
void relay_4_high() {
  digitalWrite(relay_4, HIGH);
}
void relay_4_low() {
  digitalWrite(relay_4, LOW);
}
void relay_5_high() {
  digitalWrite(relay_5, HIGH);
}
void relay_5_low() {
  digitalWrite(relay_5, LOW);
}
void relay_6_high() {
  digitalWrite(relay_6, HIGH);
}
void relay_6_low() {
  digitalWrite(relay_6, LOW);
}
void relay_7_high() {
  digitalWrite(relay_7, HIGH);
}
void relay_7_low() {
  digitalWrite(relay_7, LOW);
}
void relay_8_high() {
  digitalWrite(relay_8, HIGH);
}
void relay_8_low() {
  digitalWrite(relay_8, LOW);
}

void setup()
{
  // sets the relays pins as outputs
  pinMode(relay_1, OUTPUT); pinMode(relay_2, OUTPUT);
  pinMode(relay_3, OUTPUT); pinMode(relay_4, OUTPUT);
  pinMode(2, OUTPUT); pinMode(3, INPUT); pinMode(13, OUTPUT);
  digitalWrite(2, HIGH); pinMode(3, LOW); digitalWrite(13, LOW);
  Serial.begin(57600);
  Serial.print(relayvar);
}
void loop()
{
  button_State = digitalRead(3);


  if (button_State != last_Button_State)
  {
    if (button_State == HIGH)
    {
      relayvar = relayvar + 1;
      if (relayvar == 1) {
        relay_1_high();
      }
      else if (relayvar == 2) {
        relay_1_low();
      }
      else if (relayvar == 3) {
        relay_2_high();
        relay_4_high();
      }
      else if (relayvar == 4) {
        relay_2_low();
        relay_4_low();
      }
      else if (relayvar == 5) {
        relay_3_high();
        relay_4_high();
      }
      else if (relayvar == 6) {
        relay_3_low();
        relay_4_low();
      }
      else if (relayvar == 7) {
        relay_1_high();
      }
      else if (relayvar == 8) {
        relay_1_low();
      }
      else if (relayvar == 9) {
        relay_6_high();
        relay_8_high();
      }
      else if (relayvar == 10) {
        relay_6_low();
        relay_8_low();
      }
      else if (relayvar == 11) {
        relay_6_high();
        relay_7_high();
      }
      else if (relayvar == 12) {
        relay_6_low();
        relay_7_low();
      }
      else if (relayvar == 13) {
        relay_1_high();
      }
      else if (relayvar == 14) {
        relay_1_low();
      }
      else if (relayvar == 15) {
        relay_2_high();
      }
      else if (relayvar == 16) {
        relay_2_low();
      }
      else if (relayvar == 17) {
        relay_1_high();
      }
      else if (relayvar == 18) {
        relay_1_low();
      }
      else if (relayvar == 19) {
        relay_2_high();
      }
      else if (relayvar == 20) {
        relay_2_low();
      }
      else if (relayvar == 21) {
        relay_1_high();
      }
      else if (relayvar == 22) {
        relay_1_low();
      }
      else if (relayvar == 23) {
        relayvar = 0;
      }
    }
    last_Button_State = button_State;
  }
}

Have gotten hold of an amplifier which seems to be removing the relay click and also (I have been told) wont have the open circuit issue that was spoken about earlier.

Have been looking at code for trying to fix the switch denouncing and came across this example:

Have tried implementing in but I am fairly sure I am screwing up the process at some point. This is my new adapted code with denouncing stuff added, anyone see any glaring issues?

int relay_1 = 4;
int relay_2 = 7;
int relay_3 = 8;
int relay_4 = 12;
int relay_5 = 0;
int relay_6 = 0;
int relay_7 = 0;
int relay_8 = 0;
int relayvar = 0;
int button_State = 0;
int last_Button_State = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void relay_1_high() {
  digitalWrite(relay_1, HIGH);
}
void relay_1_low() {
  digitalWrite(relay_1, LOW);
}
void relay_2_high() {
  digitalWrite(relay_2, HIGH);
}
void relay_2_low() {
  digitalWrite(relay_2, LOW);
}
void relay_3_high() {
  digitalWrite(relay_3, HIGH);
}
void relay_3_low() {
  digitalWrite(relay_3, LOW);
}
void relay_4_high() {
  digitalWrite(relay_4, HIGH);
}
void relay_4_low() {
  digitalWrite(relay_4, LOW);
}
void relay_5_high() {
  digitalWrite(relay_5, HIGH);
}
void relay_5_low() {
  digitalWrite(relay_5, LOW);
}
void relay_6_high() {
  digitalWrite(relay_6, HIGH);
}
void relay_6_low() {
  digitalWrite(relay_6, LOW);
}
void relay_7_high() {
  digitalWrite(relay_7, HIGH);
}
void relay_7_low() {
  digitalWrite(relay_7, LOW);
}
void relay_8_high() {
  digitalWrite(relay_8, HIGH);
}
void relay_8_low() {
  digitalWrite(relay_8, LOW);
}

void setup()
{
  // sets the relays pins as outputs
  pinMode(relay_1, OUTPUT); pinMode(relay_2, OUTPUT);
  pinMode(relay_3, OUTPUT); pinMode(relay_4, OUTPUT);
  pinMode(2, OUTPUT); pinMode(3, INPUT); pinMode(13, OUTPUT);
  digitalWrite(2, HIGH); pinMode(3, LOW); digitalWrite(13, LOW);
  Serial.begin(57600);
  Serial.print(relayvar);
}
void loop()
{
  button_State = digitalRead(3);
  if (button_State != last_Button_State) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    if (button_State != last_Button_State)
    {

      if (button_State == HIGH)
      {
        relayvar = relayvar + 1;
        if (relayvar == 1) {
          relay_1_high();
        }
        else if (relayvar == 2) {
          relay_1_low();
        }
        else if (relayvar == 3) {
          relay_2_high();
          relay_4_high();
        }
        else if (relayvar == 4) {
          relay_2_low();
          relay_4_low();
        }
        else if (relayvar == 5) {
          relay_3_high();
          relay_4_high();
        }
        else if (relayvar == 6) {
          relay_3_low();
          relay_4_low();
        }
        else if (relayvar == 7) {
          relay_1_high();
        }
        else if (relayvar == 8) {
          relay_1_low();
        }
        else if (relayvar == 9) {
          relay_6_high();
          relay_8_high();
        }
        else if (relayvar == 10) {
          relay_6_low();
          relay_8_low();
        }
        else if (relayvar == 11) {
          relay_6_high();
          relay_7_high();
        }
        else if (relayvar == 12) {
          relay_6_low();
          relay_7_low();
        }
        else if (relayvar == 13) {
          relay_1_high();
        }
        else if (relayvar == 14) {
          relay_1_low();
        }
        else if (relayvar == 15) {
          relay_2_high();
        }
        else if (relayvar == 16) {
          relay_2_low();
        }
        else if (relayvar == 17) {
          relay_1_high();
        }
        else if (relayvar == 18) {
          relay_1_low();
        }
        else if (relayvar == 19) {
          relay_2_high();
        }
        else if (relayvar == 20) {
          relay_2_low();
        }
        else if (relayvar == 21) {
          relay_1_high();
        }
        else if (relayvar == 22) {
          relay_1_low();
        }
        else if (relayvar == 23) {
          relayvar = 0;
        }
        button_State = digitalRead(3);
      }
      last_Button_State = button_State;
    }
  }
}
void relay_1_high() {
  digitalWrite(relay_1, HIGH);
}
void relay_1_low() {
  digitalWrite(relay_1, LOW);
}

Pretty glaring - if these functions provided an layer of abstraction, I could understand, but they simply duplicate functionality.

AWOL:

void relay_1_high() {

digitalWrite(relay_1, HIGH);
}
void relay_1_low() {
  digitalWrite(relay_1, LOW);
}


Pretty glaring - if these functions provided an layer of abstraction, I could understand, but they simply duplicate functionality.

They could provide platform independence. Not that they actually do. Just saying.

AWOL:

void relay_1_high() {

digitalWrite(relay_1, HIGH);
}
void relay_1_low() {
  digitalWrite(relay_1, LOW);
}


Pretty glaring - if these functions provided an layer of abstraction, I could understand, but they simply duplicate functionality.

I get that it probably doesn't make sense, works though. Anything wrong with how I have done the switch debouncing?

Why has line 159 crept back into the code in Reply #26 - albeit now as line 166?

Life would be much easier if you put the code for reading your button into a short function that you test independently of the rest of your code. Especially as you seem to have an enthusiasm for functions.

The way the code is now my mind can't see the button code on its own.

...R