The Planted Tank Forum banner

arduino coding help?

2K views 15 replies 4 participants last post by  Bryanmc1988 
#1 · (Edited)
ok so i am having a little issue with trying to make the case smaller due to its repeating it self for timer 1-4...

i know that in normal functions you can use the "for loop" to reprint things that are repeating but what about the touch portion on a utft?


here is my sketch that i want to make smaller...

Code:
    case 10:     //--------------------- Display Set Timer SCREEN -------------------------
if ((x>=1) && (y>=31) && (x<=318) && (y<=55)                                // Set Misting Timer 1
         && (mistingT1OnH==0) && (mistingT1OnM==0) && (mistingT1OnS==0) 
         && (mistingT1OffH==0) && (mistingT1OffM==0) && (mistingT1OffS==0)
         || (x>=1) && (y>=32) && (x<=220) && (y<=55))
      {
        timerPage=1;
        timerHoursOn=timeHour; timerMinsOn=t.min; timerSecsOn=t.sec;
        timerHoursOff=timeHour; timerMinsOff=t.min; timerSecsOff=t.sec;
        timerAmPmOn=AM_PM; timerAmPmOff=AM_PM;
        timerSetSu=0; timerSetMo=0; timerSetTu=0; timerSetWe=0;
        timerSetTh=0; timerSetFr=0; timerSetSa=0;
        dispScreen=9;
        clearScreen();
        setTimerScreen();
      }
    if ((mistingT1OnH>0) || (mistingT1OnM>0) || (mistingT1OnS>0) || (mistingT1OffH>0) || (mistingT1OffM>0) || (mistingT1OffS>0))
      {
        if ((x>=onOffTimerButton1[0]) && (y>=onOffTimerButton1[1]) && (x<=onOffTimerButton1[2]) && (y<=onOffTimerButton1[3]))       //Pushing On/Off Button
          {
            if (mistingSwitch1OnOff==false)
              {
                mistingSwitch1OnOff=true;
              } else {
                  mistingSwitch1OnOff=false;}
  
            waitForIt(onOffTimerButton1[0], onOffTimerButton1[1], onOffTimerButton1[2], onOffTimerButton1[3]);
            SaveMistingSwitch1OnOffToEEPROM();
            dispScreen=10;
            clearScreen();
            displayTimerScreen();
          }
        if ((x>=resetTimerButton1[0]) && (y>=resetTimerButton1[1]) && (x<=resetTimerButton1[2]) && (y<=resetTimerButton1[3]))       //Pushing Reset Button
            {
              waitForIt(resetTimerButton1[0], resetTimerButton1[1], resetTimerButton1[2], resetTimerButton1[3]);
              mistingT1OnH=0; mistingT1OnM=0; mistingT1OnS=0;
              mistingT1OffH=0; mistingT1OffM=0; mistingT1OffS=0;
              timerAmPm1On=1; timerAmPm1Off=1;
              timerSetSu1=0; timerSetMo1=0; timerSetTu1=0; timerSetWe1=0;
              timerSetTh1=0; timerSetFr1=0; timerSetSa1=0;
              mistingSwitch1OnOff=false;
              SaveMistingSwitch1OnOffToEEPROM();
              SaveMisting1ToEEPROM();
              dispScreen=10;
              clearScreen();
              displayTimerScreen();
            }
      }
      
        
      if ((x>=1) && (y>=59) && (x<=318) && (y<=83)                                // Set Misting Timer 2
         &&(mistingT2OnH==0) && (mistingT2OnM==0) && (mistingT2OnS==0) 
         && (mistingT2OffH==0) && (mistingT2OffM==0) && (mistingT2OffS==0)
         || (x>=1) && (y>=59) && (x<=220) && (y<=83))
      {
        timerPage=2;
        timerHoursOn=timeHour; timerMinsOn=t.min; timerSecsOn=t.sec;
        timerHoursOff=timeHour; timerMinsOff=t.min; timerSecsOff=t.sec;
        timerAmPmOn=AM_PM; timerAmPmOff=AM_PM;
        timerSetSu=0; timerSetMo=0; timerSetTu=0; timerSetWe=0;
        timerSetTh=0; timerSetFr=0; timerSetSa=0;
        dispScreen=9;
        clearScreen();
        setTimerScreen();
      }
    if ((mistingT2OnH>0) || (mistingT2OnM>0) || (mistingT2OnS>0) || (mistingT2OffH>0) || (mistingT2OffM>0) || (mistingT2OffS>0))
      {
        if ((x>=onOffTimerButton2[0]) && (y>=onOffTimerButton2[1]) && (x<=onOffTimerButton2[2]) && (y<=onOffTimerButton2[3]))       //Pushing On/Off Button
          {
            if (mistingSwitch2OnOff==false)
              {
                mistingSwitch2OnOff=true;
              } else {
                  mistingSwitch2OnOff=false;}
            waitForIt(onOffTimerButton2[0], onOffTimerButton2[1], onOffTimerButton2[2], onOffTimerButton2[3]);
            SaveMistingSwitch2OnOffToEEPROM();
            dispScreen=10;
            clearScreen();
            displayTimerScreen();
          }
        if ((x>=resetTimerButton2[0]) && (y>=resetTimerButton2[1]) && (x<=resetTimerButton2[2]) && (y<=resetTimerButton2[3]))       //Pushing Reset Button
            {
              waitForIt(resetTimerButton2[0], resetTimerButton2[1], resetTimerButton2[2], resetTimerButton2[3]);
              mistingT2OnH=0; mistingT2OnM=0; mistingT2OnS=0;
              mistingT2OffH=0; mistingT2OffM=0; mistingT2OffS=0;
              timerAmPm2On=1; timerAmPm2Off=1;
              timerSetSu2=0; timerSetMo2=0; timerSetTu2=0; timerSetWe2=0;
              timerSetTh2=0; timerSetFr2=0; timerSetSa2=0;
              mistingSwitch2OnOff=false;
              SaveMistingSwitch2OnOffToEEPROM();
              SaveMisting2ToEEPROM();
              dispScreen=10;
              clearScreen();
              displayTimerScreen();
            }
      }
      
      
      if ((x>=1) && (y>=87) && (x<=318) && (y<=111)                                // Set Misting Timer 3
         &&(mistingT3OnH==0) && (mistingT3OnM==0) && (mistingT3OnS==0) 
         && (mistingT3OffH==0) && (mistingT3OffM==0) && (mistingT3OffS==0)
         || (x>=1) && (y>=87) && (x<=220) && (y<=111))
      {
        timerPage=3;
        timerHoursOn=timeHour; timerMinsOn=t.min; timerSecsOn=t.sec;
        timerHoursOff=timeHour; timerMinsOff=t.min; timerSecsOff=t.sec;
        timerAmPmOn=AM_PM; timerAmPmOff=AM_PM;
        timerSetSu=0; timerSetMo=0; timerSetTu=0; timerSetWe=0;
        timerSetTh=0; timerSetFr=0; timerSetSa=0;
        dispScreen=9;
        clearScreen();
        setTimerScreen();
      }
    if ((mistingT3OnH>0) || (mistingT3OnM>0) || (mistingT3OnS>0) || (mistingT3OffH>0) || (mistingT3OffM>0) || (mistingT3OffS>0))
      {
        if ((x>=onOffTimerButton3[0]) && (y>=onOffTimerButton3[1]) && (x<=onOffTimerButton3[2]) && (y<=onOffTimerButton3[3]))       //Pushing On/Off Button
          {
            if (mistingSwitch3OnOff==false)
              {
                mistingSwitch3OnOff=true;
              } else {
                  mistingSwitch3OnOff=false;}
            waitForIt(onOffTimerButton3[0], onOffTimerButton3[1], onOffTimerButton3[2], onOffTimerButton3[3]);
            SaveMistingSwitch3OnOffToEEPROM();
            dispScreen=10;
            clearScreen();
            displayTimerScreen();
          }
        if ((x>=resetTimerButton3[0]) && (y>=resetTimerButton3[1]) && (x<=resetTimerButton3[2]) && (y<=resetTimerButton3[3]))       //Pushing Reset Button
            {
              waitForIt(resetTimerButton3[0], resetTimerButton3[1], resetTimerButton3[2], resetTimerButton3[3]);
              mistingT3OnH=0; mistingT3OnM=0; mistingT3OnS=0;
              mistingT3OffH=0; mistingT3OffM=0; mistingT3OffS=0;
              timerAmPm3On=1; timerAmPm3Off=1;
              timerSetSu3=0; timerSetMo3=0; timerSetTu3=0; timerSetWe3=0;
              timerSetTh3=0; timerSetFr3=0; timerSetSa3=0;
              mistingSwitch3OnOff=false;
              SaveMistingSwitch3OnOffToEEPROM();
              SaveMisting3ToEEPROM();
              dispScreen=10;
              clearScreen();
              displayTimerScreen();
            }
      }
      
      
      if ((x>=1) && (y>=115) && (x<=318) && (y<=139)                                // Set Misting Timer 4
         &&(mistingT4OnH==0) && (mistingT4OnM==0) && (mistingT4OnS==0) 
         && (mistingT4OffH==0) && (mistingT4OffM==0) && (mistingT4OffS==0)
         || (x>=1) && (y>=115) && (x<=220) && (y<=139))
      {
        timerPage=4;
        timerHoursOn=timeHour; timerMinsOn=t.min; timerSecsOn=t.sec;
        timerHoursOff=timeHour; timerMinsOff=t.min; timerSecsOff=t.sec;
        timerAmPmOn=AM_PM; timerAmPmOff=AM_PM;
        timerSetSu=0; timerSetMo=0; timerSetTu=0; timerSetWe=0;
        timerSetTh=0; timerSetFr=0; timerSetSa=0;
        dispScreen=9;
        clearScreen();
        setTimerScreen();
      }
    if ((mistingT4OnH>0) || (mistingT4OnM>0) || (mistingT4OnS>0) || (mistingT4OffH>0) || (mistingT4OffM>0) || (mistingT4OffS>0))
      {
        if ((x>=onOffTimerButton4[0]) && (y>=onOffTimerButton4[1]) && (x<=onOffTimerButton4[2]) && (y<=onOffTimerButton4[3]))       //Pushing On/Off Button
          {
            if (mistingSwitch4OnOff==false)
              {
                mistingSwitch4OnOff=true;
              } else {
                  mistingSwitch4OnOff=false;}
            waitForIt(onOffTimerButton4[0], onOffTimerButton4[1], onOffTimerButton4[2], onOffTimerButton4[3]);
            SaveMistingSwitch4OnOffToEEPROM();
            dispScreen=10;
            clearScreen();
            displayTimerScreen();
          }
        if ((x>=resetTimerButton4[0]) && (y>=resetTimerButton4[1]) && (x<=resetTimerButton4[2]) && (y<=resetTimerButton4[3]))       //Pushing Reset Button
            {
              waitForIt(resetTimerButton4[0], resetTimerButton4[1], resetTimerButton4[2], resetTimerButton4[3]);
              mistingT4OnH=0; mistingT4OnM=0; mistingT4OnS=0;
              mistingT4OffH=0; mistingT4OffM=0; mistingT4OffS=0;
              timerAmPm4On=1; timerAmPm4Off=1;
              timerSetSu4=0; timerSetMo4=0; timerSetTu4=0; timerSetWe4=0;
              timerSetTh4=0; timerSetFr4=0; timerSetSa4=0;
              mistingSwitch4OnOff=false;
              SaveMistingSwitch4OnOffToEEPROM();
              SaveMisting4ToEEPROM();
              dispScreen=10;
              clearScreen();
              displayTimerScreen();
            }
          }
break;


i have tryed doing it like this and it doesnt seem to work....


Code:
case 10:     //--------------------- Display Set Timer SCREEN -------------------------
byte timerOnH[]={0, mistingT1OnH, mistingT2OnH, mistingT3OnH, mistingT4OnH},
     timerOnM[]={0, mistingT1OnM, mistingT2OnM, mistingT3OnM, mistingT4OnM}, 
     timerOnS[]={0, mistingT1OnS, mistingT2OnS, mistingT3OnS, mistingT4OnS},
     timerOffH[]={0, mistingT1OffH, mistingT2OffH, mistingT3OffH, mistingT4OffH},
     timerOffM[]={0, mistingT1OffM, mistingT2OffM, mistingT3OffM, mistingT4OffM},
     timerOffS[]={0, mistingT1OffS, mistingT2OffS, mistingT3OffS, mistingT4OffS};
byte timerAmPmOnx[]={0, timerAmPm1On, timerAmPm2On, timerAmPm3On, timerAmPm4On},
     timerAmPmOffx[]={0, timerAmPm1Off, timerAmPm2Off, timerAmPm3Off, timerAmPm4Off};
byte timerSetSux[]={0, timerSetSu1, timerSetSu2, timerSetSu3, timerSetSu4},
     timerSetMox[]={0, timerSetMo1, timerSetMo2, timerSetMo3, timerSetMo4},
     timerSetTux[]={0, timerSetTu1, timerSetTu2, timerSetTu3, timerSetTu4},
     timerSetWex[]={0, timerSetWe1, timerSetWe2, timerSetWe3, timerSetWe4},
     timerSetThx[]={0, timerSetTh1, timerSetTh2, timerSetTh3, timerSetTh4},
     timerSetFrx[]={0, timerSetFr1, timerSetFr2, timerSetFr3, timerSetFr4},
     timerSetSax[]={0, timerSetSa1, timerSetSa2, timerSetSa3, timerSetSa4};
boolean SwitchOnOff[]={0, mistingSwitch1OnOff, mistingSwitch2OnOff, mistingSwitch3OnOff, mistingSwitch4OnOff};

    if (offMisting2RH>0)
      {
        // do nothing
      } else {
        
    for (byte xx= 1; xx< 5; xx++)
       {
         if ((x>=1) && (y>=4 + xx * 28) && (x<=318) && (y<=27 + xx * 28)                                // Set Misting Timer 1
             && (timerOnH[xx]==0) && (timerOnM[xx]==0) && (timerOnS[xx]==0) 
             && (timerOffH[xx]==0) && (timerOffM[xx]==0) && (timerOffS[xx]==0)
             || (x>=1) && (y>=4 + xx * 28) && (x<=220) && (y<=27 + xx * 28))
          {
            timerPage=xx;
            timerHoursOn=timeHour; timerMinsOn=t.min; timerSecsOn=t.sec;
            timerHoursOff=timeHour; timerMinsOff=t.min; timerSecsOff=t.sec;
            timerAmPmOn=AM_PM; timerAmPmOff=AM_PM;
            timerSetSu=0; timerSetMo=0; timerSetTu=0; timerSetWe=0;
            timerSetTh=0; timerSetFr=0; timerSetSa=0;
            dispScreen=9;
            clearScreen();
            setTimerScreen();
          }
        if ((timerOnH[xx]>0) || (timerOnM[xx]>0) || (timerOnS[xx]>0) || (timerOffH[xx]>0) || (timerOffM[xx]>0) || (timerOffS[xx]>0))
          {
            if ((x>=onOffTimerButton1[0]) && (y>=5 + xx * 28) && (x<=onOffTimerButton1[2]) && (y<=25 + xx * 28))       //Pushing On/Off Button
              {
                if (SwitchOnOff[xx]==false)
                  {
                    SwitchOnOff[xx]=true;
                  } else {
                      SwitchOnOff[xx]=false;}
      
                waitForIt(onOffTimerButton1[0], (5 + xx * 28), onOffTimerButton1[2], (25 + xx * 28));
                if (xx=1){
                  SaveMistingSwitch1OnOffToEEPROM();}
                if (xx=2){
                  SaveMistingSwitch2OnOffToEEPROM();}
                if (xx=3){
                  SaveMistingSwitch3OnOffToEEPROM();}
                if (xx=4){
                  SaveMistingSwitch4OnOffToEEPROM();}
                dispScreen=10;
                clearScreen();
                displayTimerScreen();
              }
            if ((x>=resetTimerButton1[0]) && (y>=5 + xx * 28) && (x<=resetTimerButton1[2]) && (y<=25 + xx * 28))       //Pushing Reset Button
                {
                  waitForIt(resetTimerButton1[0], (y>=5 + xx * 28), resetTimerButton1[2], (25 + xx * 28));
                  timerOnH[xx]=0; timerOnM[xx]=0; timerOnS[xx]=0;
                  timerOffH[xx]=0; timerOffM[xx]=0; timerOffS[xx]=0;
                  timerAmPmOnx[xx]=1; timerAmPmOffx[xx]=1;
                  timerSetSux[xx]=0; timerSetMox[xx]=0; timerSetTux[xx]=0; timerSetWex[xx]=0;
                  timerSetThx[xx]=0; timerSetFrx[xx]=0; timerSetSax[xx]=0;
                  SwitchOnOff[xx]=false;
                if (xx=1){
                  SaveMistingSwitch1OnOffToEEPROM();
                  SaveMisting1ToEEPROM();}
                if (xx=2){
                  SaveMistingSwitch2OnOffToEEPROM();
                  SaveMisting2ToEEPROM();}
                if (xx=3){
                  SaveMistingSwitch3OnOffToEEPROM();
                  SaveMisting3ToEEPROM();}
                if (xx=4){
                  SaveMistingSwitch4OnOffToEEPROM();
                  SaveMisting4ToEEPROM();}
                  dispScreen=10;
                  clearScreen();
                  displayTimerScreen();
                }
            }
       }
break;



any one with arduino knowledge that can help would be great...
 
See less See more
#4 ·
I'm an electrical engineer, not a software engineer, so I tend to lean more towards repetitive simple code over using something more advanced, because I'm unlikely to remember how the more advanced code works incase I need to change it....

I'm having a hard time understanding what exactly this code does? Set time for dosing? Or lights to come on? Does it mostly have to do with the user storing the timers, or just displaying the timers that are already stored?
 
#6 ·
what this code does is its a touch portion for the 3.2" tft for a timer...


here is what the display looks like



if "set misting timer 1-4" from the photo above is touch it will take you to another screen where you can set the time and date for the timer...

the portion of the code where it says on/off... this is when a time is set it will display the on/off button & reset button to be touch... if time was not set, it will display a "timer # has not been scheduled"

the misting that i have code this for is to make rain drops in a aquarium part land, part water setup...

i hope this clears some things up...
 
#7 ·
Ah, You just trying to organize the mess. You're on the right track but I would further organize. Anything with 'variableName'+number should go by using arrays, structs, and or classes. Let's use arrays and structs. I'm doing this off the top off my head so it'll need cleaning and error checking.

Code:
struct MistingButton
{
    byte hourOn;
    byte minuteOn;
    byte secondOn;
    byte hourOff;
    byte minuteOff;
    byte secondOff;
    byte day;
    boolean switch;
}

struct button MistingButton[3];

button[0].hourOn = 3;
button[0].hourOff = 5;
button[0].switch = false;
.
.
etc...
button[1].hourOn = 5;
button[1].hourOff = 6;
.
.
etc
// loop through buttons
for (byte i = 0; i< 4; i++)
{
     if ((x>=1) && (y >=  (i * 28+ 31)) && (x <= 318) && (y <= (27 + i * 28 +31))
     {
         if(button[i].hourOn == 0 && button[i].minuteOn == 0 && button[i].secondOn == 0) 
          {
               //do your thing
           }
         // etc.... more code
      }

     // etc.... more code
}
 
#8 ·
wouldn't this be the same as what i had tryed at the top with arrays? Also the "for loop" repeats it self but then stops once the statment is false. Also this is a touch part of the sketch so won't the "i" variable need to stay the same for it to read correctly when touching to set the timer 1-4?
 
#10 ·
it doesnt read more then what i needed too but i dont think there is a way anymore as its a touch portion of the utft and if any loop was add to where it needs to be touch it wont work... it'll just over lap it self...


say u do a for loop for a, b, c, and d...

with the for loop it will end at d and when trying to push a... nothing will happen as it ended at d, if that makes any sense...
 
#11 ·
In a for loop, there is no time delay. So when you press the screen, it will check a,b,c,d against that event. You have to put the logic in the void loop() function so it's continuously checking for the event.

Your proposed event would/should never happen.


Sent from my iPad using Tapatalk HD
 
#13 ·
It shouldn't stop. Like I said, your logic that needs an event like a press on the screen needs to be in the void loop. The for loop needs to be in the called constantly until it's no longer needed like going to another screen/page.

Take a look at other projects for examples.

Sent from my iPad using Tapatalk HD
 
#14 ·
Ok I get what ur saying... Instead of being in a void loop... It can be in a while loop also when will always loop over n over but then again the screen will slow down in mins as it just over laps what was already there and build up... So I don't think it's a good ideas to put it into a loop in that way ... Correct me if I am wrong here
 
#15 ·
Check out the iAqua project. It's well organized.

I haven't seen your whole code so I don't know but a while loop is basically a for loop configured differently. Any event logic needs to be in the void loop or called from the void loop. For loops & while & do while etc.. are iteration loops not time based loops.
 
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top