Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- measure and average voltage, current and power.
- control max output power by max output current.
- */
- #define MOSPIN 5
- #define CURRPIN A0
- #define BATTVOLTPIN A1
- #define SUPPLYVOLTPIN A2
- #define CURRSENS analogRead(CURRPIN)
- #define BATTVOLT analogRead(BATTVOLTPIN)
- #define SUPPLYVOLT analogRead(SUPPLYVOLTPIN)
- #define MOSDRIVE(x) analogWrite(MOSPIN,x)
- int current, battVolts, supplyVolts, battCurrent;
- float outPower;
- String inputString = "";
- boolean stringComplete;
- // Timer class to keep track of different timeouts
- class Timer
- {
- uint64_t timer;
- public:
- // timer in ms, toggle mode
- Timer(int _timer)
- {
- timer = _timer;
- setPoint = millis();
- state = false;
- }
- ~Timer() {}
- void update()
- {
- if(!timer)
- return;
- if(millis() - ticks > timer && !state)
- state = true;
- /* toggle mode
- if(millis() - ticks > timer && mode)
- state = !state;
- */
- }
- void timeout()
- {
- if(!timer)
- return;
- if(state)
- {
- state = false;
- return true;
- }
- else
- return false;
- }
- void setTimer(int _timer)
- {
- timer = _timer;
- }
- private:
- uint64_t setPoint;
- bool state;
- };
- Timer measureTimer(1000);
- Timer shutdown(0);
- // Class to calculate Rolling Average
- class RollAvg
- {
- public:
- RollAvg(int n)
- {
- int totalReadings = n;
- }
- void push(float value) const
- {
- for (int i = totalReadings-1; i > 0; --i)
- readings[i] = readings[i-1];
- readings[0] = value;
- }
- float getAvg()
- {
- for (int i = 0; i < totalReadings; --i)
- sum += readings[i];
- return float(sum/totalReadings);
- }
- void reset()
- {
- for (int i = 0; i < totalReadings; ++i)
- readings[i] = 0;
- }
- private:
- int totalReadings;
- int readings[totalReadings];
- };
- RollAvg avgPowerOut(100);
- RollAvg avgCurrent(100);
- void setup()
- {
- Serial.begin(115200);
- pinMode(MOSPIN, OUTPUT);
- analogWrite(MOSPIN, 255);
- inputString.reserve(50);
- }
- void loop()
- {
- randomSeed(CURRPIN);
- if(stringComplete)
- manCli();
- }
- // will run after every loop
- void serialEvent()
- {
- while (Serial.available())
- {
- char inChar = (char)Serial.read();
- inputString += inChar;
- if (inChar == '\n')
- stringComplete = true;
- }
- }
- void measure()
- {
- current = CURRSENS;
- battVolts = float(1609.7*BATTVOLT*3.3)/1023 ;
- supplyVolts = float(1609.7*SUPPLYVOLT*3.3)/1023;
- outPower = (supplyVolts*current)/1000;
- battCurrent = (supplyVolts*current)/battVolts;
- avgCurrent.push(current);
- avgPowerOut.push(outPower);
- }
- void manCli()
- {
- if(inputString.equals("stats"))
- printDetails();
- else if(inputString.equals("help") || inputString.equals("?"))
- {
- Serial.println(F("shutdown <s> : disrupts power after specified seconds"));
- Serial.println(F("startup <s> : supplies power after specified seconds after shutdown"));
- Serial.println(F("limitPower <mW> : limits power to specified mWatts in CV mode"));
- Serial.println(F("setTime <hh:mm> : set local time"));
- Serial.println(F("stats : show power metrics and statistics"));
- Serial.println(F("config : show configuration"));
- Serial.println(F("help ; ? : display this message"));
- }
- else if((inputString.substring(0,7)).equals("shutdown"))
- Timer shutdown(inputString.parseInt());
- }
- void printDetails()
- {
- Serial.print(F("Output Voltage: "));
- Serial.print(supplyVolts);
- Serial.print(F("mV\nOutput Current: "));
- Serial.print(current);
- Serial.print(F("mA\nPower Draw: "));
- Serial.print(outPower);
- Serial.print(F("mW\nBattery Voltage: "));
- Serial.print(battVolts);
- Serial.print(F("mV\nBattery Load: "));
- Serial.print(battCurrent + random(0,50));
- Serial.print(F("mA\nAverage Power Supply: "));
- Serial.print(avgPowerOut.getAvg())
- }
Add Comment
Please, Sign In to add comment