Facing probleb with DC Motor Forward and reverse

my code are bellow

const int button1 = 2;

const int button2 = 3;

const int button3 = 4;


const int led1 = 8;

const int M1 = 9;

const int M2 = 10;


bool isPin1,isPin2;


int buttonState1 =0;

int buttonState2 =0;

int buttonState3 =0;




void setup() 
{
 
 // put your setup code here, to run once:
  
pinMode(led1,OUTPUT);
 
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);

 
pinMode(button1,INPUT);
  
pinMode(button2,INPUT);
  
pinMode(button3,INPUT);

  
isPin1 = false;
  
isPin2 = false;


}



void loop() 

{
  
// put your main code here, to run repeatedly:
  
buttonState1 = digitalRead(button1);
  
buttonState2 = digitalRead(button2);
  
buttonState3 = digitalRead(button3);
  
if(buttonState1==HIGH && isPin1 && !isPin2 )

{
    
    
digitalWrite(led1,HIGH);  
isPin2 =true;
    
  
}

else 

if(buttonState2==HIGH && isPin2)

{
   
 
digitalWrite(M1,HIGH);
    
isPin2=false;
    
delay(5000);
    
digitalWrite(M1,LOW);
    
isPin1=true;
  
  

} 
else 

if(buttonState3==HIGH)

{
    

digitalWrite(M2,HIGH);
       
delay(5000);
    
digitalWrite(M2,LOW);
    
isPin2=true;
  
}
  
  

}

When I'm using this code everything is working bt when I connect 24 volt power supply to motor after that sometime its reset auto and some time its not cut off on delay time. please see attached file also

Double-posting won't help, and is against the forum rules.

http://forum.arduino.cc/index.php?topic=425816.0

Here's the same code with all the whitespaces removed.
44 lines instead of 120.
Leo..

const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int led1 = 8;
const int M1 = 9;
const int M2 = 10;
bool isPin1, isPin2;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  isPin1 = false;
  isPin2 = false;
}

void loop() {
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  buttonState3 = digitalRead(button3);
  if (buttonState1 == HIGH && isPin1 && !isPin2 ) {
    digitalWrite(led1, HIGH);
    isPin2 = true;
  }
  else if (buttonState2 == HIGH && isPin2) {
    digitalWrite(M1, HIGH);
    isPin2 = false;
    delay(5000);
    digitalWrite(M1, LOW);
    isPin1 = true;
  }
  else if (buttonState3 == HIGH) {
    digitalWrite(M2, HIGH);
    delay(5000);
    digitalWrite(M2, LOW);
    isPin2 = true;
  }
}

You need a separate power supply for the relay coils, remove the JD-VCC jumper on the relay board AND the ground wire from relay board to Arduino, you're prob'ly getting interference through the ground wire. Connect the separate PS to JD-VCC and GND on relay board.


Also, you need to reverse HIGH & LOW in digitalWrite() M1 & M2, LOW turns the relay ON.