Double Relay and motor issue: URGENT

Hello all,

I am new to this so please be patient with me. I have a project that involves two double relays, two motors and a push button switch. When I wire everything together and run my code, double relay (1)only runs the motor(1) in one direction and instead of going the other direction the relay light flashes on and off quickly and continues in the same direction. I ran the relay without the motor and the lights on the relay flashed as programmed. The second motor and relay work just fine. I’m not sure why this is happening? Does anyone have any ideas? Oh and I tried to debug by having that relay and motor run on its own, and it ran perfectly. But once the push button switch and other relay and motor are added the issue begins. Any ideas?
Here is My Schematic:

CODE:

#define M1 2 // relay 1 Motor 1
#define M2 3 // relay 2 Motor 1
#define M3 4 // relay 3 Motor 2
#define M4 5 // relay 4 Motor 2
#define LED 7 // for LED
#define SWITCH 8 //switch
void setup() {
  pinMode(M1,OUTPUT); 
  pinMode(M2,OUTPUT);
  pinMode(M3,OUTPUT);
  pinMode(M4,OUTPUT);
  pinMode(Sensor,INPUT);
  pinMode(LED,OUTPUT);
  pinMode(SWITCH,INPUT);
  digitalWrite(M1,HIGH); // start with relay off
  digitalWrite(M2,HIGH); // start with relay off
  digitalWrite(M3,HIGH); // start with relay off
  digitalWrite(M4,HIGH); // start with relay off
  delay(1000); // pause for effect
}

void loop() { 
while (digitalRead(SWITCH) == LOW){ //does until switch is closed
  digitalWrite(LED,HIGH); //flash LED once
  delay(250);
  digitalWrite(LED,LOW);
  delay(250);
}
  digitalWrite(M3,LOW); //Motor 2 Direction 1
  delay(600);
  digitalWrite(M4,LOW); //off
  delay(5000); //brief pause to dissipate any voltage spikes
  digitalWrite(M1,LOW); //Motor 1 Direction 1
  delay(1900);
  digitalWrite(M2,LOW); //off
  delay(5000);
  digitalWrite(M3,HIGH); //Motor 2 Direction 2
  delay(600);
  digitalWrite(M4,HIGH);//off
  delay(5000); //brief pause to dissipate any voltage spikes
  digitalWrite(M1,HIGH); //Motor 1 Direction 2
  delay(1900);
  digitalWrite(M2,HIGH);//off
  delay(5000);

 }

Please share your code and schematic (circuit diagram)

You will get help faster if you provide the information that we need in order to help you. Please read the how to get the most from the forum post.

Please read the forum guidelines to see how to properly post code and some information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

the idea that solves everything is
providing the complete sketch and a fully detailed wiring diagram = schematic.

The other idea is: finding a magical chrystal ball where inside the chrystal-ball you can see the solution.

Hey I added my code and schematic. I didn't think it through when positing.

RE-edit your post to show your code as a code-section

The "H Bridge" which you have formed out of a DPDT relay to reverse the motor looks wrong to me. The N/O and N/C pins should be crossed over something like this:

EDIT

Added COM labels to schematic.

Thank you again. I was able to post it right !

Hey, I am sorry but I am a but confused on the schematic you sent. So the motor is suppose to be connected to the N/C not the COM?

Do you have a digital multimeter?
If you measure the "output" of your DPDT does the polarity change as expected?

I'm a electronic hobbiest and have quite some experience with schematics.
If I would like to analyse your schematic I would immidiately re-draw your schematic line by line so that the new drawing follows the common rules of how schematics shall be drawn.

And the most important difference would be to draw it in a way that lines do not cross more than nescessary and that the lines do not change their direction more than nescessary.

Using letters "NC" "C" and "NO"

instead of the typical relay-symbols
An Overview Of Control (Ice Cube) Relays
makes it very hard to follow

Have you considered that you have NO WAY to turn the motors off?

Yes I do have a millimeter and the polarity changed as expected when measured.

The code has the motors turn off when it is finished rotating for a specific amount of time. I am not sure what you mean. Do you mean that it is running forever ?

The schematic does not show any way to turn off the power to the motors.

Oh I see that now. That is how I set up so what do I add to turn off the power to the motors?

I am surprised you did not notice the motors running continuously! Add another relay to turn the power on for each motor.

How would I code it? And I only have one extra double relay.

@ana24 Your schematic doesn't match your code

In code you have used digital pin 2,3,4,5,7&8

But in schematic you are using pin 3,4,5,6,7&8

Also what do you mean by "double relay" are you using total 4 relay for two motors ?

To clarify, the OP's schematic in No.1
is correct. The motor is stopped when both
poles are Pos or both poles are Neg.
(Using DPDT relays is, IMO, a little 'dicey' though - there could be some sparking depending on how they make and break.)

Then it seems the OP did not actually test his design.