Hi everyone,
I'm a complete novice with Arduino programing and have a project that I have to get done ASAP for our family business.
I have a Arduino Mega with a 8 way relay board, 3.2 inch tft screen and a 4x4 membrane keypad.
What I need,
- 8 relays to fire in sequence with one on and off time for all.
- Screen to display various outputs regarding the process of the unit
- Use the keypad to change the values of offTime and onTime.
What I've done so far,
- Relays are working with the offTime and onTime manually set in the global variables
- Screen displaying all needed info and also with three different "menus"
- I have the keypad setup with input working onto the Serial monitor
- The "A", "B" and "C" buttons select the relevant menus.
What I'm struggling with,
- I need to use the keypad to change the maximum 4 digit offTime/onTime by going into menu "B" or "C", and then saving that value, but pressing "#", into the global variable so that it changes the delays for the relays.
- I need to be able to cancel the input of the value in case it was done incorrectly, or even delete the previously pushed values.
- The changed value needs to be saved to the EEPROM in case there's a power outage for any reason.
My code so far is as follows,
// Code
#include <UTFT.h>
#include <Keypad.h>
// TFT
UTFT myGLCD(CTE32HR, 38, 39, 40, 41);
extern uint8_t BigFont[];
extern uint8_t Grotesk32x64[];
// Serial Number, change for each new unit
int serialNum = 0001;
// Blowers
const byte Machine1 = A0; // I/O pin connected by the first relay
const byte Machine2 = A1;
const byte Machine3 = A2;
const byte Machine4 = A3;
const byte Machine5 = A4;
const byte Machine6 = A5;
const byte Machine7 = A6;
const byte Machine8 = A7;
unsigned long BlowerStartTime;
unsigned long BlowerStopTime;
bool BlowerIsRunning = false;
byte CurrentBlower = Machine1;
int onTime = 1 ; // seconds on
int offTime = 30 ; // seconds off
unsigned long blows = 0;
unsigned long activeDays = 0;
unsigned long activeHours = 0;
unsigned long powderUsed = 0;
// Keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
for (int i = Machine1; i <= Machine8; i += 1)
{
pinMode(i, OUTPUT); //set digital I/O pin as output
}
Serial.begin(9600);
keypad.addEventListener(keypadEvent);
keypad.setDebounceTime(100);
// Intro screen
myGLCD.InitLCD();
myGLCD.setFont(Grotesk32x64);
myGLCD.clrScr();
myGLCD.setColor(VGA_BLUE);
myGLCD.print("CHINCO FIRESIDE", CENTER, 100);
myGLCD.print("TREATMENT", CENTER, 164);
delay (3000);
myGLCD.clrScr();
Splash();
}
void Splash() {
myGLCD.setColor(VGA_BLUE);
myGLCD.drawRect(0, 0, 479, 305);
myGLCD.setFont(BigFont);
myGLCD.print("CHINCO FIRESIDE TREATMENT", CENTER, 2);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("'Press 'A' - Statistics'", 1, 100);
myGLCD.print("'Press 'B' - Delay Setup'", 1, 116);
myGLCD.print("'Press 'C' - Blow Setup'", 1, 132);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("Serial #", 270, 286);
myGLCD.printNumI(serialNum, 398, 286);
}
void title() {
myGLCD.clrScr();
myGLCD.setColor(VGA_BLUE);
myGLCD.drawRect(0, 0, 479, 305);
myGLCD.setFont(BigFont);
myGLCD.print("CHINCO FIRESIDE TREATMENT", CENTER, 2);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("Delay Between Blows :", 2, 48); // Delay time
myGLCD.printNumI(offTime, 337, 48);
myGLCD.print("s", 403, 48);
myGLCD.print("Duration of Each Blow :", 2, 64); // Blow time
myGLCD.printNumI(onTime, 368, 64);
myGLCD.print("s", 402, 64);
myGLCD.print("System Active For :", 2, 80); //Active Days
myGLCD.printNumI(activeDays, 305, 80);
myGLCD.print("Days", 355, 80);
myGLCD.print("System Active For :", 2, 96); //Active Hours
myGLCD.printNumI(activeHours, 305, 96);
myGLCD.print("Hours", 355, 96);
myGLCD.print("Number of Blows :", 2, 112); //Blows
myGLCD.printNumI(blows, 274 , 112);
myGLCD.print("KGs powder used :", 2, 128); //KGs used
myGLCD.printNumF(powderUsed, 3, 274, 128);
myGLCD.setColor(VGA_RED);
myGLCD.print("'Press 'A' - Update Stats'", 1, 150);
myGLCD.print("'Press 'B' - Delay Setup'", 1, 166);
myGLCD.print("'Press 'C' - Blow Setup'", 1, 182);
myGLCD.setColor(VGA_GRAY);
myGLCD.print("Serial #", 270, 286);
myGLCD.printNumI(serialNum, 398, 286);
}
void offTimeChange() {
myGLCD.clrScr();
myGLCD.setColor(VGA_BLUE);
myGLCD.drawRect(0, 0, 479, 305);
myGLCD.setFont(BigFont);
myGLCD.print("CHINCO FIRESIDE TREATMENT", CENTER, 2);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("CHANGE BLOW DELAY TIME", CENTER, 50);
myGLCD.print("ENTER VALUE", CENTER, 66);
myGLCD.print("PRESS # TO SAVE", CENTER, 100);
myGLCD.setColor(VGA_RED);
myGLCD.print("'Press 'A' - Statistics'", 1, 146);
myGLCD.print("'Press 'C' - Blow Setup'", 1, 162);
myGLCD.setColor(VGA_GRAY);
myGLCD.print("Serial #", 270, 286);
myGLCD.printNumI(serialNum, 398, 286);
}
void onTimeChange() {
myGLCD.clrScr();
myGLCD.setColor(VGA_BLUE);
myGLCD.drawRect(0, 0, 479, 305);
myGLCD.setFont(BigFont);
myGLCD.print("CHINCO FIRESIDE TREATMENT", CENTER, 2);
myGLCD.setColor(VGA_WHITE);
myGLCD.print("CHANGE LENGTH OF BLOW TIME", CENTER, 50);
myGLCD.print("ENTER VALUE", CENTER, 66);
myGLCD.print("PRESS # TO SAVE", CENTER, 100);
myGLCD.setColor(VGA_RED);
myGLCD.print("'Press 'A' - Statistics'", 1, 146);
myGLCD.print("'Press 'B' - Delay Setup'", 1, 162);
myGLCD.setColor(VGA_GRAY);
myGLCD.print("Serial #", 270, 286);
myGLCD.printNumI(serialNum, 398, 286);
}
void loop()
{
ManageBlowers();
int activeDs = millis() / 864000000UL;
int activeHrs = millis() / 3600000UL;
activeDays = activeDs;
activeHours = activeHrs;
unsigned long int powderUse = (blows * 50) / 1000;
powderUsed = powderUse;
Serial.print(powderUsed);
char key = keypad.getKey();
if (key) {
Serial.print(key);
}
}
void ManageBlowers() {
if (BlowerIsRunning)
{
if (millis() - BlowerStartTime > onTime * 1000UL)
{
StopBlower(CurrentBlower);
CurrentBlower += 1;
if (CurrentBlower > Machine8)
CurrentBlower = Machine1;
}
}
else
{
if (millis() - BlowerStopTime > offTime * 1000UL)
{
StartBlower(CurrentBlower);
}
}
}
void StopBlower(int BlowerPin)
{
digitalWrite(BlowerPin, LOW);
BlowerIsRunning = false;
BlowerStopTime = millis();
}
void StartBlower(int BlowerPin)
{
digitalWrite(BlowerPin, HIGH);
blows ++;
BlowerIsRunning = true;
BlowerStartTime = millis();
myGLCD.setColor(VGA_WHITE);
myGLCD.print("LAST APPLICATOR TO BLOW", CENTER, 216);
myGLCD.setColor(VGA_RED);
myGLCD.printNumI((BlowerPin - Machine1) / 1 + 1, CENTER, 234);
}
void keypadEvent (KeypadEvent key) {
{
switch (keypad.getState())
{
case PRESSED:
if (key == 'A') {
title();
}
else if (key == 'B') {
offTimeChange();
offT();
}
else if (key == 'C') {
onTimeChange();
onT();
}
break;
}
}
}
void offT() {
}
void onT() {
}
Any advise would be greatly appreciated!