AkkuPowerMeter/src/main.cpp
2020-04-13 16:07:51 +02:00

68 lines
1.8 KiB
C++

//
// Created by lukas on 10.04.20.
//
#include "Arduino.h"
#define MINVOLTAGE 2.5
#define OHM 4.9
#define INTERVALTIME 5 // refresh interval in secs
#define MEASUREPIN A3
#define MOSFETPIN A2
unsigned long oldtime = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(MOSFETPIN, OUTPUT);
pinMode(MEASUREPIN, INPUT_PULLUP);
digitalWrite(MOSFETPIN, LOW);
oldtime = micros();
}
unsigned int time = 0;
double all = 0;
void loop() {
Serial.println();
double voltage = (float) (analogRead(MEASUREPIN)) / 1024 * 5;
if (voltage > MINVOLTAGE && voltage < 4.5) {
digitalWrite(MOSFETPIN, HIGH);
double current = voltage / OHM;
double watt = current * voltage;
double amphours = current / (3600.0 / INTERVALTIME); // every 10secs is a measurement point...
all += amphours;
Serial.print(all * 1000.0, 5);
Serial.print("mA/h already captured after - ");
Serial.print(time * INTERVALTIME);
Serial.println("s");
Serial.print("Watts: ");
Serial.println(watt);
Serial.print("Current: ");
Serial.println(current);
time++;
} else if (voltage <= MINVOLTAGE) {
digitalWrite(MOSFETPIN, LOW);
Serial.print("FINISHED voltage below minvoltage - ");
Serial.print(all * 1000, 5);
Serial.print("mA/h in - ");
Serial.print(time * INTERVALTIME);
Serial.println("s");
Serial.println("please restart the print");
delay(999999999);
} else if (voltage > 4.2) {
Serial.println("no battery connected!");
}
Serial.print("Voltage: ");
Serial.println(voltage);
unsigned int delaytime = (micros() - oldtime) / 1000;
Serial.println(INTERVALTIME * 1000 - delaytime);
delay(INTERVALTIME * 1000 - delaytime);
oldtime = micros();
}