Motor Potentiometer Speed Control and Start/Stop Trouble

Hello,
I recently had some amazing help here on this forum. I have a code that is sooo close to being perfect and the last few things I can not figure out. I have a DC motor using an H bridge and the speed is controlled by a potentiometer. This is wired to 2 momentary buttons that act as start and stop. The issues are...
1. Motor should be in off state until start button is pressed but starts running after the setup.
2. Potentiometer speed displays and reads correctly but the actual motor speed does not change accordingly.
3. Once stop button is pressed the start button will not restart the motor.
Any help, suggestions or info on this topic would be greatly appreciated. Thank you kindly!

//#define serialLCD

#ifdef  serialLCD

//********************************************************************************
#include <Wire.h>

//Use I2C library:     https://github.com/duinoWitchery/hd44780
//LCD Referance:       https://www.arduino.cc/en/Reference/LiquidCrystal
#include <hd44780.h>   //main hd44780 header

//NOTE:
//hd44780_I2Cexp control LCD using I2C I/O exapander backpack (PCF8574 or MCP23008)
//hd44780_I2Clcd control LCD with native I2C interface (PCF2116, PCF2119x, etc...)

#include <hd44780ioClass/hd44780_I2Cexp.h> //I2C expander i/o class header

//If you do not know what your I2C address is, first run the 'I2C_Scanner' sketch
hd44780_I2Cexp lcd(0x27);

//********************************************************************************

#else
#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 12, 2, 3, 4, 5);

#endif

//********************************************************************************

const byte buttonPin1 = 6; //START BUTTON +5V---[INPUT_PULLUP]---(input Pin 6)---[Switch]---GND
const byte buttonPin2 = 7; //STOP BUTTON  +5V---[INPUT_PULLUP]---(input Pin 7)---[Switch]---GND

const byte in1        = 8;
const byte in2        = 9;
const byte enA        = 10;

const byte yellowLED  = 13;
const byte greenLED   = A1;
const byte redLED     = A2;

int speed1;
int dt = 100;

byte buttonNew1;           
byte buttonNew2;          
byte buttonOld1       = 1;
byte buttonOld2       = 1;
byte motorState       = 0; //start with motor off

unsigned long switchMillis;
unsigned long potMillis;

//********************************************************************************
void setup()
{
  //Serial.begin(9600);

  pinMode(in1, OUTPUT);                //motor
  pinMode(in2, OUTPUT);                //motor
  pinMode(enA, OUTPUT);                //motor

  pinMode(buttonPin1, INPUT_PULLUP);   //Button Green
  pinMode(buttonPin2, INPUT_PULLUP);   //Button Red
  pinMode(yellowLED, OUTPUT);          //LED Yellow
  pinMode(greenLED, OUTPUT);           //Green LED
  pinMode(redLED, OUTPUT);             //Red LED
  

  //pinMode(A0, INPUT);                //POTENTIOMETER  <----<<<< line not needed

  lcd.begin(16, 2);
  //                   111111
  //         0123456789012345
  lcd.print("XENTRIC GUITARS ");
  delay(5000);

  lcd.setCursor(0, 0);
  //                   111111
  //         0123456789012345
  lcd.print("MACHINE READY   ");
   digitalWrite(yellowLED, HIGH);       //Yellow LED On
  delay(5000);

} //END of setup()


//********************************************************************************
void loop()
{
   //*******************************************
  //time to check the switches?
  if (millis() - switchMillis > 50)
  {
    switchMillis = millis();

    checkSwitches();
  }

  //*******************************************
  //time to display the pot setting?
  if (millis() - potMillis > 500)
  {
    potMillis = millis();

    displayPot();
  }


  //*******************************************
  //other none blocking code goes here
  //*******************************************


} //END of loop


//********************************************************************************
void TurnMotorA()
{
  digitalWrite(in1, LOW);      //Switch between this HIGH and LOW to change direction
  digitalWrite(in2, HIGH);

  speed1 = analogRead(A0);
  speed1 = speed1 / 4 ;

  //  speed1 = speed1 * 0.2492668622;

  analogWrite(enA, speed1);

} //END of TurnMotorA()


//********************************************************************************
void checkSwitches()
{
  //*******************************************
  //buttonPin1 switch
  buttonNew1 = digitalRead(buttonPin1);
  if (buttonOld1 != buttonNew1)
  {
    //update to the new state
    buttonOld1 = buttonNew1;

    //********************
    if (buttonNew1 == LOW && motorState == 0)
    {
      motorState = 1;
      TurnMotorA();

      lcd.setCursor(0, 0);
      //                   111111
      //         0123456789012345
      lcd.print("MACHINE RUNNING ");

      digitalWrite(greenLED, HIGH);   //GREEN LED On
      digitalWrite(yellowLED, LOW);   //YELLOW LED OFF
      digitalWrite(redLED, LOW);      //RED LED OFF

    }
  }

  //*******************************************
  //buttonPin2 switch
  buttonNew2 = digitalRead(buttonPin2);

  if (buttonOld2 != buttonNew2)
  {
    //update to the new state
    buttonOld2 = buttonNew2;

    //********************
    if (buttonNew2 == LOW && motorState == 1)
    {
      motorState = 0;
      digitalWrite(in2, LOW);
      analogWrite(enA, 0);

      lcd.setCursor(0, 0);
      //                   111111
      //         0123456789012345
      lcd.print("MACHINE STOPPED ");
      lcd.setCursor(0, 1);
      //                   111111
      //         0123456789012345
      lcd.print("Speed =         ");
      lcd.setCursor(8, 1);
      lcd.print("0");

      digitalWrite(greenLED, LOW);    //GREEN LED OFF
      digitalWrite(yellowLED, LOW);   //YELLOW LED OFF
      digitalWrite(redLED, HIGH);     //RED LED ON

    }

    delay(dt);

  }

} //END of switchMillis()


//********************************************************************************
void displayPot()
{
  lcd.setCursor(0, 1);
  //                   111111
  //         0123456789012345
  lcd.print("Speed =           ");
  lcd.setCursor(8, 1);
  lcd.print(analogRead(A0) / 4);

} //END of displayPot()

//*

Once the motor has started, do you want to allow the potentiometer to vary the speed in real time?

Hello Larryd,
Yes that is what I would like. It's meant for fine tuning the speed.

Try

//Version 2.0

//#define serialLCD

#ifdef  serialLCD

//********************************************************************************
#include <Wire.h>

//Use I2C library:     https://github.com/duinoWitchery/hd44780
//LCD Referance:       https://www.arduino.cc/en/Reference/LiquidCrystal
#include <hd44780.h>   //main hd44780 header

//NOTE:
//hd44780_I2Cexp control LCD using I2C I/O exapander backpack (PCF8574 or MCP23008)
//hd44780_I2Clcd control LCD with native I2C interface (PCF2116, PCF2119x, etc...)

#include <hd44780ioClass/hd44780_I2Cexp.h> //I2C expander i/o class header

//If you do not know what your I2C address is, first run the 'I2C_Scanner' sketch
hd44780_I2Cexp lcd(0x3F);
//hd44780_I2Cexp lcd(0x27);

//********************************************************************************

#else
#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 12, 2, 3, 4, 5);

#endif

//********************************************************************************

const byte startSwitch = 6; //START BUTTON +5V---[INPUT_PULLUP]---(input Pin 6)---[Switch]---GND
const byte stopSwitch  = 7; //STOP BUTTON  +5V---[INPUT_PULLUP]---(input Pin 7)---[Switch]---GND

const byte in1         = 8;
const byte in2         = 9;
const byte enA         = 10;

const byte yellowLED   = 13;
const byte greenLED    = A1;
const byte redLED      = A2;

int speed1;
int dt = 100;

byte buttonNew1;
byte buttonNew2;
byte buttonOld1        = 1;
byte buttonOld2        = 1;
byte motorState        = 0;            //start with motor off

unsigned long switchMillis;
unsigned long potMillis;

//********************************************************************************
void setup()
{
  //Serial.begin(9600);

  pinMode(startSwitch, INPUT_PULLUP);  //Button Green  START
  pinMode(stopSwitch, INPUT_PULLUP);   //Button Red    STOP

  pinMode(yellowLED, OUTPUT);          //LED Yellow    READY
  pinMode(greenLED, OUTPUT);           //Green LED     RUNNING
  pinMode(redLED, OUTPUT);             //Red LED       STOPPED

  pinMode(in1, OUTPUT);                //motor         DIRECTION
  pinMode(in2, OUTPUT);                //motor         RUN/STOP
  pinMode(enA, OUTPUT);                //motor         SEED CONTROL
  //*******************************************
  //Motor OFF
  motorState = 0;                      //OFF at powerup
  digitalWrite(in1, LOW);              //Switch between this HIGH and LOW to change direction
  digitalWrite(in2, LOW);              //Motor OFF
  digitalWrite(enA, LOW);              //PWM (speed) set to LOW

  digitalWrite(greenLED, LOW);         //GREEN LED OFF
  digitalWrite(redLED, HIGH);          //RED LED ON       <----<<<< Motor OFF
  digitalWrite(yellowLED, LOW);        //YELLOW LED OFF
  //*******************************************

  lcd.begin(16, 2);
  //                   111111
  //         0123456789012345
  lcd.print("XENTRIC GUITARS ");
  delay(5000);

  lcd.setCursor(0, 0);
  //                   111111
  //         0123456789012345
  lcd.print("MACHINE READY   ");
  digitalWrite(yellowLED, HIGH);       //Yellow LED On
  delay(5000);

} //END of setup()


//********************************************************************************
void loop()
{
  //*******************************************
  //time to check the switches?
  if (millis() - switchMillis > 50)
  {
    switchMillis = millis();

    checkSwitches();
  }

  //*******************************************
  //time to display the pot setting?
  if (millis() - potMillis > 500)
  {
    potMillis = millis();

    displayPot();
  }

  //*******************************************
  //when motor is running, send speed changes to the driver
  if (motorState == 1)
  {
    speed1 = analogRead(A0);
    speed1 = speed1 / 4 ;    //  speed1 = speed1 * 0.2492668622; //?????

    analogWrite(enA, speed1);
  }

  //*******************************************



  //*******************************************
  //other none blocking code goes here
  //*******************************************


} //END of loop


//********************************************************************************
void TurnMotorA()
{
  digitalWrite(in1, LOW);      //Switch between this HIGH and LOW to change direction
  digitalWrite(in2, HIGH);

} //END of TurnMotorA()


//********************************************************************************
void checkSwitches()
{
  //*******************************************
  //startSwitch switch   MOTOR ON
  buttonNew1 = digitalRead(startSwitch);

  if (buttonOld1 != buttonNew1)
  {
    //update to the new state
    buttonOld1 = buttonNew1;

    //********************
    if (buttonNew1 == LOW && motorState == 0)
    {
      //the motro is now in the running state
      motorState = 1;

      TurnMotorA();

      lcd.setCursor(0, 0);
      //                   111111
      //         0123456789012345
      lcd.print("MACHINE RUNNING ");

      digitalWrite(greenLED, HIGH);   //GREEN LED On
      digitalWrite(yellowLED, LOW);   //YELLOW LED OFF
      digitalWrite(redLED, LOW);      //RED LED OFF

    }
  }

  //*******************************************
  //stopSwitch switch   Motor OFF
  buttonNew2 = digitalRead(stopSwitch);

  if (buttonOld2 != buttonNew2)
  {
    //update to the new state
    buttonOld2 = buttonNew2;

    //********************
    if (buttonNew2 == LOW && motorState == 1)
    {
      //the motro is now in the stopped state
      motorState = 0;

      digitalWrite(in2, LOW);
      digitalWrite(enA, LOW);
      //analogWrite(enA, 0);

      lcd.setCursor(0, 0);
      //                   111111
      //         0123456789012345
      lcd.print("MACHINE STOPPED ");
      lcd.setCursor(0, 1);
      //                   111111
      //         0123456789012345
      lcd.print("Speed =         ");
      lcd.setCursor(8, 1);
      lcd.print("0");

      digitalWrite(greenLED, LOW);    //GREEN LED OFF
      digitalWrite(yellowLED, LOW);   //YELLOW LED OFF
      digitalWrite(redLED, HIGH);     //RED LED ON

    }

    delay(dt);

  }

} //END of switchMillis()


//********************************************************************************
void displayPot()
{
  lcd.setCursor(0, 1);
  //                   111111
  //         0123456789012345
  lcd.print("Speed =         ");
  lcd.setCursor(8, 1);
  lcd.print(analogRead(A0) / 4);

} //END of displayPot()

//********************************************************************************

Thank you! The speed is working. The only thing now is once I push the stop button the start will not active the motor again. Is there a way to use the serial print option to see the button states?

Pushing the stop then start button works here.

Do your RED and GREEN LEDs alternate when you push: Stop then Start then Stop etc. ?

Currently once the stop button has been pressed the start button has no effect and the same with the green LED. The red stays on and the LCD displays motor stopped regardless of the start button.

The stop and start switches work okay here.

Let's see a good image of your switch wiring.

EDIT

Does your display switch between:

MACHINE RUNNING to MACHINE STOPPED to MACHINE RUNNING etc?

You should look very closely at: your switch wiring, soldering and maybe you have a bad or intermittent start switch.

Time for :sleeping:

Changed the sketch so the motor speed is updated every 100ms.

You still need to check your switch wiring.

//
//Version    2.01
//
//https://forum.arduino.cc/index.php?topic=661570.msg4457338#msg4457338
//

//********************************************************************************
//#define area
#define ledON            HIGH
#define ledOFF           LOW

#define motorON          HIGH
#define motorOFF         LOW

#define motorIsRunning   HIGH
#define motorIsStopped   LOW

#define isPushed         LOW
#define isNotPushed      HIGH

//******************************

//#define serialLCDisBeingUsed    <-----<<<<<

//********************************************************************************
#ifdef  serialLCDisBeingUsed

#include <Wire.h>

//Use I2C library:     https://github.com/duinoWitchery/hd44780
//LCD Reference:       https://www.arduino.cc/en/Reference/LiquidCrystal
#include <hd44780.h>   //main hd44780 header

//NOTE:
//hd44780_I2Cexp control LCD using I2C I/O expander backpack (PCF8574 or MCP23008)
//hd44780_I2Clcd control LCD with native I2C interface (PCF2116, PCF2119x, etc...)

#include <hd44780ioClass/hd44780_I2Cexp.h> //I2C expander i/o class header

//If you do not know what your I2C address is, first run the 'I2C_Scanner' sketch
hd44780_I2Cexp lcd(0x3F);
//hd44780_I2Cexp lcd(0x27);

//********************************************************************************

#else

#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 12, 2, 3, 4, 5);

#endif

//********************************************************************************
const byte startSwitch = 6;    //START BUTTON +5V---[INPUT_PULLUP]---(input Pin 6)---[Switch]---GND
const byte stopSwitch  = 7;    //STOP BUTTON  +5V---[INPUT_PULLUP]---(input Pin 7)---[Switch]---GND

const byte in1         = 8;
const byte in2         = 9;
const byte enA         = 10;

const byte yellowLED   = 13;
const byte greenLED    = A1;
const byte redLED      = A2;

const byte potPin      = A0;

unsigned int  speedSetting;    //range is 0 to 1023

byte startButtonNew;
byte stopButtonNew;
byte startButtonOld    = 1;
byte stoptButtonOld    = 1;
byte motorState;

//timing stuff
unsigned long speedMillis;
unsigned long switchMillis;
unsigned long potMillis;

//********************************************************************************
void setup()
{
  //Serial.begin(9600);

  pinMode(startSwitch, INPUT_PULLUP);  //Button Green  START
  pinMode(stopSwitch, INPUT_PULLUP);   //Button Red    STOP

  pinMode(yellowLED, OUTPUT);          //LED Yellow    READY
  pinMode(greenLED, OUTPUT);           //Green LED     RUNNING
  pinMode(redLED, OUTPUT);             //Red LED       STOPPED

  pinMode(in1, OUTPUT);                //motor         DIRECTION
  pinMode(in2, OUTPUT);                //motor         RUN/STOP
  pinMode(enA, OUTPUT);                //motor         SEED CONTROL
  
  //*******************************************
  //Motor OFF
  motorState = motorIsStopped;         //OFF at power up
  digitalWrite(in1, LOW);              //Switch between HIGH and LOW to change direction
  digitalWrite(in2, motorOFF);         //Motor OFF     <----<<<< Motor is OFF
  digitalWrite(enA, LOW);              //PWM (speed) set to LOW i.e. 0 volts zero speed  <----<<<< Motor is OFF

  digitalWrite(greenLED,  ledOFF);     //GREEN 
  digitalWrite(redLED,    ledON);      //RED           <----<<<< Motor is OFF
  digitalWrite(yellowLED, ledOFF);     //YELLOW 
  //*******************************************

  lcd.begin(16, 2);
  //                   111111
  //         0123456789012345
  lcd.print("XENTRIC GUITARS ");
  delay(2500);

  lcd.setCursor(0, 0);
  //                   111111
  //         0123456789012345
  lcd.print("MACHINE READY   ");
  digitalWrite(yellowLED, ledON);      //Yellow LED On
  delay(2500);

} //END of setup()


//********************************************************************************
void loop()
{
  //*******************************************
  //time to check the switches?
  if (millis() - switchMillis > 50)
  {
    switchMillis = millis();

    checkSwitches();
  }

  //*******************************************
  //when the motor is running, send speed changes to the driver every 100ms
  if (motorState == motorIsRunning && millis() - speedMillis >= 100)
  {
    //restart timing
    speedMillis = millis();

    speedSetting = analogRead(potPin);
    speedSetting = speedSetting / 4 ;       //speedSetting = speedSetting * 0.2492668622; //?????

    analogWrite(enA, speedSetting);

  }

  //*******************************************
  //is it time to print the speed setting?
  if(millis() - potMillis >= 500)
  {
    //restart timing
    potMillis = millis();
    
    lcd.setCursor(0, 1);
    //                   111111
    //         0123456789012345
    lcd.print("Speed =         ");
    lcd.setCursor(8, 1);
    lcd.print(analogRead(potPin) / 4);    
  }

  //*******************************************
  //other none blocking code goes here          <------<<<<<
  //*******************************************


} //END of loop


//********************************************************************************
void TurnMotorA()
{
  digitalWrite(in1, LOW);      //Switch between HIGH and LOW to change direction
  digitalWrite(in2, motorON);  //Motor set to running

} //END of TurnMotorA()


//********************************************************************************
void checkSwitches()
{
  //*******************************************
  //startSwitch switch   MOTOR ON
  startButtonNew = digitalRead(startSwitch);

  if (startButtonOld != startButtonNew)
  {
    //update to the new state
    startButtonOld = startButtonNew;

    //********************
    if (startButtonNew == isPushed && motorState == motorIsStopped)
    {
      //the motor is now in the running state
      motorState = motorIsRunning;

      TurnMotorA();

      lcd.setCursor(0, 0);
      //                   111111
      //         0123456789012345
      lcd.print("MACHINE RUNNING ");

      digitalWrite(greenLED,  ledON);     
      digitalWrite(yellowLED, ledOFF);   
      digitalWrite(redLED,    ledOFF);      

    }
    
  }

  //*******************************************
  //stopSwitch switch   Motor OFF
  stopButtonNew = digitalRead(stopSwitch);

  if (stoptButtonOld != stopButtonNew)
  {
    //update to the new state
    stoptButtonOld = stopButtonNew;

    //********************
    if (stopButtonNew == isPushed && motorState == motorIsRunning)
    {
      //the motor is now in the stopped state
      motorState = motorIsStopped;

      digitalWrite(in2, motorOFF);    //Motor set to stopped
      digitalWrite(enA, LOW);         //zero speed

      lcd.setCursor(0, 0);
      //                   111111
      //         0123456789012345
      lcd.print("MACHINE STOPPED ");
      lcd.setCursor(0, 1);

      //                   111111
      //         0123456789012345
      //lcd.print("Speed =         ");
      //lcd.setCursor(8, 1);
      //lcd.print("0");

      digitalWrite(greenLED,  ledOFF);  
      digitalWrite(yellowLED, ledOFF); 
      digitalWrite(redLED,    ledON);     

    }

  }

} //END of switchMillis()

//********************************************************************************