aquarium light controller wannabe

Dependencies:   DS3231 FastPWM Menu QEI TextLCD _24LCXXX mbed

Files at this revision

API Documentation at this revision

Comitter:
rakware
Date:
Sun Mar 22 17:28:46 2015 +0000
Parent:
0:0b9548db2291
Child:
2:c50dfa998b4c
Commit message:
stuff

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Mar 05 17:40:21 2015 +0000
+++ b/main.cpp	Sun Mar 22 17:28:46 2015 +0000
@@ -55,29 +55,30 @@
 //    { {0, 100} }
 //};
 
+
 uint8_t Pwm_Min_Max_Array[sizeof(Pwm_Array)/sizeof(FastPWM)][2] = {
-    {0, 100},
-    {20, 80},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100},
-    {0, 100}
+    {0, 1},
+    {0, 5},
+    {0, 5},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35},
+    {0, 35}
 };
 
 int pwm_idx = 0;
 
-uint8_t SunRiseSet[2][2] = {{0,1},{21,9}};
+uint8_t SunRiseSet[2][2] = {{11,30},{21,30}};
 
-uint8_t ramptime = 1; //in minutes
+uint8_t ramptime = 60; //in minutes
 
 bool refresh_display = true;
 
@@ -370,14 +371,14 @@
 {
     for(uint8_t index = 0; index < sizeof(Pwm_Array)/sizeof(FastPWM); index++) { //index < sizeof(Pwm_Array)/sizeof(FastPWM)
 
-        double ramp = double( (Pwm_Min_Max_Array[index][1]-Pwm_Min_Max_Array[index][0]) ) / double( (( ((SunRiseSet[0][0]*3600) + (SunRiseSet[0][1]*60))+(ramptime*60) )-( (SunRiseSet[0][0]*3600) + (SunRiseSet[0][1]*60) )) ) ;
+        double ramp = double( (Pwm_Min_Max_Array[index][1]-Pwm_Min_Max_Array[index][0]) ) / double( (( ((SunRiseSet[1][0]*3600) + (SunRiseSet[1][1]*60))+(ramptime*60) )-( (SunRiseSet[1][0]*3600) + (SunRiseSet[1][1]*60) )) ) ;
 
-        if (Pwm_Array[index].read() < Pwm_Min_Max_Array[index][1]/100.0) {
-            Pwm_Array[index] = Pwm_Array[index].read() + double(ramp/100.0);
+        if (Pwm_Array[index].read() > Pwm_Min_Max_Array[index][0]/100.0) {
+            Pwm_Array[index] = Pwm_Array[index].read() - double(ramp/100.0);
             pc.printf("sunset:[%i]%f ", index, Pwm_Array[index].read());
         }
-        if (Pwm_Array[index].read() > Pwm_Min_Max_Array[index][1]/100.0) {
-            Pwm_Array[index] = Pwm_Min_Max_Array[index][1]/100.0;
+        if (Pwm_Array[index].read() < Pwm_Min_Max_Array[index][0]/100.0) {
+            Pwm_Array[index] = Pwm_Min_Max_Array[index][0]/100.0;
             Sunset_Ticker.detach();
             pc.printf("\n sunset end:[%i]%f \n", index, Pwm_Array[index].read());
         }
@@ -403,7 +404,7 @@
     lcd.cls();
     lcd.printf("rtc: %i / %02i-%02i-%02i %02i:%02i:%02i",rtc_dow,rtc_mday,rtc_mon,rtc_year,rtc_hour,rtc_min,rtc_sec);
 
-    
+
     t.tm_sec = rtc_sec;    // 0-59
     t.tm_min = rtc_min;    // 0-59
     t.tm_hour = rtc_hour;   // 0-23
@@ -439,7 +440,11 @@
 
     //TODO
     //rtc init (set 32KHz output to enable, erase OSF, etc?
-    
+
+
+    char buffer[32];
+    struct tm *twhile;
+
     set_time_from_rtc(); //read time from DS3231, set time to STM32
     //read external EEPROM and restore saved values (what values?)
     //lcd.cls();
@@ -448,6 +453,27 @@
 
     //TODO set initial pwm_channel state (ie: if it's betweeen sunrise_end and sunset_start set it to sunrise_max, if it's ramping calculate the initial value
 
+    //initial state = pwm_min
+    for(uint8_t index = 0; index < sizeof(Pwm_Array)/sizeof(FastPWM); index++) {
+        Pwm_Array[index].period_ms(2);
+        Pwm_Array[index] = Pwm_Min_Max_Array[index][0]/100.0;
+    }
+
+    //see if we are between sunrise and sunset
+    time_t seconds = time(NULL);
+    twhile = localtime(&seconds);
+
+    int timerica = (twhile->tm_hour*3600) + (twhile->tm_min*60) + twhile->tm_sec;
+
+    if ((timerica > (SunRiseSet[0][0]*3600) + (SunRiseSet[0][1]*60)) && (timerica < (SunRiseSet[1][0]*3600) + (SunRiseSet[1][1]*60))) {
+        for(uint8_t index = 0; index < sizeof(Pwm_Array)/sizeof(FastPWM); index++) {
+            Pwm_Array[index] = Pwm_Min_Max_Array[index][1]/100.0; //add smoothing
+            pc.printf("pwm:[%i]%f ", index, Pwm_Array[index].read());
+        }
+    }
+
+
+
     qei_idx.mode(PullUp);
     qei_t.attach(&qei_cb, 0.05); //calls qei_cb every 50ms
 
@@ -496,10 +522,6 @@
 
     activeMenu = &settingsMenu;
 
-
-    char buffer[32];
-    struct tm *twhile;
-
     while(1) {
 
         time_t seconds = time(NULL);
@@ -511,14 +533,15 @@
         int timerica = (twhile->tm_hour*3600) + (twhile->tm_min*60) + twhile->tm_sec;
 
 
-        pc.printf("%s %d %d\n", buffer, timerica, (SunRiseSet[0][0]*3600) + (SunRiseSet[0][1]*60) );
+        pc.printf("time: %s in seconds: %d sunrise: %d sunset: %d\n", buffer, timerica, (SunRiseSet[0][0]*3600) + (SunRiseSet[0][1]*60), (SunRiseSet[1][0]*3600) + (SunRiseSet[1][1]*60) );
 
         if (timerica == (SunRiseSet[0][0]*3600) + (SunRiseSet[0][1]*60)) { //TODO attach once at start out of while()
             Sunrise_Ticker.attach(&incs,1); //TODO check if allready attached
         }
-        
+
         if (timerica == (SunRiseSet[1][0]*3600) + (SunRiseSet[1][1]*60)) {
             Sunset_Ticker.attach(&decs,1); //TODO check if allready attached
+            pc.printf("sunset timer attached to decs\n");
         }
 
 //        for(uint8_t index = 0; index < 3; index++) { //index < sizeof(Pwm_Array)/sizeof(FastPWM)
@@ -530,7 +553,8 @@
             switch(menu_state) {
                 case MAIN:
                     lcd.cls();
-                    lcd.printf("MAIN");
+                    lcd.printf("     BAT-LED    ");
+                    lcd.printf("     v.1.0      ");
                     refresh_display = false;
                     break;
                 case SETTINGS: