Snippets

Peter Scargill Experimental NANO supply code

Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
-// see blog entry at http://tech.scargill.net/uninterruptable-supplies/
+// see blog entry at http://tech.scargill.net/gpio-the-hard-way/
+// now superceded by OLED version
+
 #define LIGHT 12
 #define RELAY 11
 #define WARNING 10
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 #define WAITING 60
 #define ONDELAY 20
 
+#define SWITCHON 3.7
+#define SWITCHOFF 3.1
+
 // Below for lcd
 
 /*
 uint16_t lightOnSet = 2;
 uint16_t lightOffSet = 1022;
 
-float offVoltage = 3.0;
-float onVoltage = 3.6;
+float offVoltage = SWITCHOFF;
+float onVoltage = SWITCHON;
 
 float average=0;
 
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 #define RELAY 11
 #define WARNING 10
 
+#define WAITING 60
+#define ONDELAY 20
+
 // Below for lcd
 
 /*
 
 // end of lcd stuff
 
-#define WAITING 30
-#define ONDELAY 20
 #include <stdarg.h>
 
 uint32_t mymillis = 0;
 uint16_t lightOffSet = 1022;
 
 float offVoltage = 3.0;
-float onVoltage = 3.4;
+float onVoltage = 3.6;
 
 float average=0;
 
       {
         if (onDelay == 0)
         {
-          onDelay = ONDELAY;
+          onDelay = ONDELAY*2;
           theState = "starting";
         }
       }
     {  
       if (vol < offVoltage)
       {
-        state = WAITING + 2;
+        state = (WAITING*2) + 2;
         theState = "warning ";
       }
     }
-    if (state > 2)
+    else if (state > 2)
     {
       digitalWrite(WARNING, HIGH);
       state--;
   
     if (lightState == 0)
     {
-
      p("{\"voltage\":");
       Serial.print(vol);
       p(",\"status\":\"%s\"}\r\n", theState); // sadly printf here does not support floats
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
       gotoXY(0,1); LcdP("S: %s",theState);
 
       gotoXY(0,2); LcdP("H:%d.%d L:%d.%d ",int(onVoltage),((int(onVoltage*100))%100)/10,int(offVoltage),((int(offVoltage*100))%100)/10);
-      gotoXY(0,3); LcdP("%05ld:%02ld:%02ld",upSecs/3600,(upSecs%60)/60, upSecs%60);      
-      gotoXY(0,4); LcdP("%05ld:%02ld:%02ld",downSecs/3600,(downSecs%60)/60, downSecs%60);  
+      gotoXY(0,3); LcdP("%05ld:%02ld:%02ld",upSecs/3600,(upSecs%3600)/60, upSecs%60);      
+      gotoXY(0,4); LcdP("%05ld:%02ld:%02ld",downSecs/3600,(downSecs%3600)/60, downSecs%60);  
       gotoXY(0,5); LcdP("%05ld:%02ld:%02ld",secs/3600,(secs%3600)/60, secs%60);
 
       lightState = 1;
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 #define LCD_Y     48
 #define LCD_CMD   0
 
-uint32_t hours=0;
-uint8_t mins=0;
-uint8_t secs=0;
+uint32_t secs=0;
+uint32_t upSecs=0;
+uint32_t downSecs=0;
+
 int a = 0;
 
 static const byte ASCII[][5] =
 uint16_t lightOnSet = 2;
 uint16_t lightOffSet = 1022;
 
-// adjust this to suit. My supply cuts off at 2.9v so this seemed reasonable.
 float offVoltage = 3.0;
 float onVoltage = 3.4;
 
 void setup()
 {
   // put your setup code here, to run once:
-  Serial.begin(115200);
+  Serial.begin(57600);
   digitalWrite(LIGHT, LOW);
   digitalWrite(RELAY, LOW);
   digitalWrite(WARNING, LOW);
       if (state == 2)
       {
         state = 0;
-        digitalWrite(RELAY, LOW);
+        digitalWrite(RELAY, LOW); downSecs=0;
         analogWrite(PIN_PWM,255); // only 3v3 intended so this will likely do
         theState = "standby ";
       }
         {
           state = 1;
           analogWrite(PIN_PWM,128); // only 3v3 intended so this will likely do
-          theState = "active  ";
+          theState = "active  "; upSecs=0;
         }
       }
     }
       gotoXY(0,1); LcdP("S: %s",theState);
 
       gotoXY(0,2); LcdP("H:%d.%d L:%d.%d ",int(onVoltage),((int(onVoltage*100))%100)/10,int(offVoltage),((int(offVoltage*100))%100)/10);
-      
-      gotoXY(0,5); LcdP("%05ld:%02d:%02d",hours,mins,secs);
+      gotoXY(0,3); LcdP("%05ld:%02ld:%02ld",upSecs/3600,(upSecs%60)/60, upSecs%60);      
+      gotoXY(0,4); LcdP("%05ld:%02ld:%02ld",downSecs/3600,(downSecs%60)/60, downSecs%60);  
+      gotoXY(0,5); LcdP("%05ld:%02ld:%02ld",secs/3600,(secs%3600)/60, secs%60);
 
       lightState = 1;
       digitalWrite(LIGHT, HIGH);
       lightState = 0;
       digitalWrite(LIGHT, LOW);
       mymillis = millis() + lightOffSet;
-      if (++secs==60) { secs=0; if (++mins==60) { mins=0; hours++; } }
-      
+      secs++;
+      if (state==1) upSecs++; else downSecs++;
     }
   }
 }
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.