From 736a07feff71c0d9d552b09907101da1a37b6f83 Mon Sep 17 00:00:00 2001 From: vicentebearth Date: Thu, 23 Jul 2026 14:15:52 +0200 Subject: [PATCH] Made Uptime counter work and added some comments --- voltage-current-monitor-usb.ino | 109 +++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 8 deletions(-) diff --git a/voltage-current-monitor-usb.ino b/voltage-current-monitor-usb.ino index 5f776b1..52f7b19 100644 --- a/voltage-current-monitor-usb.ino +++ b/voltage-current-monitor-usb.ino @@ -2,13 +2,13 @@ #include #include #include -#include -#include "setup.h" -#include "dseg7_7seggchan_mini_font_48p_regular.h" -#include "dseg7_classic_mini_font_11p_regular.h" -#include "DialogInput_font_12p_regular.h" -#include -#include "images.h" +#include //not needed, idk why its here +#include "setup.h" //setup file, dont delete, else needed variables wont be declared +#include "dseg7_7seggchan_mini_font_48p_regular.h"//7segg-chan lives here! (fontfile, comment out if 7segg-chan is disabled (i told you not to >:( ) or default fonts are enabled)) +#include "dseg7_classic_mini_font_11p_regular.h"//font, comment out or delete if default fonts are enabled +#include "DejaVu_Sans_Mono_font_12p_regular.h"//font, comment out or delete if default fonts are enabled +#include //files for adc +#include "images.h"//bitmaps, comment out or delete if bitmaps are disabled //screen definitions #define SCREEN_WIDTH 128 @@ -23,11 +23,19 @@ float wattage = 0; int usb_score = 8; int uptime_min = 0; int uptime_sec = 0; +int raw_voltage = 0; +int raw_current = 0; +int counter = 0; +unsigned long old_millis = 0; +unsigned long old_millis_clock = 0; Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { + old_millis = millis(); + old_millis_clock = millis(); + analogReadResolution(12); //adc_init(); //adc_set_temp_sensor_enabled(true); Serial.begin(115200); @@ -52,8 +60,21 @@ void setup() { } void loop() { + raw_voltage = analogRead(VOLTAGE_PIN); + voltage = (raw_voltage / 4095.0) * 3.3 * voltage_divider; + raw_current = analogRead(CURRENT_PIN); + current = (raw_current / 4095.0) * 3.3 * 20 ; + wattage = current * voltage; + if(use_usbscore){ + usb_score = calculateUSBScore(); + } + else{ + usb_score = default_usbscore; + } drawDisplay(); - + runCounter(1000); + countUptime(); + delay(100); } //display draw function @@ -76,6 +97,7 @@ void drawDisplay() { } display.drawFastVLine(80,16,48,WHITE); } + display.drawFastHLine(0,16,128,WHITE); display.setCursor(0,64); display.setFont(&DialogInput_plain_12); @@ -183,3 +205,74 @@ void drawDisplay() { } } +int calculateUSBScore(){ //returns score depending on usb voltage, gets displayed as 7segg-chan + if(voltage < 3 || voltage > 8){ + if(counter == 0){ + return -1 ; + } + else { + return -2 ; + } + } + else if (voltage <3.5 || voltage > 6.5){ + return 0 ; + } + else if(voltage < 3.7 || voltage > 6.3){ + return 1 ; + } + else if (voltage < 4 || voltage > 6) { + return 2 ; + } + else if (voltage < 4.2 || voltage > 5.8 ){ + return 3 ; + } + else if (voltage < 4.4 || voltage > 5.6){ + return 4 ; + } + else if (voltage < 4.6 || voltage > 5.4){ + return 5; + } + else if (voltage < 4.7 || voltage > 5.3){ + return 6; + } + else if (voltage < 4.8 || voltage > 5.2){ + return 7 ; + } + else if (voltage < 4.9 || voltage > 5.1){ + return 8 ; + } + else if (voltage < 5.1 || voltage > 4.9){ + return 9 ; + } + else { + return -1 ; + } +} +//counter for any blink animations +void runCounter(int time){ + if (millis() > old_millis + time){ + if(counter == 0){ + counter = counter + 1; + } + else { + counter = 0; + } + old_millis = millis(); + } +} + +//counts uptime +void countUptime(){ + if (old_millis_clock+1000 < millis()){//old millidonalds had a farm heia heya hoo + uptime_sec =uptime_sec + 1; + if (uptime_sec >59){ + uptime_sec = 0; + uptime_min = uptime_min +1; + } + if (uptime_min > 99){ + uptime_min = 0; + uptime_sec = 0; + } + old_millis_clock = millis(); + } +} \ No newline at end of file