From 82563460007b326da6af90eae11a12f0b49730d7 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Fri, 1 Jul 2022 21:57:24 +0100 Subject: [PATCH] Work in progress --- power_meter.ino | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 power_meter.ino diff --git a/power_meter.ino b/power_meter.ino new file mode 100644 index 0000000..d68d0f0 --- /dev/null +++ b/power_meter.ino @@ -0,0 +1,115 @@ +#include +#include + +const int digits[] = {9, 10, 11, 12}; +const int segments[] = {2, 8, 4, 5, 6, 7, 3}; + +const bool one[7] = {false, true, true, false, false, false, false}; +const bool two[7] = {true, true, false, true, true, false, true}; +const bool three[7] = {true, true, true, true, false, false, true}; +const bool four[7] = {false, true, true, false, false, true, true}; +const bool five[7] = {true, false, true, true, false, true, true}; +const bool six[7] = {true, false, true, true, true, true, true}; +const bool seven[7] = {true, true, true, false, false, false, false}; +const bool eight[7] = {true, true, true, true, true, true, true}; +const bool nine[7] = {true, true, true, true, false, true, true}; +const bool zero[7] = {true, true, true, true, true, true, false}; + +Chrono timer; + +float photosensor_voltage = 0; + + +void writeDigit(char d, char val, bool dec) { + const bool *segs; + switch (val) { + case 0: + segs = zero; + break; + case 1: + segs = one; + break; + case 2: + segs = two; + break; + case 3: + segs = three; + break; + case 4: + segs = four; + break; + case 5: + segs = five; + break; + case 6: + segs = six; + break; + case 7: + segs = seven; + break; + case 8: + segs = eight; + break; + case 9: + segs = nine; + break; + } + for (int i = 0; i < 7; ++i) { + if (segs[i]) { + digitalWrite(segments[i], HIGH); + } else { + digitalWrite(segments[i], LOW); + } + } + if (dec) { + digitalWrite(segments[7], HIGH); + } + digitalWrite(digits[d], LOW); + delay(1); + digitalWrite(digits[d], HIGH); + digitalWrite(segments[7], LOW); +} + +void showNumber(float number) { + int n = number; + writeDigit(0, (n/1000U) % 10, false); + writeDigit(1, (n/100U) % 10, false); + writeDigit(2, (n/10U) % 10, false); + writeDigit(3, n % 10, false); +} + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); + + for (int i = 0; i < 4; ++i) { + pinMode(digits[i], OUTPUT); + digitalWrite(digits[i], HIGH); + } + for (int i = 0; i < 7; ++i) { + pinMode(segments[i], OUTPUT); + digitalWrite(segments[i], LOW); + } +} + +void loop() { + if (timer.hasPassed(100)) { + timer.restart(); + photosensor_voltage = readMinVoltage(A5); + } + showNumber(photosensor_voltage); + +} + +float readMinVoltage(int pin) { + float minvolts = 5000; + float volts = 0; + for (int i = 0; i < 100; ++i) { + volts = analogRead(A7) * (4.8/1.023); + if (volts < minvolts) { + minvolts = volts; + } + delay(0.2); + } + return minvolts; +} -- 2.46.2