PDE - change PWM settings from controller

Share you PDE file with our community
Post Reply
serhiyi
Posts: 50
Joined: Thu Jul 21, 2011 8:59 pm

PDE - change PWM settings from controller

Post by serhiyi »

This file is based on http://forum.reefangel.com/viewtopic.php?f=11&t=222

Tested on RA Controller with WiFi and one relay box.

Here is my screen
RA_Scrn.png
RA_Scrn.png (411.54 KiB) Viewed 7639 times
Relay box is set:
1 - ATO Standard
2 - Fans on my LED (will set to always On at first)
3 - LED Daylight (using MH setting in RA Client)
4 - LED Actinic (using Standard light settings in RA Client)
5 - Wave 1
6 - Wave 2
7 - Return Pump (always On)
8 - Skimmer (always On)

I was able to set it up so you can change On/Off time for lights. MH settings are used for Daylight LED, Standard light settings are used for Actinic LED. This way I can change settings using RA Client without recompiling and uploading code.

PWM functions are set to ramp up and down from 0% to the value you set up in controller using RA Client.
The idea is to load code once and be able to change settings from client if needed.
RA_Client.png
RA_Client.png (120.77 KiB) Viewed 7639 times
You can enable graph on screen if you like (just un-comment it and set correct location). Or you can add more relay boxes to display if you have more then one.

I;m still looking at option to show last error on display, but not sure how to do it yet. Will post update when I have it.

Here is my code.

Code: Select all

// RAMenus.pde
//
// This version designed for v0.8.5 Beta 10 and later

#include <ReefAngel_Features.h>
#include <ReefAngel_Globals.h>
#include <ReefAngel_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <ReefAngel_EEPROM.h>
#include <ReefAngel_NokiaLCD.h>
#include <ReefAngel_ATO.h>
#include <ReefAngel_Joystick.h>
#include <ReefAngel_LED.h>
#include <ReefAngel_TempSensor.h>
#include <ReefAngel_Relay.h>
#include <ReefAngel_PWM.h>
#include <ReefAngel_Timer.h>
#include <ReefAngel_Memory.h>
#include <ReefAngel.h>

void ConvertNumToString(char* string, int num, byte decimal)
{
   char temptxt[3];
   int Temp = num;
   if (Temp==0xFFFF) Temp=0;
      itoa(Temp/decimal,string,10);
   if (decimal>1)
   {
      itoa(Temp%decimal,temptxt,10);
      strcat(string, ".");
      if (Temp%decimal<10 && decimal==100) strcat(string, "0");
         strcat(string, temptxt);
   }
}

void DrawCustomMain()
{
   byte x = 6;
   byte y = 2;
   byte t;
   char text[7];


   ReefAngel.LCD.DrawDate(6, 2);
   ReefAngel.LCD.Clear(COLOR_BLACK, 1, 11, 132, 11);
   pingSerial();

   x = 4;
   y += 12;
   ReefAngel.LCD.DrawText(COLOR_BLACK, DefaultBGColor, x, y, "Temperature:");

   y += 10;
   ConvertNumToString(text, ReefAngel.Params.Temp1, 10);
   pingSerial();
   ReefAngel.LCD.DrawHugeNumbers(T1TempColor, DefaultBGColor, x, y, text);
   x = 76;
   ReefAngel.LCD.DrawText(T2TempColor,DefaultBGColor,x,y,"LED");
   ReefAngel.LCD.DrawSingleMonitor(ReefAngel.Params.Temp2, T2TempColor, x+28, y, 10);
   ReefAngel.LCD.DrawText(T3TempColor,DefaultBGColor,x,y+9,"ROOM");
   ReefAngel.LCD.DrawSingleMonitor(ReefAngel.Params.Temp3, T3TempColor, x+28, y+9, 10);
   pingSerial();

   ReefAngel.LCD.Clear(COLOR_BLACK, 1, 42, 132, 42);

   x = 4;
   y = 45;
   ReefAngel.LCD.DrawText(PHColor,DefaultBGColor,x,y,"pH");
   ReefAngel.LCD.DrawSingleMonitor(ReefAngel.Params.PH, PHColor, x+28, y, 100);
   y = 55;
//   ReefAngel.LCD.DrawText(COLOR_BLACK,DefaultBGColor,x,y,"ATO");
//   atoH = "Off";
//   if (ReefAngel.HighATO.IsActive()) atoH = "On";
//   atoL = "Off";
//   if (ReefAngel.LowATO.IsActive()) atoL = "On";
//   ReefAngel.LCD.DrawText(COLOR_BLACK,DefaultBGColor,x+28,y,"Hi "+atoH+" Low "+atoL);



   x = 76;
   y = 45;
   ReefAngel.LCD.DrawText(DPColor,DefaultBGColor,x,y,"DayL");
   x=104;
   ReefAngel.LCD.DrawSingleMonitor(ReefAngel.PWM.GetDaylightValue(),DPColor, x, y, 1);
   t = intlength(ReefAngel.PWM.GetDaylightValue()) + 1;
   t *= 5;
   ReefAngel.LCD.DrawText(DPColor, DefaultBGColor, x+t, y, "%");

   x = 76;
   y = 55;
   ReefAngel.LCD.DrawText(APColor,DefaultBGColor,x,y,"ActL");
   x=104;
   ReefAngel.LCD.DrawSingleMonitor(ReefAngel.PWM.GetActinicValue(),APColor, x, y, 1);
   t = intlength(ReefAngel.PWM.GetActinicValue()) + 1;
   t *= 5;
   ReefAngel.LCD.DrawText(APColor, DefaultBGColor, x+t, y, "%");

   pingSerial();


   x = 12;
   y = 118;
   byte TempRelay = ReefAngel.Relay.RelayData;
   TempRelay &= ReefAngel.Relay.RelayMaskOff;
   TempRelay |= ReefAngel.Relay.RelayMaskOn;
   ReefAngel.LCD.DrawOutletBox(x, y, TempRelay);
}

void DrawCustomGraph()
{
// ReefAngel.LCD.DrawGraph(5,50);
}


void setup()
{
    ReefAngel.Init(); //Initialize controller
    ReefAngel.SetTemperatureUnit(0);
    ReefAngel.PWM.SetActinic(0);
    ReefAngel.PWM.SetDaylight(0);

    // Set the ports that get toggled on & off during the following modes
    // To enable a port to be toggled, place a 1 in the appropriate position
    // Uncomment and update as needed
    // Port 87654321
    ReefAngel.FeedingModePorts = B11110000;
    ReefAngel.WaterChangePorts = B11110000;
    ReefAngel.OverheatShutoffPorts = B01011100;
    ReefAngel.LightsOnPorts = B00001110;

    ReefAngel.Relay.On(Port7); // Return Pump
    ReefAngel.Relay.On(Port8); // Skimmer
}

void loop()
{
    ReefAngel.ShowInterface();

    // Specific functions

    ReefAngel.StandardATO(Port1);
    ReefAngel.StandardFan(Port2);
    ReefAngel.MHLights(Port3);        // For LED Daylight
    ReefAngel.StandardLights(Port4);  // For LED Actinic
    ReefAngel.Wavemaker1(Port5);
    ReefAngel.Wavemaker1(Port6);

    ReefAngel.PWM.SetActinic(PWMSlope(InternalMemory.StdLightsOnHour_read(),InternalMemory.StdLightsOnMinute_read(),InternalMemory.StdLightsOffHour_read(),InternalMemory.StdLightsOffMinute_read(),0,InternalMemory.LEDPWMActinic_read(),90,ReefAngel.PWM.GetActinicValue()));
    ReefAngel.PWM.SetDaylight(PWMSlope(InternalMemory.MHOnHour_read(),InternalMemory.MHOnMinute_read(),InternalMemory.MHOffHour_read(),InternalMemory.MHOffMinute_read(),0,InternalMemory.LEDPWMDaylight_read(),60,ReefAngel.PWM.GetDaylightValue()));
}
This is ReefAngel_Features.h that was used to compile it:

Code: Select all

#ifndef __REEFANGEL_FEATURES_H__
#define __REEFANGEL_FEATURES_H__

#define SIMPLE_MENU
#define DisplayLEDPWM
#define wifi

#define FONT_8x16
#define NUMBERS_16x16
#define CUSTOM_MAIN


#endif  // __REEFANGEL_FEATURES_H__

Use it as it is, or modify, or just play with it to learn. It took me some time to figure it out and changing existing code helps understand how this works.

Please let me know if you see what can be improved in this code or if you know how to add error information on the screen. This can help people if they see red LED on their unit.

Thank.
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: PDE - change PWM settings from controller

Post by binder »

To save some code space, I would remove the line:

Code: Select all

#define FONT_8x16
It does not look like you are using that font. So removing it would cut down on space.

As for the errors pertaining to the red led, there is a way to do it. You have to modify the .16 libraries BUT it will be inside .17. So if you can hold off until .17, you will be able to do this.

curt
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PDE - change PWM settings from controller

Post by rimai »

Nice screen.
What I have been working with brimarq is overriding PWM % from Client by using "Mask On" on a relay box that he does not use.
So, for example, on Client, he enabled box 3 and 4.
On box 3, we set the following:
relay 1 - Whites 25%
relay 2 - Whites 50%
relay 3 - Whites 75%
relay 4 - Whites 100%
relay 5 - Blues 25%
relay 6 - Blues 50%
relay 7 - Blues 75%
relay 8 - Blues 100%
On box 4, we set the following:
relay 1 - Lightining

So, whenever he masks the relay on those boxes, the controller cleverly knows what to do :)
That's the work around I came up so far, until we get more goodies from Curt and Dave :)
Roberto.
dmolton
Posts: 182
Joined: Tue Mar 22, 2011 11:08 am

Re: PDE - change PWM settings from controller

Post by dmolton »

rimai wrote:Nice screen.
What I have been working with brimarq is overriding PWM % from Client by using "Mask On" on a relay box that he does not use.
So, for example, on Client, he enabled box 3 and 4.
On box 3, we set the following:
relay 1 - Whites 25%
relay 2 - Whites 50%
relay 3 - Whites 75%
relay 4 - Whites 100%
relay 5 - Blues 25%
relay 6 - Blues 50%
relay 7 - Blues 75%
relay 8 - Blues 100%
On box 4, we set the following:
relay 1 - Lightining

So, whenever he masks the relay on those boxes, the controller cleverly knows what to do :)
That's the work around I came up so far, until we get more goodies from Curt and Dave :)
That's pretty clever, Roberto. I like that idea a lot. There's a lot of possibilities with that method considering there's 8 expansion boxes.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PDE - change PWM settings from controller

Post by rimai »

Yeah :)
Same concept would work for internal memory too.
Assign a known value to memory block and when value changes, the controller can do something.
In this example, assign 0 to internal memory, if values changes to 25, the controller would override the PWM channel to 25%. When the internal memory value goes back to 0, it returns to auto mode.
It's just that using the Client's extra boxes as virtual switches is so much easier :D
Roberto.
serhiyi
Posts: 50
Joined: Thu Jul 21, 2011 8:59 pm

Re: PDE - change PWM settings from controller

Post by serhiyi »

looks like my code does not work as I expected.
I was hopping to be able to change pwm by simply changing time when lights come on and off in the client.
but as far I can tell, pwm function used those values at the time I uploaded code and do not change as I change lights timer.

for example, at the time I loaded code, timer was 10am to 10pm with gradual change.
later I used client to change timer to 10am to 2pm. I expected pwm to be at 0% at 3 pm but it was not. Look like pwm was still using original time 10pm.

just letting everyone know...
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PDE - change PWM settings from controller

Post by rimai »

I'll test things out tomorrow.
I liked the way you did it :)
I'm using the same PWM code. I must admit. :)
But I have not changed the time through client yet.
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PDE - change PWM settings from controller

Post by rimai »

Mine function is working just fine.
The only thing that I changed was I placed an initialization number when the controller is rebooted.
Here is the code:

Code: Select all

#include <ReefAngel_Features.h>
#include <ReefAngel_Globals.h>
#include <ReefAngel_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <ReefAngel_EEPROM.h>
#include <ReefAngel_NokiaLCD.h>
#include <ReefAngel_ATO.h>
#include <ReefAngel_Joystick.h>
#include <ReefAngel_LED.h>
#include <ReefAngel_TempSensor.h>
#include <ReefAngel_Relay.h>
#include <ReefAngel_PWM.h>
#include <ReefAngel_Timer.h>
#include <ReefAngel_Memory.h>
#include <ReefAngel.h>

//Expander I/O bits 
#define AlkDoser            8
#define CADoser             7
#define Filter              6
#define LEDFan              5
#define WM                  4
#define Heater              3
#define Daylight            2
#define Sump                1

void setup()
{
    ReefAngel.Init();  //Initialize controller
    ReefAngel.PWM.SetDaylight(0);
    ReefAngel.FeedingModePorts = B00000001;
    ReefAngel.WaterChangePorts = B00101001;
    ReefAngel.OverheatShutoffPorts = B00000000;
    ReefAngel.LightsOnPorts = B00010010;

    // Ports that are always on
    ReefAngel.Relay.On(Sump);
    ReefAngel.Relay.On(WM);
    ReefAngel.Relay.On(Filter);
}

void loop()
{
    // Specific functions
    ReefAngel.StandardHeater(Heater);
    ReefAngel.StandardLights(Daylight);
    ReefAngel.StandardLights(LEDFan,InternalMemory.StdLightsOnHour_read(),InternalMemory.StdLightsOnMinute_read()-10,InternalMemory.StdLightsOffHour_read(),InternalMemory.StdLightsOffMinute_read()+10);
    ReefAngel.PWM.SetDaylight(PWMSlope(InternalMemory.StdLightsOnHour_read(),InternalMemory.StdLightsOnMinute_read(),InternalMemory.StdLightsOffHour_read(),InternalMemory.StdLightsOffMinute_read(),30,InternalMemory.LEDPWMDaylight_read(),60,ReefAngel.PWM.GetDaylightValue()));
    ReefAngel.DosingPump1(CADoser);
    ReefAngel.DosingPump2(AlkDoser);
    ReefAngel.ShowInterface();
}
Roberto.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PDE - change PWM settings from controller

Post by rimai »

One thing I'd like to point out too is that the function will start working only on the next cycle.
Roberto.
Post Reply