Hi
I am trying to perform a few tasks with 5V pro mini, listed below:
a) Update display on LCD
b) Read values of 2 x 5K potentiometers (Sets time limit for Filling and Sealing)
c) Read values of 3 buttons (1 for Filling Job and 2 for Sealing Job)
d) Switch on the solenoids (1 for Filling Job and 2 for Sealing Job)
e) When Sealing Job is done increase the counter "totalSealed"
The issues I am facing are:
a) Either of the POTs are not setting correct time limit.
b) The LCD updates only once. The "totalSealed" value is not updated.
Here is the sketch:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//const byte lcdSense = 2;
//const byte lcdWrite = 3;
const byte npn1Seal = 4;
const byte npnFill = 9;
//const byte heaterReady = 10;
const byte sealButton1 = 11;
const byte sealButton2 = 12;
const byte fillPot = A0;
const byte sealPot= A1;
const byte npn2Seal = 16;
const byte fillButton = 17;
// ##################### Variables for Debouncing #################
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int reading = HIGH; // Temporary variable for button read value
//int fillButtonVal;
//int sealButton1Val;
//int sealButton2Val;
int fillPotVal;
int sealPotVal;
int totalSealed = 0;
unsigned long startTimeFill;
unsigned long startTimeSeal1;
unsigned long startTimeSeal2;
void setup()
{
pinMode(fillButton, INPUT_PULLUP);
pinMode(sealButton1, INPUT_PULLUP);
pinMode(sealButton2, INPUT_PULLUP);
// pinMode(lcdSense, OUTPUT);
// digitalWrite(lcdSense, HIGH);
// pinMode(lcdSense, INPUT);
// pinMode(lcdWrite, OUTPUT);
pinMode(npnFill, OUTPUT);
pinMode(npn1Seal, OUTPUT);
pinMode(npn2Seal, OUTPUT);
pinMode(heaterReady, INPUT);
// while(lcdSense == HIGH);
// {}
digitalWrite(lcdWrite, HIGH);
lcd.init();
lcd.backlight();
lcd.setCursor(0,1);
lcd.print("Fill Time: sec");
lcd.setCursor(0,2);
lcd.print("Seal Time: sec");
lcd.setCursor(0,3);
lcd.print("Total Sealed:");
digitalWrite(lcdWrite, LOW);
}
void loop()
{
fillPotVal = map(analogRead(A0),0,1023,100,500); //100-500
sealPotVal = map(analogRead(A1),0,1023,500,1500); //500-1500
updateDisplay();
// while (digitalRead(heaterReady) == HIGH)
// {
//------------------Reading Buttons ---------------
reading = digitalRead(fillButton);
debounceCheck();
fillButtonVal = reading;
reading = digitalRead(sealButton1);
debounceCheck();
sealButton1Val = reading;
reading = digitalRead(sealButton2);
debounceCheck();
sealButton2Val = reading;
//--------------- Assigning Tasks ----------------
if (fillButtonVal == LOW)
{
digitalWrite(npnFill, HIGH); // Switch ON solenoid A thru NPN
startTimeFill = millis();
fillButtonVal = HIGH;
}
if (sealButton1Val == LOW)
{
digitalWrite(npn1Seal, HIGH); // Switch ON solenoid B thru NPN
startTimeSeal1 = millis();
sealButton1Val = HIGH;
}
if (sealButton2Val == LOW)
{
digitalWrite(npn2Seal, HIGH); // Switch ON solenoid C thru NPN
startTimeSeal2 = millis();
sealButton2Val = HIGH;
}
//---------------- Checking Timer for each Task ----------------
if (millis() - startTimeFill >= fillPotVal)
{
digitalWrite(npnFill, LOW); //Switch OFF filling solenoid thru NPN
}
if (millis() - startTimeSeal1 >= sealPotVal)
{
digitalWrite(npn1Seal, LOW); //Switch OFF sealing solenoid thru NPN
totalSealed = +1;
}
if (millis() - startTimeSeal2 >= sealPotVal)
{
digitalWrite(npn2Seal, LOW); //Switch OFF sealing solenoid 2 thru NPN
totalSealed = +1;
}
// }
}
void updateDisplay()
{
//while (lcdSense == HIGH)
//{}
//digitalWrite(lcdWrite, HIGH);
lcd.setCursor(11,1);
lcd.print(fillPotVal/1000);
lcd.setCursor(11,2);
lcd.print(sealPotVal/1000);
lcd.setCursor(14,3);
lcd.print(totalSealed);
// digitalWrite(lcdWrite, LOW);
}
// ##################### Debounce ##############
void debounceCheck()
{
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
if (reading != lastButtonState) // if Switch status changed pressed or noise
{
lastDebounceTime = millis(); // reset the debouncing timer
}
// if Longer than the debounce delay, switch really pressed
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
}
}
lastButtonState = reading;
}
