jre414:
Thank you! Do you know what would cause the LCD contrast to fade over time once the program has been running for a bit?
Show us a good image of your wiring.
The pot is enabled in this version but you will have to work on these lines yourself:
speed1 = analogRead(A0);
speed1 = speed1 / 4 ; <-----<<<< //this is not be needed if you keep the next line
// speed1 = speed1 * 0.2492668622; <-----<<<< Remember, you are using integer * float here
analogWrite(enA, speed1);
//Version 1.05
//#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; //Start
byte buttonNew2; //Stop
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
digitalWrite(redLED, HIGH);
//pinMode(A0, INPUT); //POTENTIOMETER <----<<<< line not needed
lcd.begin(16, 2);
// 111111
// 0123456789012345
lcd.print("XENTRIC GUITARS ");
delay(2000);
lcd.setCursor(0, 0);
// 111111
// 0123456789012345
lcd.print("MACHINE READY ");
delay(2000);
} //END of setup()
//********************************************************************************
void loop()
{
digitalWrite(yellowLED, HIGH); //Yellow LED On <------<<<< WHY?
//*******************************************
//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("Pot = ");
lcd.setCursor(8, 1);
lcd.print(analogRead(A0) / 4);
} //END of displayPot()
//********************************************************************************