Problems with Relay-Plate and Arduino Mega 2560

Hello
I'm new to programming an Arduino. I bought an Arduino and a relay board from Elegoo. I want to use this to control the heated seats in my car. each button should switch a relay. The seat heating has 2 levels per side. one stage is to be turned off when the other is turned on. In addition, the heating should switch off when the switch for the active level is actuated. I wrote this program for it, but the heating cannot be switched off. can anyone tell me if the program is buggy?
The buttons are all connected to the digital Pins, +5V and with a 10k Ohm Resistor to GND. That must be the right way, i thought.

Thank you and i hope you can help me.

int RFahrer1=13; //Relais Fahrer Stufe 1
int RFahrer2=12; //Relais Fahrer Stufe 2
int RBeifahrer1=11; //Relais Beifahrer Stufe 1
int RBeifahrer2=10; //Relais Beifahrer Stufe 2
int Fahrer1=2; //Schalter Fahrer Stufe 1
int Fahrer2=3; //Schalter Fahrer Stufe 2
int Beifahrer1=4; //Schalter Beifahrer Stufe 1
int Beifahrer2=5; //Schalter Beifahrer Stufe 2

void setup() {
  // put your setup code here, to run once:
pinMode(RFahrer1, OUTPUT);
pinMode(RFahrer2, OUTPUT);
pinMode(RBeifahrer1, OUTPUT);
pinMode(RBeifahrer2, OUTPUT);
digitalWrite(RFahrer1, 10);
digitalWrite(RFahrer2, 10);
digitalWrite(RBeifahrer1, 10);
digitalWrite(RBeifahrer2, 10);
pinMode(Fahrer1, INPUT);
pinMode(Fahrer2, INPUT);
pinMode(Beifahrer1, INPUT);
pinMode(Beifahrer2, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
//Sitzheizung Fahrer Stufe 1
if(digitalRead(Fahrer1)== HIGH){
  digitalWrite(RFahrer2, HIGH);
  digitalWrite(RFahrer1, LOW);  
  digitalWrite(Fahrer1, LOW);
  }
if(digitalRead(Fahrer1) == HIGH && RFahrer1 == LOW){
  digitalWrite(RFahrer1, HIGH); 
  digitalWrite(Fahrer1, LOW); 
}
//Sitzheizung Fahrer Stufe 2
if(digitalRead(Fahrer2)== HIGH){
  digitalWrite(RFahrer1, HIGH);
  digitalWrite(RFahrer2, LOW);
  digitalWrite(Fahrer2, LOW);  
  }
if(digitalRead(Fahrer2)==HIGH && RFahrer2==LOW){
  digitalWrite(RFahrer2, 10);
  digitalWrite(Fahrer2, LOW);  
}
//Sitzheizung Beifahrer Stufe 1
if(digitalRead(Beifahrer1)== HIGH){
  digitalWrite(RBeifahrer2, HIGH);
  digitalWrite(RBeifahrer1, LOW);
  digitalWrite(Beifahrer1, LOW);  
  }
if(digitalRead(Beifahrer1)==HIGH && RBeifahrer1==LOW){
  digitalWrite(RBeifahrer1, HIGH);
  digitalWrite(Beifahrer1, LOW);  
}
//Sitzheizung Beifahrer Stufe 2
if(digitalRead(Beifahrer2)== HIGH){
  digitalWrite(RBeifahrer1, HIGH);
  digitalWrite(RBeifahrer2, LOW);
digitalWrite(Beifahrer2, LOW);    
  }
if(digitalRead(Beifahrer2)==HIGH && RBeifahrer2==LOW){
  digitalWrite(RBeifahrer2, HIGH);
  digitalWrite(Beifahrer2, LOW);  
}  
   
}


Yes, it's buggy while it doesn't do what You want.

Nice to find code tags used but You forgot autoformat it before pasting.

Looking briefly at the code, schematics are needed.

If you did NOT test each phase of your program, as you added it, then it defaults to being buggy. Go back and prove you have each of the switches wired properly and your code is recognizing when a switch is on and when it is off.
How much current is used by each of the seat heaters? What is the 12 volt current rating of your relays? Did you test each of the relay operations as you developed the program? If not, go back and begin verifying that you can actually control the off and on of each relay.
I suspect there is a lot of guessing in your program.

A few suggestions for debugging:

  • Add some Serial.println statements that print out the interesting variables in various stages.
  • Add delay(1000); at the end of the loop for human-readable testing.
  • After debugging is complete, you might want to replace the 1000 by a lower value (e.g. 200) to debounce switches and contain the risk of driving relays on and off each cycle.

Hi, @viper07

Can you please post a copy of your circuit?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

How have you got your buttons wired?
It looks like you are testing for HIGH on the button inputs.
This sounds like each of your buttons is between 5V and the digital input pin.
You need a 10K resistor between each digital input and the Mega gnd to make sure the input goes LOW when the button is released.

Tom.. :smiley: :+1: :coffee: :australia:

Hello viper07

Do you have experience with programming in C++?

This task can easily be realised with objects.
A structured array contains all the information, such as the pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?

Have a nice day and enjoy coding in C++.

Hello viper07

Try, check, study and modify this OOP sketch:

/* BLOCK COMMENT
  - https://forum.arduino.cc/t/problems-with-relay-plate-and-arduino-mega-2560/1096170
  - Problems with Relay-Plate and Arduino Mega 2560
  - This sketch may contain traces of C++.
  - In case of indisposition:
  - https://www.learncpp.com/
  - Hardware:
  - Thanks to LarryD
  - https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  - https://forum.arduino.cc/t/beginners-software-needs-hardware/1066703
  - Tested @ Arduino: Mega[X] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Problems with Relay-Plate and Arduino Mega 2560"
// make names
enum Driver {Fahrer, Beifahrer};
enum Stage  {One, Two};
// make variables
constexpr int Buttons[][2] {
  {A0, A1},
  {A2, A3},
};
constexpr int Relays[][2] {
  {9, 10},
  {11, 12},
};
// make structures 
struct DEBOUNCE
{
  const unsigned long Duration;
  unsigned long stamp;
};
struct BUTTON2RELAY
{
  const int Identifier;
  const int Relay;
  const int Button;
  int stateOld;
  DEBOUNCE debounce;

};
BUTTON2RELAY button2Relays[] [2] {
  {Fahrer, Relays[Fahrer][One], Buttons[Fahrer][One], false, 20, 0, Fahrer, Relays[Fahrer][Two], Buttons[Fahrer][Two], false, 20, 0},
};
// tools
void heartBeat(int LedPin)
{
  static bool setUp = false;
  if (!setUp) pinMode (LedPin, OUTPUT), setUp = !setUp;
  digitalWrite(LedPin, (millis() / 1000) % 2);
}
void setup()
{
  Serial.begin(115200);
  Serial.print(ProjectName), Serial.print(" in "), Serial.println(__FILE__);
  for (auto &button2Relay : button2Relays)
  {
    for (auto &toDo : button2Relay) {
      pinMode(toDo.Relay, OUTPUT);
      digitalWrite(toDo.Relay, HIGH);
      delay(1000);
      digitalWrite(toDo.Relay, LOW);
      pinMode(toDo.Button, INPUT_PULLUP);
      toDo.stateOld = digitalRead(toDo.Button) ? LOW : HIGH;
    }
  }
}
void loop()
{
  heartBeat(LED_BUILTIN);
  unsigned long currentMillis = millis();
  for (auto &button2Relay : button2Relays)
  {
    for (auto &toDo : button2Relay) {
      if ( currentMillis - toDo.debounce.stamp >= toDo.debounce.Duration)
      {
        toDo.debounce.stamp = currentMillis;
        int stateNew = digitalRead(toDo.Button) ? LOW : HIGH;
        if (toDo.stateOld != stateNew)
        {
          toDo.stateOld = stateNew;
          if (stateNew == HIGH)
          {
            switch (digitalRead(toDo.Relay))
            {
              case LOW:
                for (auto &Relay : Relays[toDo.Identifier]) digitalWrite(Relay, LOW);
                digitalWrite(toDo.Relay, HIGH);
                break;
              case HIGH:
                digitalWrite(toDo.Relay, digitalRead(toDo.Relay) ? LOW : HIGH);
                break;
            }
          }
        }
      }
    }
  }
}

Have a nice day and enjoy coding in C++.

You will need better relays than the common elegoo hobby board, heaters are fairly high current and will either burn or weld the relay contacts fairly quickly.

Thank you all for your answers.
in C++ i got no experience, i programmed a little bit in school, a few years ago, with java. this was just the result of one evening with YouTube.
Especially the Code of @paulpaulson looks pretty nice, but i don´t understand many lines of this code.
i had solved the problem with my code already. there was a fault in the if-statements.
So now it works. But thank you for your suggestions and inspirations.