hedrickms code

Share you PDE file with our community
User avatar
Posts: 5343
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Fri Sep 19, 2014 3:35 am
Sweet! Very nice.
User avatar
Posts: 2732
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Fri Sep 19, 2014 8:13 am
yeah, what lee said....looks good. nice work. :)

Posts: 411
Joined: Sat Mar 30, 2013 5:02 pm
Location: CT, USA
PostPosted: Fri Sep 19, 2014 8:26 am
I like this! This should be a library, no ?

Posts: 32
Joined: Tue Jul 29, 2014 7:36 pm
PostPosted: Sat Sep 20, 2014 7:33 am
Does anyone know why the random(7) function would always select #5 when the controller reboots? I am not sure if it is always doing this yet (cause I rebooted everyday) but my pumps are not switching modes during the day.

Ideas?

code below:

Code: Select all
void JebaoMode()
{
     static boolean feedingalert = false;
     static boolean pumpstormalert = false;
    static unsigned long isfeeding;
    static int ranMode = random (9);
     static WiFiAlert pumpAlert;

    if (ReefAngel.DCPump.Mode == Custom)
    {
       if (ReefAngel.DisplayedMenu==FEEDING_MODE)
       {
          isfeeding = now();
          feedingalert = false; //reset feeding flag so alert can be sent if needed
       }
       if (now()-isfeeding<5400) // Continue for the 90 minutes
       {
         if (!feedingalert)
         {
            pumpAlert.Send("Pump+Mode+NTM+Feeding!+Activated.");
           feedingalert = true;
         }
           ReefAngel.PWM.SetDaylight( NutrientTransportMode(30,70,140,true));
           ReefAngel.PWM.SetActinic( NutrientTransportMode(30,70,140,false));
       }
       else if ((now()%SECS_PER_DAY >= 82800) || (now()%SECS_PER_DAY < 28800)) // night mode 11pm -> 8am
       {
         if (now()%SECS_PER_DAY == 82800) pumpstormalert = false; //reset pump flag so alert can be sent if needed
         if (!pumpstormalert)
         {
           pumpAlert.Send("Pump+Mode+Sine+NightMode!+Activated.");
           pumpstormalert = true;
         }
         ReefAngel.PWM.SetDaylight(SineMode(30,50,20, true));
           ReefAngel.PWM.SetActinic(SineMode(30,50,20, false));
       }// end night mode 8am   
       else   //  choose a random pump mode (ReefCrest, TidalSwell, ShortPulse, LongPulse, Else) between 0 and 8
       {
        if (now()%SECS_PER_DAY == 28800)//at 8am set pump mode and determine if storm
        {
          pumpstormalert = false; //reset pump flag so alert can be sent if needed
          //set random pump mode at start of each day (midnight) and determine if storm will occur
          ranMode = random(30);
         if (ranMode!=29 || ranMode!=17) ranMode = random (9);//if not storm set normal random pump mode
        }
        if (ranMode==29 || ranMode==17)//storm mode
         {
           if (!pumpstormalert)
            {
              //enter storm mode for pumps (random)
              pumpAlert.Send("Pump+Mode+STORM!+Activated.");
              pumpstormalert = true; //reset pump flag so alert can be sent if needed
            } 
           StormMode();
         }
         else
         {
              switch (ranMode)
            {
            case 0:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+ShortPule+case0!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              } 
              ReefAngel.PWM.SetActinic( ShortPulseMode(0,60,400,true) );
              ReefAngel.PWM.SetDaylight( ShortPulseMode(0,60,400,false) );
              break;
             case 1:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+ReefCrest+case1!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              } 
              ReefAngel.PWM.SetDaylight( ReefCrestMode(50,20,true) );
              ReefAngel.PWM.SetActinic( ReefCrestMode(50,20,false) );
              break;
             case 2:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+TidalSwell+case2!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              } 
              ReefAngel.PWM.SetDaylight( TidalSwellMode(70,true) );
              ReefAngel.PWM.SetActinic( TidalSwellMode(70,false) );
              break;
             case 3:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+TidalSwell+case3!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              }           
              ReefAngel.PWM.SetDaylight( TidalSwellMode(50,true) );
              ReefAngel.PWM.SetActinic( TidalSwellMode(50,false) );
              break;
             case 4:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+TideMode+case4!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              }
              ReefAngel.PWM.SetDaylight( TideMode(50,30,70) );
              ReefAngel.PWM.SetActinic( TideMode(50,30,70) );
              break;
             case 5:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+LongPulse+case5!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              }
              ReefAngel.PWM.SetDaylight( LongPulseMode(20,60,10,true) );
              ReefAngel.PWM.SetActinic( LongPulseMode(20,60,10,false) );
              break;
             case 6:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+ElseMode+case6!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              }
              ReefAngel.PWM.SetDaylight( ElseMode(50,30,true) );
              ReefAngel.PWM.SetActinic( ElseMode(50,30,false) );
              break;
             case 7:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+SineWave+case7!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              }
              ReefAngel.PWM.SetDaylight(SineMode(40,70,20, true));
              ReefAngel.PWM.SetActinic(SineMode(40,70,20, false));
              break;
             case 8:
              if (!pumpstormalert)
              {
               //enter storm mode for pumps (random)
               pumpAlert.Send("Pump+Mode+ShortPule+case8!+Activated.");
               pumpstormalert = true; //reset pump flag so alert can be sent if needed
              }
              ReefAngel.PWM.SetActinic( ShortPulseMode(10,50,600,true) );
              ReefAngel.PWM.SetDaylight( ShortPulseMode(10,50,600,false) ); 
              break;
           }//end of random pump mode
       }//end else switch
       }//end else pump mode
    } //end of DCPump custom

} // End Void JebaoRandomMode()
User avatar
Posts: 5343
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Sat Sep 20, 2014 8:04 am
Because random() isn't seed'ed by default. So if you were troubleshooting something, you'd always have the same stream of random numbers. You will need to seed it with something that would also change, like the current time.

This is what I have:

randomSeed(now()/SECS_PER_DAY);

Posts: 32
Joined: Tue Jul 29, 2014 7:36 pm
PostPosted: Sat Sep 20, 2014 9:16 am
Ahh... got it. Thanks a bunch. Do you seed under Setup ()?
User avatar
Posts: 5343
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Sat Sep 20, 2014 9:42 am
Yes. My INO is in my signature

Posts: 32
Joined: Tue Jul 29, 2014 7:36 pm
PostPosted: Sat Sep 20, 2014 11:04 am
Awesome... Thanks again!

I should have checked there first (my apologies). Sometimes I get a little trigger happy :lol:

Posts: 32
Joined: Tue Jul 29, 2014 7:36 pm
PostPosted: Sun Sep 21, 2014 12:55 pm
ok code updated on first post to include fixes to random mode (thanks Lee!). Everything seems to be working.
User avatar
Posts: 5343
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Sun Sep 21, 2014 1:17 pm
Awesome! Now comes the incessant tweaking and feature adding. :)
PreviousNext

Return to My PDE/INO file

Who is online

Users browsing this forum: No registered users and 2 guests