Arduino programming

Mak'er Grow

Well-Known Member
To get to the point...looking for some help fixing up the spaghetti mess of code I have now.
I may eventually figure it out, but got to thinking someone may have some time on their hands and want to help. :)
I'm programming an Arduino Mega with a few relay boards sensors and LCD touch screen (with SD card slot) and maybe eventually a couple cameras.
I have started the touch screen parts and a few things to display on LCD from sensors, but I know things can/could be simplified (small routines maybe) in a few areas, but I'm not that advanced as a programmer.
Thanks to anyone interested...send a PM if you choose.
 

Renfro

Well-Known Member
I haven't done any programming in the last 15 years lol but if I were trying to do what you are doing I would start by taking a look at the code linked on this page. It may be as simple as just modifying it and / or integrating new functionality.


I am sure there are other resources out there that others will hopefully point out.
 

ilovereggae

Well-Known Member
check out this thread, the ppl in there can help you out for sure

 

Mak'er Grow

Well-Known Member
check out this thread, the ppl in there can help you out for sure

Did you happen to notice my many posts in that thread ? :P


I haven't done any programming in the last 15 years lol but if I were trying to do what you are doing I would start by taking a look at the code linked on this page. It may be as simple as just modifying it and / or integrating new functionality.


I am sure there are other resources out there that others will hopefully point out.
It would almost be a shame to go that route now. I have a lot done already, but I know theres more then likely a much better way to do a few things I kind of stumbled my way through...lol
It works, I guess, but it could be shortened for sure from the just over 600 lines I have so far.
1 Arduino Mega controls 4 tents (well its 1 big box cut into 4 smaller 2' x 4' grow boxes).
 

solakani

Well-Known Member
To get to the point...looking for some help fixing up the spaghetti mess of code I have now.
I may eventually figure it out, but got to thinking someone may have some time on their hands and want to help. :)
I'm programming an Arduino Mega with a few relay boards sensors and LCD touch screen (with SD card slot) and maybe eventually a couple cameras.
I have started the touch screen parts and a few things to display on LCD from sensors, but I know things can/could be simplified (small routines maybe) in a few areas, but I'm not that advanced as a programmer.
Thanks to anyone interested...send a PM if you choose.
Is your code base C++? open source?
 

SamWE19

Well-Known Member
Just to throw a spanner in the works for you.

try a raspberry pi central server running home assistant and then several WiFi enabled ESP32s controlling different devices that send data back to your PI. I found it much easier to program since there is already thousands of preprogrammed source codes available
 

Mak'er Grow

Well-Known Member
Is your code base C++? open source?
I'd say opensource basically. I've been finding sketches of similar things I need and altering it to do what I want it to do...which where all open source.
I have been using the Arduino programming app, its C++ based.

Just to throw a spanner in the works for you.

try a raspberry pi central server running home assistant and then several WiFi enabled ESP32s controlling different devices that send data back to your PI. I found it much easier to program since there is already thousands of preprogrammed source codes available
That sounds like a lot of work to change everything over to Ras Pi now...I like the Arduino better anyways...sure it would be nicer if it was a bit better, but what I want/need it to do, it does just great, turn a few relays on/off, read some sensors and show stats on the 2.8" LCD touch screen with micro SD slot. Much later I want to add code for 2 ( want 4, but only got 2 for testing atm) cameras I have


Its not that I don't know how to code, I just don't know all of what I should to do this properly And its starting to get out of hand I think...I think new routines can be made from the mess of code I managed to get to work. :P
 

Mak'er Grow

Well-Known Member
How many classes in your 600 lines of C++? If you want help, I can take a look.
That is my biggest downfall...I can make it work, but not sure of the proper names of things...yet. ;)
Is it ok to PM the code to you?
Or anyone else that would like a look let me know.
Eventually I want to share it as open source in the end...fyi. :P
 

solakani

Well-Known Member
That is my biggest downfall...I can make it work, but not sure of the proper names of things...yet. ;)
Is it ok to PM the code to you?
Or anyone else that would like a look let me know.
Eventually I want to share it as open source in the end...fyi. :P
Please code share here any snippets you are working on proper names.
 

Mak'er Grow

Well-Known Member
Well here it is in its entirety atm...I will be adding 4 more SSR's to control the tiny recirculating pump on and off times...and add a bit more code to control them. :P
It has some 'extra' stuff in there mostly due to limitation to 2 times all the parts...so I added a bit of an editor for time and date...simple things tho as you will see. :)
Now remember I'm no programmer, but this is my Frankenstein creation as of today...with a few edits needed...to remove values and make proper screens/info and so much more...be gentle...lmao ;P

Code:
//========================================================================================================================================
//  USER SETTINGS FOR 16 TIMERS (4 RELAY BOARDS @ 4 RELAYS EACH)
//  Use "army" time for settings...minus the "0"...example...09:00 (9am) = 9 as value....0 = Midnight/12:00am
//  00 to 23.
//  Cannot do pm to am unless you reverse digitalRead(s) & digitalWrites for that Timer/Relay # and reverse times
//  for ON/OFF, due to now setting an OFF Run Time instead of ON Run Time.
//
//---->   Last edit = Dec 14, 2020.
//
//========================================================================================================================================

int L1_S = 7;       //Starting @
int L1_E = 23;      //Ending @
int P1_S = 2;       //Every x minutes
int P1_R = 10;      //For y seconds
int G1_D = 1;       //Day to start
int G1_M = 1;       //Month to start
int G1_Y = 2018;    //Year to start
int G1_L = 60;      //Grow for x days

int L2_S = 7;       //Starting @
int L2_E = 23;      //Ending @
int P2_S = 2;       //Every x minutes
int P2_R = 10;      //For y seconds
int G2_D = 1;       //Day to start
int G2_M = 1;       //Month to start
int G2_Y = 2018;    //Year to start
int G2_L = 60;      //Grow for x days

int L3_S = 7;       //Starting @
int L3_E = 19;      //Ending @
int P3_S = 1;       //Every x minutes
int P3_R = 10;      //For y seconds
int G3_D = 1;       //Day to start
int G3_M = 1;       //Month to start
int G3_Y = 2018;    //Year to start
int G3_L = 60;      //Grow for x days

int L4_S = 7;       //Starting @
int L4_E = 19;      //Ending @
int P4_S = 1;       //Every x minutes
int P4_R = 10;      //For y seconds
int G4_D = 1;       //Day to start
int G4_M = 1;       //Month to start
int G4_Y = 2018;    //Year to start
int G4_L = 60;      //Grow for x days

#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <UTFT.h>
#include <URTouch.h>
#include <avr/pgmspace.h>
#include <SPI.h>
#include <SD.h>
#include "BlueDot_BME280.h"
#include <avr/wdt.h>

#define TIME_Q   'T'
#define DAY_Q   'D'
#define MONTH_Q   'M'
#define YEAR_Q   'Y'
#define hour_Q   'h'
#define minute_Q   'm'
#define second_Q   's'
#define read_Q   'r'
#define clrScrn_Q    'c'
#define reset_Q   'R'
#define preset_Q  'p'
#define misc_Q  'x'
#define menu_Q  '?'
#define TCAADDR 0x70

static boolean isLongFormat = true;

extern uint8_t SmallFont[];         //40 Characters X 20 Lines (8x12 px)
extern uint8_t BigFont[];           //20 Characters X 15 Lines (16x16 px)

const char *MITY[12] = {"Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"};

String D = "";
String C = "";
int Y = 2020;
String data = "";
double TempC;
double TempF;
String TempChoice = "C";    //"C" or "F"
int CurDay = 10;
BlueDot_BME280 bme280_i;

File myFile;
const int chipSelect = 4;
String fileName = "";
String Rx = "";
String tempRx = "";
String rxName = "";
String rxValue = "";
String varName = "";
String varValue = "";

String b1PlantName = "";
String b1LightStart = "";
String b1LightEnd = "";
String b1PumpStart = "";
String b1PumpRun = "";

String b2PlantName = "";
String b2LightStart = "";
String b2LightEnd = "";
String b2PumpStart = "";
String b2PumpRun = "";

String b3PlantName = "";
String b3LightStart = "";
String b3LightEnd = "";
String b3PumpStart = "";
String b3PumpRun = "";

String b4PlantName = "";
String b4LightStart = "";
String b4LightEnd = "";
String b4PumpStart = "";
String b4PumpRun = "";

int L_S = 0;
int L_E = 0;
int P_S = 0;
int P_R = 0;
int c1 = 0;
int c2 = 0;
int c3 = 0;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
int fillIn = 0;
int Pg = 0;
int x, y;
int Bright = 0;
int zzz = 2000;
int resync = 0;
int rtcReads = 0;
int rtcPassed = 0;
int rtcFailed = 0;
int resyncFailed = 0;

//----------------------------------------------------------------------------------------------------------------------------
UTFT myGLCD(HX8347I, 38, 39, 40, 41); URTouch  myTouch( 6, 5, 4, 3, 2);       //Big box display.
//UTFT myGLCD(SSD1289, 38, 39, 40, 41); URTouch  myTouch( 6, 5, 4, 3, 2);       //Test display...broken.
//----------------------------------------------------------------------------------------------------------------------------

void setup() {
  Serial.begin(9600);
  wdt_enable(WDTO_8S);
  Wire.begin();
  Serial.println("");
  Serial.println("Looking for SD card...");
  pinMode(SS, OUTPUT);
  if (SD.begin(chipSelect))
  {
    Serial.print("Found...\nReading data.");
    for (int B = 1; B <= 4; B++) {
      varName = "";
      varValue = "";
      fileName = "BOX" + String(B);
      fileName = fileName + ".txt";
      myFile = SD.open(fileName);
      if (myFile) {readFile(B);} myFile.close();} Serial.println("Done.");} else {Serial.println("No card found...Using Default values.");}

  Read_RTC();

  myGLCD.InitLCD(0);   //0=Portrait 1=Landscape (default) ??=Rotate 90' (90/180/270)
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  pinMode(14, OUTPUT);    //RELAY 1 #1 - Lights
  pinMode(16, OUTPUT);    //RELAY 1 #2 - Water Pump
  pinMode(A8, INPUT);     //SENSOR #1 - Box Temp
  pinMode(A9, INPUT);     //SENSOR #2 - Water Temp

  pinMode(15, OUTPUT);    //RELAY 1 #3 - Lights
  pinMode(17, OUTPUT);    //RELAY 1 #4 - Water Pump
  pinMode(A10, INPUT);    //SENSOR #3 - Box Temp
  pinMode(A11, INPUT);    //SENSOR #4 - Water Temp

  pinMode(8, OUTPUT);     //RELAY 2 #1 - Lights
  pinMode(10, OUTPUT);    //RELAY 2 #2 - Water Pump
  pinMode(A12, INPUT);    //SENSOR #5 - Box Temp
  pinMode(A13, INPUT);    //SENSOR #6 - Water Temp

  pinMode(9, OUTPUT);     //RELAY 2 #3 - Lights
  pinMode(11, OUTPUT);    //RELAY 2 #4 - Water Pump
  pinMode(A14, INPUT);    //SENSOR #7 - Box Temp
  pinMode(A15, INPUT);    //SENSOR #8 - Water Temp

  listMenu();
}

void loop() {

  showPg(0);

  resyncRTC();

  portCheck();

  lightCheck("L1", 14, 4, 75);
  TempCheck(1, 3, 110);
  HumiCheck(1, 3, 140);
  pumpCheck("P1", 16, 3, 175);

  lightCheck("L2", 15, 123, 75);
  TempCheck(2, 123, 110);
  HumiCheck(2, 123, 140);
  pumpCheck("P2", 17, 123, 175);

  lightCheck("L3", 8, 3, 200);
  TempCheck(3, 3, 235);
  HumiCheck(3, 3, 265);
  pumpCheck("P3", 10, 3, 300);

  lightCheck("L4", 9, 123, 200);
  TempCheck(4, 123, 235);
  HumiCheck(4, 123, 265);
  pumpCheck("P4", 11, 123, 300);

  touchCheck();
}
<CONTINUES TO NEXT POST>
 
Last edited:

Mak'er Grow

Well-Known Member
<CONTINUED CODE>

Code:
void Date(int h, int v) {
  D = month();
  D = D + String(" ");
  if (day() < 10) {
    D = D + "0"  + String(day());
  } else {
    D = D + String(day());
  }
  D = D + " " + String(year());
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(BigFont);
  myGLCD.print(" " + D + " ", h, v);
}

void Clock(int h, int v) {
  C = hour();
  C = C + String(":");
  //if (hour() < 1) {C = String("12") + String(":");} else {if (hour() > 12) {if (hour() < 22) {C = String(" ") + String(hour() - 12) + String(":");} else {C = String(hour() - 12) + String(":");}} else {C = (hour()) + String(":");}}
  if (minute() < 10) {
    C = C + String("0") + String(minute()) + String(":");
  } else {
    C = C + String(minute()) + String(":");
  }
  if (second() < 10) {
    C = C + String("0") + String(second());
  } else {
    C = C + String(second());
  }
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(BigFont);
  myGLCD.print(" " + C + " ", h, v);
}

void resyncRTC() {
  if (year() != Y) {delay(5000); Read_RTC();}
  if (resync == 0) {if (minute() == 0) {if (second() == 0) {Read_RTC(); resync = 1; resyncFailed = resyncFailed + 1;}}}
  if (resync == 1) {if (minute() == 1) {if (second() == 1) {resync = 0;}}}
}

void listMenu() {
  Serial.println();
  Serial.println("T = Input all 10 digits.   eg T1536620390 = Sept 10 2018 @ 10:59:50pm");
  Serial.println("D = Add 1 DAY.");
  Serial.println("M = Add 1 MONTH.");
  Serial.println("Y = Add 1 YEAR.");
  Serial.println("h = Add 1 hour.");
  Serial.println("m = Add 1 minute.");
  Serial.println("s = Add 1 second.");
  Serial.println("r = Read current value.");
  Serial.println("c = Clears/Refreshes LCD screen.");
  Serial.println("R = Reset to Jan 01 1970.");
  Serial.println("p = Preset value @ 1603726000 = Oct 26 2020 @ 15:26:40");
  Serial.println("x = setTime(14, 59, 00, 26, 10, 2020)");
  Serial.println("            hh, mm, ss, DD, MM, YYYY)");
  Serial.println("? = Show input list/info.");
}

void portCheck() {
  if (Serial.available() > 0)
  { char Rx = Serial.read();
    if (Rx == TIME_Q) {
      setTime(Serial.parseInt());
    }
    if (Rx == DAY_Q) {
      setTime(now() + 86400);
    }
    if (Rx == MONTH_Q) {
      if (String(month()) == "1" or String(month()) == "3" or String(month()) == "5" or String(month()) == "7" or String(month()) == "8" or String(month()) == "10" or String(month()) == "12") {setTime(now() + 2678400);} else {if (String(month()) == "4" or String(month()) == "6" or String(month()) == "9" or String(month()) == "11") {setTime(now() + 2592000);} else {if (String(month()) == "2") {setTime(now() + 2419200);}}}}
    if (Rx == YEAR_Q) {setTime(now() + (86400 * 365));}
    if (Rx == hour_Q) {setTime(now() + 3600);}
    if (Rx == minute_Q) {setTime(now() + 60);}
    if (Rx == second_Q) {setTime(now() + 1);}
    if (Rx == read_Q) {Serial.println(String(now()));}
    if (Rx == clrScrn_Q) {myGLCD.clrScr();}
    if (Rx == reset_Q) {setTime(0);}
    if (Rx == preset_Q) {setTime(1603726000);} //T1603726000}
    if (Rx == misc_Q)  {setTime(14, 59, 00, 26, 10, 2020);}
    if (Rx == menu_Q) {listMenu();}
    }
}

void lightCheck(String Light, int Pin, int h, int v) {

  if ((Light + "_S") == "L1_S") {L_S = L1_S;}
  if ((Light + "_E") == "L1_E") {L_E = L1_E;}
  if ((Light + "_S") == "L2_S") {L_S = L2_S;}
  if ((Light + "_E") == "L2_E") {L_E = L2_E;}
  if ((Light + "_S") == "L3_S") {L_S = L3_S;}
  if ((Light + "_E") == "L3_E") {L_E = L3_E;}
  if ((Light + "_S") == "L4_S") {L_S = L4_S;}
  if ((Light + "_E") == "L4_E") {L_E = L4_E;}
  if (hour() < L_S or hour() > L_E - 1 && digitalRead(Pin) == 0) {digitalWrite(Pin, 1);}
  if (hour() >= L_S && hour() <= L_E - 1 && digitalRead(Pin) == 1) {digitalWrite(Pin, 0);}
  if (digitalRead(Pin) == 0) {myGLCD.setColor(0, 255, 0);} else {myGLCD.setColor(255, 0, 0);}
  myGLCD.print(String(L_S) + " - " + String(L_E), h, v);
  myGLCD.setColor(255, 255, 255);
}

void pumpCheck(String Pump, int Pin, int h, int v) {

  if ((Pump + "_S") == "P1_S") {P_S = P1_S;}
  if ((Pump + "_R") == "P1_R") {P_R = P1_R;}
  if ((Pump + "_S") == "P2_S") {P_S = P2_S;}
  if ((Pump + "_R") == "P2_R") {P_R = P2_R;}
  if ((Pump + "_S") == "P3_S") {P_S = P3_S;}
  if ((Pump + "_R") == "P3_R") {P_R = P3_R;}
  if ((Pump + "_S") == "P4_S") {P_S = P4_S;}
  if ((Pump + "_R") == "P4_R") {P_R = P4_R;}
  if ((digitalRead(Pin) == 1) && (minute() % P_S == 0) && (second() < P_R)) {digitalWrite(Pin, 0);}
  if ((digitalRead(Pin) == 0) && (second() > P_R - 1)) {digitalWrite(Pin, 1);}
  if (digitalRead(Pin) == 0) {myGLCD.setColor(0, 255, 0);} else {myGLCD.setColor(255, 0, 0);}
  myGLCD.print(String(P_R) + " - " + String(P_S), h, v);
  myGLCD.setColor(255, 255, 255);
}

void tcaselect(uint8_t i) {
  if (i > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
  bme280_i.parameter.communication = 0;
  bme280_i.parameter.I2CAddress = 0x76;                    //Choose I2C Address
  bme280_i.parameter.sensorMode = 0b11;                    //Choose sensor mode
  bme280_i.parameter.IIRfilter = 0b100;                    //Setup for IIR Filter
  bme280_i.parameter.humidOversampling = 0b101;            //Setup Humidity Oversampling
  bme280_i.parameter.tempOversampling = 0b101;             //Setup Temperature Ovesampling
  bme280_i.parameter.pressOversampling = 0b101;            //Setup Pressure Oversampling
  bme280_i.init();
}

double TempCheck(int pin, int h, int v) {
  double Temp;
  tcaselect(pin);
  if (TempChoice = "C") {
    myGLCD.print(" " + String(bme280_i.readTempC(), 0) + " C ", h, v);
  } else {
    myGLCD.print(" " + String(bme280_i.readTempF(), 0) + " F ", h, v);
  }
}

double HumiCheck(int pin, int h, int v) {
  double Temp;
  tcaselect(pin);
  myGLCD.print(" " + String(bme280_i.readHumidity(), 0) + " % ", h, v);
}

void Read_RTC() {
  tcaselect(0);
  setSyncProvider(RTC.get);
  rtcReads = rtcReads + 1;
  if (timeStatus() != timeSet) {Serial.println("RTC read fall."); rtcFailed = rtcFailed + 1; if (rtcFailed == 5) {showPg(99);}}  //Failed read.
  if (timeStatus() == timeSet) {Serial.println("RTC is ON."); rtcPassed = rtcPassed + 1;}  //Good read.
}

void readFile(int BX) {
  while (myFile.available()) {
    char Rx = myFile.read();

    if (String(Rx) == "=") { ;} else {if (String(Rx) == "!") { ;} else {if (String(Rx) == " ") { ;} else {if (Rx == 10) { ;} else {if (Rx == 13) { ;} else {tempRx += Rx;}}}}}
    if (String(Rx) == "=") {rxName = tempRx; varName = "b" + String(BX); varName = String(varName) + String(rxName); tempRx = ""; Rx = ""; Serial.print(".");}
    if (String(Rx) == "!") {rxValue = tempRx; varValue = rxValue; tempRx = ""; Rx = ""; Serial.print(".");}

// pattern = b + BX + char$/variable name

    if (String(varName) == "b1PlantName") {b1PlantName = varValue;}
    if (String(varName) == "b1LightStart") {L1_S = varValue.toInt();}
    if (String(varName) == "b1LightEnd") {L1_E = varValue.toInt();}
    if (String(varName) == "b1PumpStart") {P1_S = varValue.toInt();}
    if (String(varName) == "b1PumpRun") {P1_R = varValue.toInt();}

    if (String(varName) == "b2PlantName") {b2PlantName = varValue;}
    if (String(varName) == "b2LightStart") {L2_S = varValue.toInt();}
    if (String(varName) == "b2LightEnd") {L2_E = varValue.toInt();}
    if (String(varName) == "b2PumpStart") {P2_S = varValue.toInt();}
    if (String(varName) == "b2PumpRun") {P2_R = varValue.toInt();}

    if (String(varName) == "b3PlantName") {b3PlantName = varValue;}
    if (String(varName) == "b3LightStart") {L3_S = varValue.toInt();}
    if (String(varName) == "b3LightEnd") {L3_E = varValue.toInt();}
    if (String(varName) == "b3PumpStart") {P3_S = varValue.toInt();}
    if (String(varName) == "b3PumpRun") {P3_R = varValue.toInt();}

    if (String(varName) == "b4PlantName") {b4PlantName = varValue;}
    if (String(varName) == "b4LightStart") {L4_S = varValue.toInt();}
    if (String(varName) == "b4LightEnd") {L4_E = varValue.toInt();}
    if (String(varName) == "b4PumpStart") {P4_S = varValue.toInt();}
    if (String(varName) == "b4PumpRun") {P4_R = varValue.toInt();}
    }
  }

void box(int c1, int c2, int c3, int x1, int y1, int x2, int y2, int fillIn) {
  myGLCD.setColor(c1, c2, c3);
  if (fillIn == 0) {myGLCD.drawRect(x1, y1, x2, y2);} else {myGLCD.fillRect(x1, y1, x2, y2);}
  }

void touchCheck() {
  if (myTouch.dataAvailable())
  {myTouch.read();
  x = myTouch.getX();
  y = myTouch.getY();
  if ((x >= 0) && (x <=65) && (y >= 0) && (y <= 50)) {showPg(99);}
  if ((x >= 0) && (x <=65) && (y >= 50) && (y <= 320)) {showPg(5);}
  if ((y >= 1) && (y <= 120)) {if ((x >= 65) && (x <= 190)) {box(255, 0, 0, 1, 71, 120, 195, 1); showPg(1);}
  if ((x >= 191) && (x <= 320)) {box(255, 0, 0, 1, 196, 120, 318, 1); showPg(3);}}
  else {if ((x >= 65) && (x <= 190)) {box(255, 0, 0, 121, 71, 238, 195, 1); showPg(2);}
  if ((x >= 191) && (x <= 320)) {box(255, 0, 0, 121, 196, 238, 318, 1); showPg(4);}}
  }
}
<CONTINUE AGAIN...>
 
Last edited:

Mak'er Grow

Well-Known Member
<CONTINUE #2...>

Code:
void showPg(int Pg) {
  if (Pg == 0)
  {
    basicPg();
    box(255, 255, 255, 1, 71, 120, 195, 0); //BOX 1 - Top Left frame
    box(255, 255, 255, 121, 71, 238, 195, 0); //BOX 2 - Top Right frame
    box(255, 255, 255, 1, 196, 120, 318, 0); //BOX 3 - Bottom Left frame
    box(255, 255, 255, 121, 196, 238, 318, 0); //BOX 4 - Bottom Right frame
  }
  if (Pg == 1)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("BOX 1", CENTER, 75);
    myGLCD.print(b1PlantName, 10, 100);
    if (L1_E - L1_S > 12) {myGLCD.print("Flower Cycle", 10, 125);} else {myGLCD.print("Veg Cycle", 10, 125);}
    if (CurDay < 10) {myGLCD.print("Day 0" + String(CurDay) + " of " + String(G1_L), 10, 150);} else {myGLCD.print("Day " + String(CurDay) + " of " + String(G1_L), 10, 150);}
    delay(zzz);
    myGLCD.clrScr();
  }
  if (Pg == 2)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("BOX 2", CENTER, 75);
    myGLCD.print(b2PlantName, 10, 100);
    if (L2_E - L2_S > 12) {myGLCD.print("Flower Cycle    ", 10, 125);} else {myGLCD.print("Veg Cycle    ", 10, 125);}
    if (CurDay < 10) {myGLCD.print("Day 0" + String(CurDay) + " of " + String(G2_L), 10, 150);} else {myGLCD.print("Day " + String(CurDay) + " of " + String(G2_L), 10, 150);}
    delay(zzz);
    myGLCD.clrScr();
  }
  if (Pg == 3)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("BOX 3", CENTER, 75);
    myGLCD.print(b3PlantName, 10, 100);
    if (L3_E - L3_S > 12) {myGLCD.print("Flower Cycle    ", 10, 125);} else {myGLCD.print("Veg Cycle    ", 10, 125);}
    if (CurDay < 10) {myGLCD.print("Day 0" + String(CurDay) + " of " + String(G3_L), 10, 150);} else {myGLCD.print("Day " + String(CurDay) + " of " + String(G3_L), 10, 150);}
    delay(zzz);
    myGLCD.clrScr();
  }
  if (Pg == 4)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);
    myGLCD.print("BOX 4", CENTER, 75);
    myGLCD.print(b4PlantName, 10, 100);
    if (L4_E - L4_S > 12) {myGLCD.print("Flower Cycle    ", 10, 125);} else {myGLCD.print("Veg Cycle    ", 10, 125);}
    if (CurDay < 10) {myGLCD.print("Day 0" + String(CurDay) + " of " + String(G4_L), 10, 150);} else {myGLCD.print(" Day " + String(CurDay) + " of " + String(G4_L), 10, 150);}
    delay(zzz);
    myGLCD.clrScr();
  }
  if (Pg == 5)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);

    Serial.println("Reads:");
    Serial.print(rtcReads);
    myGLCD.print("Reads:", 10, 75);
    myGLCD.print(String(rtcReads), 200, 75);
    Serial.println("Passed Read");
    Serial.print(rtcPassed);
    myGLCD.print("Passed", 10, 100);
    myGLCD.print(String(rtcPassed), 200, 100);
    Serial.println("Failed Read");
    Serial.print(rtcFailed);
    myGLCD.print("Failed", 10, 125);
    myGLCD.print(String(rtcFailed), 200, 125);
    Serial.println("Resync Fail");
    Serial.print(resyncFailed);
    myGLCD.print("Resync Fail", 10, 150);
    myGLCD.print(String(resyncFailed), 200, 150);

    exitButton(); //was delay(zzz);
    myGLCD.clrScr();
  }
  if (Pg == 10)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);
//
    myGLCD.clrScr();
  }
  if (Pg == 99)
  {
    myGLCD.clrScr();
    basicPg();
    myGLCD.setFont(BigFont);
    myGLCD.setColor(255, 255, 255);
    Serial.print("RESETTING");
    myGLCD.print("RESETTING", CENTER, 90);
    Serial.println("---------");
    myGLCD.print("---------", CENTER, 110);
    for (int x=0; x<=8; x++) {Serial.println(8-x); myGLCD.print("         ", CENTER, 150); delay(100); myGLCD.print(String(8-x), CENTER, 150); delay(900);}
    myGLCD.clrScr();
  }
}

void basicPg() {
  box(255, 255, 255, 0, 0, 239, 319, 0); //Main outside frame
  box(255, 255, 255, 1, 1, 238, 70, 0); //Top center frame
  myGLCD.setColor(0, 255, 0);
  myGLCD.setFont(BigFont);
  myGLCD.print("T0YB0X", CENTER, 6);
  myGLCD.setColor(0, 0, 0);
  myGLCD.drawLine(2, 25, 237, 25);
  Date(CENTER, 30);
  Clock(CENTER, 50);

  wdt_reset();
}

void exitButton() {

  box(255, 255, 255, 10, 175, 50, 215, 0);
  myGLCD.print("1", 22, 187);
  box(255, 255, 255, 55, 175, 95, 215, 0);
  myGLCD.print("2", 67, 187);
  box(255, 255, 255, 100, 175, 140, 215, 0);
  myGLCD.print("3", 112, 187);
  box(255, 255, 255, 145, 175, 185, 215, 0);
  myGLCD.print("4", 157, 187);
  box(255, 255, 255, 190, 175, 230, 215, 0);
  myGLCD.print("5", 202, 187);
  box(255, 255, 255, 10, 225, 50, 265, 0);
  myGLCD.print("6", 22, 237);
  box(255, 255, 255, 55, 225, 95, 265, 0);
  myGLCD.print("7", 67, 237);
  box(255, 255, 255, 100, 225, 140, 265, 0);
  myGLCD.print("8", 112, 237);
  box(255, 255, 255, 145, 225, 185, 265, 0);
  myGLCD.print("9", 157, 237);
  box(255, 255, 255, 190, 225, 230, 265, 0);
  myGLCD.print("0", 202, 237);
  box(255, 255, 255, 10, 275, 228, 309, 0);
  myGLCD.print("E X I T", CENTER, 285);

  delay(zzz*2);

  myGLCD.clrScr();

  myTouch.read();
  x = myTouch.getX();
  y = myTouch.getY();

  myGLCD.print(String(x), 22, 187);
  myGLCD.print(String(y), 22, 237);

  delay(zzz*10);
}
<CODE FINISHED.>
 
Last edited:

Mak'er Grow

Well-Known Member
Well I hope that pasted right...will look later on how to shrink it up, but thats where I'm at...which I'm sure can be 'fixed' up some and then on to cameras...I have 2 and think it would be neat to make stop frame videos of my grows. :)
 

solakani

Well-Known Member
Thanks for sharing. Disclaimer. I can help with the C++ but just getting into embedded systems. I do not have access to arduino ide atm and unable to test any changes to your project. I find your listing to be structured into functions. Recommend trying OOP with classes. It may be a steep learning curve but as a beginner, you do not have to unlearn structured programming.

Which function to focus on first?

UTFT myGLCD(HX8347I, 38, 39, 40, 41); URTouch myTouch( 6, 5, 4, 3, 2); //Big box display.
 

Mak'er Grow

Well-Known Member
Thanks for sharing. Disclaimer. I can help with the C++ but just getting into embedded systems. I do not have access to arduino ide atm and unable to test any changes to your project. I find your listing to be structured into functions. Recommend trying OOP with classes. It may be a steep learning curve but as a beginner, you do not have to unlearn structured programming.

Which function to focus on first?

UTFT myGLCD(HX8347I, 38, 39, 40, 41); URTouch myTouch( 6, 5, 4, 3, 2); //Big box display.
This line sets up which pins to use on the LCD shields, ordered 2 the same, but seller did a swap on me...got refunded so no big prob...lol,, and I added the touch pins as well. This could/should be 2 lines...I just put it into 1 line for space really.
Is there a problem that can happen by doing this?
Do you have any sites that can help me understand more about using "OOP with classes"...don't want to waste time reading something useless...lol
 

guitarguy10

Well-Known Member
I haven't looked through your code yet my head is splitting, but if you are using a device over SPI (serial peripheral interface) that has MOSI, MISO, SCK, , SS, GND, VCC on an Arduino Mega they use different pins then an Arduino Uno.

Specifically MISO=50, MOSI=51, SCK=52 and SS=53. You can also just connect it straight to the ICSP header but documentation is uh confusing if you're not a pro at all of this (I am not, I am telling you this from my own headaches in experience).

You never said what you're problem is though? Does your code actually work? If not where and what errors is it spitting out?

If it fits on the controller optimization isn't really that important, unless you're going to be like selling these or something. But if it works and fits thats all that matters.

I can PM you my gardener's microcontroller code if you want but TBH all you really need to know how to do is load the correct libraries into the arduino IDE and they all come with a bunch of example codes that you can just copy/paste and modify a few variable names, function calls etc.

If you are planning to use a camera module and save pictures onto an SD card I *highly* recomend that you get something like an adafruit data logging shield. I have tried to do it with just the parts (SD card reader and an RTC) but it is truly a PITA that requires you resoldering a point on the SD card reader because they're generally not wired to work out the box on SPI connected to an RTC.
 
Top