Repeat a certain part of code.

So… I’m a newbie trying to make my first scratch project and I’m Stuck. Can someone diagnose the code please? It be great if i could have some help.
Code:

// This sketch is made by Naquino14. This code is an H-bridge motor driver
const int codeMaximum = 5;
int codeState = 0;
const int button = 6;
int buttonState = 0;
int buttonSwitch = 0;
const int port1 = 2;
const int port2 = 3;
const int port3 = 4;
const int port4 = 5;

void setup() {
  pinMode(button, INPUT);
  pinMode(port1, OUTPUT);
  pinMode(port2, OUTPUT);
  pinMode(port3, OUTPUT);
  pinMode(port4, OUTPUT);
  Serial.begin(9600);
  Serial.println("Source is 12V DC");
}

void loop() {
  buttonState = digitalRead(button); //Read buttonState
  if (buttonState = 1) { //Driver mode 1
    digitalWrite(port1, HIGH);
    delay(5000);
    digitalWrite(port1, LOW);
    delay(500);
    buttonState++;
    Serial.println(buttonState);
    delay(50);
  }
  if (buttonState = 2); { //Driver mode 2
    digitalWrite(port2, HIGH);
    delay(5000);
    digitalWrite(port2, LOW);
    delay(500);
    buttonState++;
    Serial.println(buttonState);
    delay(50);
  }
  if (buttonState = 3); { //Driver mode 3
    digitalWrite(port3, HIGH);
    delay(5000);
    digitalWrite(port3, LOW);
    delay(500);
    buttonState++;
    Serial.println(buttonState);
    delay(50);
   }
   if (buttonState = 4); { //Driver mode 4
     digitalWrite(port4, HIGH);
     delay(5000);
     digitalWrite(port4, LOW);
     delay(500);
     buttonState++;
     Serial.println(buttonState);
     delay(50);
   }
   if (buttonState = 5); { //Reset Code
     buttonState--;
     delay(10);
     buttonState--;
     delay(10);
     buttonState--;
     delay(10);
     buttonState--;
     delay(10);
     buttonState--;
     delay(10);
     Serial.print(buttonState);
   }
   codeState++;
   if (codeState = codeMaximum) { //Stops code after 5 presses
     exit(0);
   }
   buttonState = digitalRead(button);
}

Schematic:
See attached

Motor_driver__Toggle_.ino (1.74 KB)

  if (buttonState = 1)

Look up the difference between an assignment "=" and a comparison "==".

Pete

Then, once you've done that and understand the difference between the two, perhaps you could explain what your code is supposed to do and what it is actually doing.

Pete

This if statement you got right

if (buttonState = 1) { //Driver mode 1
...
...

All the others you did get wrong :wink: E.g.

if (buttonState = 2); { //Driver mode 2
...
...

The semi-colon terminates the if statement and hence the code block after it will unconditionally be executed.