Hello,
I am looking to improve my code and coding abilities, I have been looking to see what is best method but I need a push in the right direction. I am seeking to see if there is a better way to structure, write, and organize code.
I have been looking into object orientated programming using classes but unsure if this is the correct method to make my code better.
I will be continuing to add things for the Arduino to control but this is what I have so far
What my code does: Controls fish tank operations
Brief explanation
-
Checks water level, asks for permission to top off if low, if approved tops of water until water is at high level float switch.
-
When water change button is selected:
checks sensor readings, pumps out water to low level sensor, pause, pumps water in until high level float switch
-manual fill/ drain:
if fill button is pressed fill
if drain button is pressed drain
Thanks for the help!
/* Verison 3.1 ignore top off with millis for 10 minutes
New: When sensor bypass is initiated drain and fill will show screen with option to cancel fill
and drain function
*/
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_LEFT 85
#define TS_RIGHT 900
#define TS_TOP 105
#define TS_BOT 920
// pressure resisistance @300ohms
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
//16 bit color values
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define BOXSIZE 40 //size of boxes for button
#define PENRADIUS 3 //size of what how big of a line (circle)
int oldcolor, currentcolor;
//----------------------------------------
int on=LOW ;
int off=HIGH ;
int fillPin=23 ;
int drainPin=22 ;
int highlevelPin=24 ;
int lowlevelPin= 25 ;
int lowtopoffPin=26 ;
int lowtopOff ;
int SWCButtonNew ;
int highLevel ;
int lowLevel ;
int newState ;
int dt=250 ;
int NewlowLevel;
int drainfillDelay=3000 ;
int py ;
int px ;
int fillbucketlevelPin = 26 ;
int fillbucketLevel ;
int button ;
int d;
int menuChoice ;
int x ;
int currentScreen ;
int oldScreen ;
int topoffScreen ;
int y ;
int ignore ;
unsigned long ignoreStartMillis ;
unsigned long ignoreCurrentMillis ;
unsigned long ignoreTime ;
unsigned long currentMillis ;
const unsigned longPeriod = 1000 ;
int wait;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600) ;
Serial.println("Program run") ;
pinMode(13,OUTPUT) ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
pinMode(drainPin,OUTPUT);
pinMode(highlevelPin,INPUT) ;
pinMode(lowlevelPin,INPUT);
digitalWrite(drainPin,off);
digitalWrite(fillPin,off);
pinMode(fillPin,OUTPUT) ;
ignore = 0 ;
currentMillis ;
tft.reset() ;
tft.begin(0x9341);
tft.setRotation(3) ;
#define MINPRESSURE 5
#define MAXPRESSURE 1000
//create home screen--------------------------------------------------------------------
//Draw white frame
tft.fillScreen (BLUE) ;
tft.drawRect (0,0,319,240,WHITE) ;
//Print "hello" text
tft.setCursor(100,30) ;
tft.setTextColor (WHITE) ;
tft.setTextSize(2) ;
tft.print("Tankduino V3.0") ;
//create water change button
tft.fillRoundRect(40,180,260,40,6,GREEN) ;
tft.drawRoundRect(40,180,260,40,6,WHITE) ;
tft.setCursor(60,188) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Begin Water Change") ;
//create manual fill/drain
tft.fillRoundRect(40,120,260,40,6,GREEN) ;
tft.drawRoundRect(40,120,260,40,6,WHITE) ;
tft.setCursor(60,128) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("manual fill/drain") ;
}
void loop() {
//Serial.println ("restart code") ;
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
menuChoice=1 ;
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
Serial.print("("); Serial.print(px);
Serial.print(", "); Serial.print(py);
Serial.println(")");
if (( px > 40 && px < 300) && (py > 180 && py < 220)) { // if the create water change button is selected
menuChoice=3 ;
}
if (( px > 40 && px < 300) && (py > 120 && py < 160)) { // if the manual fill/drain button is selected
menuChoice=2 ;
}
}
Serial.print ("Menu choice is ");
Serial.println(menuChoice) ;
Serial.print ("Current screen is ") ;
Serial.println (currentScreen) ;
switch(menuChoice) {
//Water change case ------------------------------------------------------------------------
case 3 : //water change case
Serial.println("Water Change") ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//create water change in progress button
tft.fillRect(40,180,260,40,RED) ;
tft.drawRect(40,180,260,40,WHITE) ;
tft.setCursor(60,188) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(1) ;
tft.print("Water change in progress") ;
highLevel=digitalRead(highlevelPin) ;
lowLevel=digitalRead(lowlevelPin) ;
Serial.print("low Level is ") ;
Serial.println(lowLevel) ;
Serial.print("High Level is ") ;
Serial.println(highLevel) ;
if (highLevel==1 && lowLevel==1) {
Serial.println ("sensors working") ;
lowLevel=digitalRead(lowlevelPin) ;
while (lowLevel==1) {
digitalWrite(drainPin,on) ;
Serial.println ("Drain on") ;
lowLevel=digitalRead(lowlevelPin) ;
Serial.print("low level is ") ;
Serial.println (lowLevel) ;
}
digitalWrite(drainPin,off) ;
delay(drainfillDelay) ;
highLevel=digitalRead(highlevelPin) ;
while (highLevel==0) {
digitalWrite(fillPin,on) ;
Serial.println ("Filling") ;
highLevel=digitalRead(highlevelPin) ;
Serial.print("High level pin is ") ;
Serial.println (highLevel) ;
}
digitalWrite(drainPin,off) ;
digitalWrite(fillPin,off) ;
}
// bypass sensor check screen
else {
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillScreen(RED) ;
//Print "Sensor check" text
tft.setCursor(75,30) ;
tft.setTextColor (WHITE) ;
tft.setTextSize(2) ;
tft.print("Sensor reading low") ;
tft.fillRoundRect (24,128, 260, 40,6, WHITE) ;
tft.setCursor (28,140) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Bypass sensor check") ;
tft.fillRoundRect(5,5,30,30,6,BLACK) ;
tft.drawRoundRect(5,5,30,30,6,WHITE) ;
tft.setCursor(11,6) ;
tft.setTextColor(RED) ;
tft.setTextSize(3) ;
tft.print("x") ;
wait=0 ;
while (wait==0) {
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
Serial.print("("); Serial.print(px);
Serial.print(", "); Serial.print(py);
Serial.println(")");
if (( px > 20 && px < 270) && (py > 120 && py < 260)) {
highLevel=digitalRead(highlevelPin) ;
lowLevel=digitalRead(lowlevelPin) ;
Serial.print ("Bypass enabled") ;
while (lowLevel==1) {
digitalWrite(drainPin,on) ;
Serial.println ("Drain on") ;
lowLevel=digitalRead(lowlevelPin) ;
Serial.print("low level is ") ;
Serial.println (lowLevel) ;
}
digitalWrite(drainPin,off) ;
delay(drainfillDelay) ;
highLevel=digitalRead(highlevelPin) ;
while (highLevel==0) {
digitalWrite(fillPin,on) ;
Serial.println ("Filling") ;
highLevel=digitalRead(highlevelPin) ;
Serial.print("High level pin is ") ;
Serial.println (highLevel) ;
}
digitalWrite(drainPin,off) ;
digitalWrite(fillPin,off) ;
}
menuChoice=1 ;
currentScreen =0 ;
break ;
}
if ((px > 5 && px < 35) && (py > 5 && py < 35)) {
Serial.print ("Home") ;
wait=1 ;
menuChoice =1 ;
currentScreen=0 ;
}
}
}
menuChoice=1 ;
currentScreen =0 ;
break ;
// Manual fill/drain case-------------------------------------------------------------------
case 2 :
Serial.println("Enter manual fill/Drain") ;
//Print create fill and drain buttons
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//Print "hello" text
tft.setCursor(100,30) ;
tft.setTextColor (BLUE) ;
tft.setTextSize(4) ;
tft.print("Hello") ;
tft.setCursor(60,30) ;
tft.setTextColor (WHITE) ;
tft.setTextSize(2) ;
tft.print("Manual fill/drain") ;
//create fill button
tft.fillRoundRect(40,180,260,40,6,GREEN) ;
tft.drawRoundRect(40,180,260,40,6,WHITE) ;
tft.setCursor(60,188) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Fill") ;
//create drain
tft.fillRoundRect(40,120,260,40,6,GREEN) ;
tft.drawRoundRect(40,120,260,40,6,WHITE) ;
tft.setCursor(60,128) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Drain") ;
// create back to home button
tft.fillRoundRect(5,5,30,30,6,RED) ;
tft.drawRoundRect(5,5,30,30,6,WHITE) ;
tft.setCursor(11,6) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(3) ;
tft.print("x") ;
x=0 ;
while (x==0) {
Serial.println ("x=0") ;
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
/* Drain */ while ( (px > 40 && px < 300) && (py > 120 && py < 160)) {
Serial.println ("drain") ;
digitalWrite (drainPin, on) ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(40,120,260,40,6,RED) ;
tft.drawRoundRect(40,120,260,40,6,WHITE) ;
tft.setCursor(60,128) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Drain") ;
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
while ( (px > 40 && px < 300) && (py > 120 && py < 160)) {
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
}
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(40,120,260,40,6,GREEN) ;
tft.drawRoundRect(40,120,260,40,6,WHITE) ;
tft.setCursor(60,128) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Drain") ;
}
digitalWrite (drainPin,off) ;
/* pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(40,120,260,40,6,GREEN) ;
tft.drawRoundRect(40,120,260,40,6,WHITE) ;
tft.setCursor(60,128) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Drain") ;
*/
/* fill */ while ( (px > 40 && px < 300) && (py > 180 && py < 220)) {
Serial.println ("fill") ;
digitalWrite (fillPin, on) ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(40,180,260,40,6,RED);
tft.drawRoundRect(40,180,260,40,6,WHITE) ;
tft.setCursor(60,188) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Fill") ;
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
while ( (px > 40 && px < 300) && (py > 180 && py < 220)) {
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
}
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(40,180,260,40,6,GREEN) ;
tft.drawRoundRect(40,180,260,40,6,WHITE) ;
tft.setCursor(60,188) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Fill") ;
}
digitalWrite (fillPin,off) ;
//------------------------
if ((px>5 && px<35) && (py>5 && py <35)) {
x=1 ;
menuChoice=1 ;
currentScreen=0 ;
Serial.println ("Exit") ;
break ;
}
}
// Main menu case ----------------------------------------------------------------------------
case 1 :
if (currentScreen ==0) {
Serial.print ("Main Menu") ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//Draw white frame
tft.fillScreen (BLUE) ;
tft.drawRect (0,0,319,240,WHITE) ;
//Print "hello" text
tft.setCursor(100,30) ;
tft.setTextColor (WHITE) ;
tft.setTextSize(4) ;
tft.print("Hello") ;
//create water change button
tft.fillRoundRect(40,180,260,40,6,GREEN) ;
tft.drawRoundRect(40,180,260,40,6,WHITE) ;
tft.setCursor(60,188) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Begin Water Change") ;
//create manual fill/drain
tft.fillRoundRect(40,120,260,40,6,GREEN) ;
tft.drawRoundRect(40,120,260,40,6,WHITE) ;
tft.setCursor(60,128) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("manual fill/drain") ;
currentScreen=1 ;
break ;
}
}
// Automatic top off with approval of topping off from touchscreen user
highLevel = digitalRead (highlevelPin) ;
lowtopOff= digitalRead (lowtopoffPin) ;
currentMillis= (millis());
ignoreTime=currentMillis - ignoreStartMillis ;
// Serial.println(ignoreTime) ;
if (ignoreTime > 600000) {
ignore=0 ;
}
if ((lowtopOff==0) && (ignore==0)) {
/// Alert screen When fish tank needs to be topped off
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillScreen(RED) ;
tft.fillRoundRect (24,128, 260, 40,6, WHITE) ;
tft.setCursor (28,140) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Approve water top off") ;
tft.fillRoundRect(5,5,30,30,6,BLACK) ;
tft.drawRoundRect(5,5,30,30,6,WHITE) ;
tft.setCursor(11,6) ;
tft.setTextColor(RED) ;
tft.setTextSize(3) ;
tft.print("x") ;
x=0 ;
while ((lowtopOff==0) && (ignore==0)) {
digitalWrite (13,HIGH) ;
TSPoint p = ts.getPoint() ;
digitalWrite (13,LOW) ;
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
py = map (p.x, TS_TOP, TS_BOT, 0, tft.height()) ;
px = map (p.y, TS_LEFT, TS_RIGHT,0, tft.width()) ;
Serial.print("("); Serial.print(px);
Serial.print(", "); Serial.print(py);
Serial.println(")");
if ((px > 5 && px < 35) && (py > 5 && py < 35)) {
Serial.print ("Ignore") ;
ignore=1 ;
ignoreStartMillis=(millis()) ;
menuChoice =1 ;
currentScreen=0 ;
}
if (( px > 20 && px < 270) && (py > 120 && py < 260)) {
highLevel=digitalRead(highlevelPin) ;
Serial.println ("Top off Approved") ;
Serial.print("High level is ") ;
Serial.println(highLevel) ;
while (highLevel ==0) {
Serial.println("Topping off") ;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillScreen(RED) ;
tft.fillRect (20,120, 250, 40, GREEN) ;
tft.setCursor (20,140) ;
tft.setTextColor(BLACK) ;
tft.setTextSize(2) ;
tft.print("Topping off water") ;
digitalWrite(fillPin,on);
highLevel=digitalRead(highlevelPin) ;
}
digitalWrite (fillPin,off) ;
highLevel=digitalRead(highlevelPin) ;
lowtopOff= digitalRead (lowtopoffPin) ;
menuChoice=1 ;
currentScreen =0 ;
}
}
}
}
}