controlling 3 relays with 3 buttons

Hi,

I am new to Arduino and programming. I built my card and tested it it is working. For now, I only sucseeded to control one relay with one button.

What I'm doing is:

I have three voltage cutter boards for battery low voltage cutting. They work as buttons at my setup and they stop sending 5V to arduinos digital input when the connected batteries voltage drops.

My aim is to activate and de-activate these relays whenever voltage cutters send signal to Arduino.

Here is my sketch:

int role1 = 1;
int role2 = 2;
int role3 = 3;
int role4 = 4;
int role5 = 5;
int role6 = 6;
int volt1 = 45;
int volt2 = 49;
int volt3 = 53;
int voltstate1 = HIGH;
int voltstate2 = HIGH;
int voltstate3 = HIGH;
int reading1 ;
int reading2 ;
int reading3 ;
int previous1 = LOW;
int previous2 = LOW;
int previous3 = LOW;
long time1 = 0;
long time2 = 0;
long time3 = 0;
long debounce1 = 200;
long debounce2 = 200;
long debounce3 = 200;

void setup() {

pinMode(role1, OUTPUT);
pinMode(role2, OUTPUT);
pinMode(role3, OUTPUT);
pinMode(role4, OUTPUT);
pinMode(role5, OUTPUT);
pinMode(role6, OUTPUT);
pinMode(volt1, INPUT);
pinMode(volt2, INPUT);
pinMode(volt3, INPUT);
}

void loop() {

reading1 = digitalRead(volt1);

if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
delay (2000);
if (voltstate1 == HIGH) {
voltstate1 = LOW;
}
else {
voltstate1 = HIGH;
}

time1 = millis();
}

digitalWrite(role3, voltstate1);

previous1 = reading1;
}


In this sketch, not used other three relays (role4, 5 and 6) will be used to activate the battery chargers later. (this is kind of easy to do)

I just need a three button, three relay (or led, actually it doesn't matter) sketch example. Using if statements inside if statements is not practical for my approach. I guess there should be an easier way. If you can help me I will really appriciate it.

I think you can do it this way... three inputs, three relays.

I used arrays to simplify... note that you can really eliminate a lot of code of you do it that way.

I also look for a state change on the pins... that is, the sensor pins switching from high to low.

try it and see if it works for you (I compiled it but couldn't test it).

int voltagePin[3] = {2,3,4};// you do not want to use digital pins 0 or 1
int relayPin[3] = {5,6,7};
int lastVoltage[3];
int nowVoltage[3];


void setup() 
{
  Serial.begin(115200);
  for (int i = 0; i < 3; i++)
  {
    pinMode(relayPin[i], OUTPUT);
    Serial.print("Relay on pin"); Serial.print(relayPin[i]); Serial.println(" now set up.");
    pinMode(voltagePin[i], INPUT); 
    Serial.print("Voltage sensor on pin"); Serial.print(voltagePin[i]); Serial.println(" now ready");
  }
}

void loop() 
{
  for (int i = 0; i < 3; i++)
  {
    nowVoltage[i] = digitalRead(voltagePin[i]);
    if (nowVoltage[i] == HIGH && lastVoltage[i] == LOW)
    {
      digitalWrite(relayPin[i], HIGH);
      Serial.print("Relay on pin"); Serial.print(relayPin[i]); Serial.println(" now HIGH");
    }
    else if (nowVoltage[i] == LOW && lastVoltage[i] == HIGH)
    {
      digitalWrite(relayPin[i], LOW);
      Serial.print("Relay on pin"); Serial.print(relayPin[i]); Serial.println(" now LOW");
    }
    lastVoltage[i] = nowVoltage[i];
  }
}

BulldogLowell:
I think you can do it this way... three inputs, three relays.

I used arrays to simplify... note that you can really eliminate a lot of code of you do it that way.

I also look for a state change on the pins... that is, the sensor pins switching from high to low.

try it and see if it works for you (I compiled it but couldn't test it).

Thank you for quick response. I didn't know anything about arrays. That will simplify the code. Thank you very much. :slight_smile:

I tested the code. It controls the relays; but acts wierd and I couldn't come up with any solutions.

My aim is:

I have three sets of batteries to power up a hi-fi pc. These three seperate sets are made of two batteries. Three voltage cutter boards check the voltage going to the pc. If voltage drops under 11.3V the cutter stops sending 5V to Arduino. At this point arduino activates a relay which switches to the other battery. When batteries are switched my voltage cutter board starts sending voltage to arduino since new connected battery is full. At this point arduino should not switch the relay again; untill cutter stops sending signal to arduino again. I need to work this model for three battery sets independently. By this, one battery will power up CPU and other high noise components. The other battery will power up mainboard (which needs lowest noise possible for higher sound quality). A 5V battery will power up the soundcard. I use two pico atx 12V powersupplys for pc. And in each battery set, while one battery powers the pc, the other will be charged. These three independent packs at total will run a hi-fi pc, which can compete with high-end cd decks by means of sound quality. :smiley:

But with the code above, when voltage cutter cuts the 5V input, relay1 activates for a few miliseconds; but I need it to stay activated until another 5V cut happens.

more interestingly, when I connect my batteries to second voltage cutter, relay2 starts to open and close 3-4 times in a second. So with same voltage cutter, with same batteries, with same setup, Arduino commands relay1 and 2 differently. In my code relays were working exactly the same.

So I need to work on the code you sent me a bit more to find out what I am missing and untill now, I couldn't find the problem. But thanks alot! :slight_smile:

good... think about creating a state for each condition that you describe, then we can modify your program to behave differently based on those known states.