Controlling a Gearmotor in CW and CCW Directions With 1 Push Button

Hello, I am looking for help on a project I'm working on. I want to be able to rotate a motor in the clockwise and counterclockwise directions with one AbleNet push button. The idea is that if the button is pressed it moves in the clockwise direction, and if pressed again it moves in the counterclockwise direction. If pressed a third time it again moves in the clockwise direction, and so on. The button is designed to be a momentary switch, so the motor only moves for as long as the button is pressed.

My current approach is to track the motor state as a boolean (either true or false), if it is true it moves clockwise and if false it moves counterclockwise. Then at the end of the code that boolean state is switch (if it was true it is now false).

Full disclosure, I am super new to coding in Arduino, and coding in general, so I do expect my code to be pretty buggy! The circuit has the button connected to the Arduino, which is connected to an H-bridge driver to control the direction of the motor.

Thank you for your help!

const int MotorButton= 8; //Button is connected to pin 8
bool MotorDirection = true; //MotorDirection Starts as true
int MotorPin1= 6; //Driver connected to pin 6
int MotorPin2= 7; //Driver connected to pin 7

void setup(){
  Serial.begin(9600); 
  pinMode(MotorButton, INPUT_PULLUP); //set MotorButton as input
  pinMode(MotorPin1, OUTPUT); //set MotorPin1 as output
  pinMode(MotorPin2, OUTPUT); //set MotorPin2 as output
}

void loop(){
  int ButtonState = digitalRead(MotorButton); //read state of button
  bool NewMotorDirection = MotorDirection; //set new direction to old direction
  if (ButtonState == LOW){ //if button presed
    if (NewMotorDirection == true){ // if New motor direction is true
      digitalWrite(MotorPin1, LOW); //set one pin to low
      digitalWrite(MotorPin2, HIGH); //set other to high
    }
    else if (NewMotorDirection == false){ //if New motor direction is false
      digitalWrite(MotorPin1, HIGH); //set one pin to hight
      digitalWrite(MotorPin2, LOW); //set other to low
    }
  }
  MotorDirection = !NewMotorDirection;//now change old motor direction to opposite of new motor direction
}

If You read General Guidance and How to use the forum and BEGINNERS and LEARNERS read this... at the top You would know how to post code. There are many helpers using other equipment than Pc and they can't read Your code.

What is Your question? What is happening that is not like Your description? The code looks possible to me.

Please do not post pictures of code. The how to use this forum sticky tells how to properly post code.

The state change detection example might be of help.

An example using state change detection:

// This is to illustrate using the state change detectin method to
// toggle the state of a pin (Pin 13, the on board LED) and the direction of a motor
// a momentary switch (pushbutton) is wired from pin 8 to ground
// by C Goulding aka groundFungus

// this constant won't change:
const int  buttonPin = 8;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
// Variables will change:
boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

boolean direction = false;

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(115200);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)  // went from not pressed to pressed
         {
            direction = !direction;
            if (direction == false);
            {
               digitalWrite(MotorPin1, LOW);
               digitalWrite(MotorPin2, HIGH);
            }
            else
            {
               digitalWrite(MotorPin1, HIGH);
               digitalWrite(MotorPin2, LOW);
            }
            Serial.print("direction  ");
            Serial.println(direction);
            digitalWrite(ledPin, !digitalRead(ledPin));
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}