Wheels stop moving

Hi Arduino form page, so I am in a robotics course using the Arduino elegoo robot car kit. We are supposed to make the car move in a square with a given code. For some reason, my code doesn’t work. It will move forward as directed to, but after it turns right and begins to move forward the right side motors stop working. I ran a friend’s code and my robot worked fine so it is not a hardware issue. I will post my code below, please let me know if there is anything I need to change!

/*
// put your setup code here, to run once:
//Defines the constant value 5 with the name ENA
//Note the lack of a semi-colon!
#define ENA 5
//Defines the constant value 6 with the name ENB
#define ENB 6

//Defines the read only variable IN1 and assigns integer value of 7
const int IN1 = 7;
//Defines the read only variable IN2 and assigns integer value of 8
const int IN2 = 8;

//Defines the variable IN3 and assigns integer value of 9
const int IN3 = 9;
//Defines the variable IN4 and assigns integer value of 11
const int IN4 = 11;
*/
void setup() {
// put your setup code here, to run once:
pinMode(5,OUTPUT); //ENA
pinMode(6,OUTPUT); //ENB
pinMode(7,OUTPUT); //IN1
pinMode(8,OUTPUT); //IN2
pinMode(9,OUTPUT); //IN3
pinMode(11,OUTPUT); //IN4
}

void loop() {
// put your main code here, to run repeatedly:
forward();
Right();
forward();
}
void forward(){
digitalWrite(5,HIGH); //ENA
digitalWrite(6,HIGH); //ENB
digitalWrite(7,HIGH); //IN1
digitalWrite(8,LOW); //IN2
digitalWrite(8,LOW); //IN3
digitalWrite(11,HIGH); //IN4
delay(1000); //Wait 1 second before continuing
}
void reverse(){
digitalWrite(5,HIGH); //ENA
digitalWrite(6,HIGH); //ENB
digitalWrite(7,LOW); //IN1
digitalWrite(8,HIGH); //IN2
digitalWrite(9,HIGH); //IN3
digitalWrite(11,LOW); //IN4
delay(500); //Wait 1 second before continuing
}
void Right(){
digitalWrite(5,HIGH); //ENA
digitalWrite(6,HIGH); //ENB
digitalWrite(7,HIGH); //IN1
digitalWrite(8,LOW); //IN2
digitalWrite(9,HIGH); //IN3
digitalWrite(11,LOW); //IN4
delay(1000); //Wait 1 second before continuing
}
void Left(double tim){
digitalWrite(5,HIGH); //ENA
digitalWrite(6,HIGH); //ENB
digitalWrite(7,LOW); //IN1
digitalWrite(8,HIGH); //IN2
digitalWrite(9,LOW); //IN3
digitalWrite(11,HIGH); //IN4
delay(tim); //Wait 1 second before continuing
}

Ask your teacher?

Ask your friend?

I've asked my friend and he didn't see a problem with my code. I'm going to take his code tomorrow and test it again.

A couple of general points:

There is a post at the top of the forum on how to use this forum - please read it and pay special attention to using the code tags, then please edit your post to make the code easier to read

On your code, you need to use better comments and variables for starters, use descriptors that make sense - "ENA" is sort of gibberish to me - better maybe Enable Motor 1 forward or something, else you will spend a lot of time trying to remember what a line of code should do

Can you describe the (desired?) effect of the different values for IN1 to IN4? Which setting makes the left motor really run forward, which back, which stop? Same for the right motor.

If you make the robot turn right(), and wait delay(1000) for a second, the robot will turn for one second - how many turns will that be?

Also use meaningful names for the pins, I'd accept ENA (or ENLeft, ENRight), and IN1-IN4 (or INLeftA, INLeftB...).

DrDiettrich: Also use meaningful names for the pins, I'd accept ENA (or ENLeft, ENRight), and IN1-IN4 (or INLeftA, INLeftB...).

The OP has declared somewhat meaningful names. Just didn't bother to use them.

In your forward() function you have:

    digitalWrite(5, HIGH); //ENA
    digitalWrite(6, HIGH); //ENB
    digitalWrite(7, HIGH); //IN1
    digitalWrite(8, LOW); //IN2
    digitalWrite(8, LOW); //IN3    <<<< problem
    digitalWrite(11, HIGH); //IN4
.
.
.

You write IN2 (pin 8 ) twice instead of updating IN3 (pin 9).

Not sure if that’s your problem but it’s not helping.

I just fixed that and made it a 9. That fixed my issue! Thank you!

ShadowMan24:
I just fixed that and made it a 9. That fixed my issue! Thank you!

Now you also know why we NEVER use pin numbers in the code, ALWAYS use variables - declared at the top of the code. It goes a long way preventing this kind of errors. In fact your code does declare these variables, but you subsequently don’t use them…