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()
//*