From ade1c83ff05160d6c5f044495437cd1468c86ff6 Mon Sep 17 00:00:00 2001 From: vicentebearth Date: Thu, 27 Aug 2026 17:54:42 +0200 Subject: [PATCH] Update rp2040-zero/voltage-current-monitor-usb.ino --- rp2040-zero/voltage-current-monitor-usb.ino | 407 ++++++++++---------- 1 file changed, 203 insertions(+), 204 deletions(-) diff --git a/rp2040-zero/voltage-current-monitor-usb.ino b/rp2040-zero/voltage-current-monitor-usb.ino index 52f7b19..309bd54 100644 --- a/rp2040-zero/voltage-current-monitor-usb.ino +++ b/rp2040-zero/voltage-current-monitor-usb.ino @@ -2,13 +2,13 @@ #include #include #include -#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 +#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 @@ -26,15 +26,19 @@ int uptime_sec = 0; int raw_voltage = 0; int raw_current = 0; int counter = 0; -unsigned long old_millis = 0; +unsigned long old_millis_blink = 0; unsigned long old_millis_clock = 0; +unsigned long old_millis_main = 0; Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { - old_millis = millis(); + //set old milli values + old_millis_blink = millis(); old_millis_clock = millis(); + old_millis_main = millis(); + analogReadResolution(12); //adc_init(); //adc_set_temp_sensor_enabled(true); @@ -46,233 +50,228 @@ void setup() { Wire.begin(); //display init - if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)){ + if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println("Display Error: Check Connections"); - Serial.print("SDA="); + Serial.print("SDA:"); Serial.println(I2C_SDA); Serial.print("SCL:"); Serial.println(I2C_SCL); - while(true){ - delay(100); + while (true) { + delay(100); } } - + drawSplash(); + delay(splash_time); } 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(); + if (millis() - old_millis_main >= 100) { + raw_voltage = analogRead(VOLTAGE_PIN); + voltage = (raw_voltage / 4095.0) * 3.3 * voltage_divider; + raw_current = analogRead(CURRENT_PIN); + current = (raw_current / 4095.0f) * 3.3f * (1000.0f / shunt_resistance); + wattage = current * voltage; + if (use_usbscore) { + usb_score = calculateUSBScore(); + } else { + usb_score = default_usbscore; + } + drawDisplay(); } - else{ - usb_score = default_usbscore; - } - drawDisplay(); + old_millis_main = millis(); runCounter(1000); countUptime(); - delay(100); } //display draw function void drawDisplay() { - if(!use_default_fonts){ - display.clearDisplay(); - display.setTextColor(WHITE); - display.setTextSize(1); - if(use_7seggchan){ - display.setFont(&DSEG7_7SEGGCHAN_MINI_Regular_48); - display.setCursor(84, 66); - if(usb_score == -1){ - display.println("-"); - } - else if (usb_score == -2) { - display.println(" "); - } - else { - display.println(usb_score); - } - display.drawFastVLine(80,16,48,WHITE); + if (!use_default_fonts) { + display.clearDisplay(); + display.setTextColor(WHITE); + display.setTextSize(1); + if (use_7seggchan) { + display.setFont(&DSEG7_7SEGGCHAN_MINI_Regular_48); + display.setCursor(84, 66); + if (usb_score == -1) { + display.println("-"); + } else if (usb_score == -2) { + display.println(" "); + } else { + display.println(usb_score); + } + display.drawFastVLine(80, 16, 48, WHITE); } - display.drawFastHLine(0,16,128,WHITE); - display.setCursor(0,64); - display.setFont(&DialogInput_plain_12); - display.print("W:"); - display.setCursor(0, 48); - display.print("A:"); - display.setCursor(0,32); - display.print("V:"); - display.setFont(&DSEG7_Classic_Mini_Regular_11); - display.setCursor(13, 64); - display.print(wattage); - display.setCursor(13,48); - display.print(current); - display.setCursor(13,32); - display.print(voltage); - if(use_bitmaps){ - display.setCursor(16,0); - display.drawBitmap(0,0,temperature_bitmap,16,16,WHITE); - display.setFont(&DSEG7_Classic_Mini_Regular_11); - display.setCursor(16,16); - } - else { - display.setCursor(0,16); - } - display.print(analogReadTemp(),1); - display.print(" C°"); - if(use_bitmaps){ - display.drawBitmap(65,0,clock_bitmap,16,16,WHITE); - display.setCursor(82,16); - } - else{ - display.setCursor(82,16); - } - display.drawFastVLine(62,0,16,WHITE); - if(uptime_min<10){ - display.print("0"); - } - display.print(uptime_min); - display.print(":"); - display.setFont(&DSEG7_Classic_Mini_Regular_11); - if(uptime_sec < 10){ - display.print("0"); - } - display.print(uptime_sec); - display.display(); - } - else { - display.clearDisplay(); - display.drawFastHLine(0,16,128,WHITE); - display.setTextColor(WHITE); - display.setTextSize(1); - display.setCursor(0,56); - display.setFont(NULL); - display.print("W:"); - display.setCursor(0, 44); - display.print("A:"); - display.setCursor(0,32); - display.print("V:"); - display.setFont(NULL); - display.setCursor(13, 56); - display.print(wattage); - display.setCursor(13,44); - display.print(current); - display.setCursor(13,32); - display.print(voltage); - if(use_bitmaps){ - display.setCursor(16,0); - display.drawBitmap(0,0,temperature_bitmap,16,16,WHITE); - display.setFont(NULL); - display.setCursor(16,8); - } - else { - display.setCursor(0,8); - } - display.print(analogReadTemp(),1); - display.print(" C"); - if(use_bitmaps){ - display.setCursor(16,0); - display.drawBitmap(0,0,temperature_bitmap,16,16,WHITE); - display.setCursor(16,16); - } - else { - display.setCursor(0,8); - } + display.drawFastHLine(0, 16, 128, WHITE); + display.setCursor(0, 64); + display.setFont(&DialogInput_plain_12); + display.print("W:"); + display.setCursor(0, 48); + display.print("A:"); + display.setCursor(0, 32); + display.print("V:"); + display.setFont(&DSEG7_Classic_Mini_Regular_11); + display.setCursor(13, 64); + display.print(wattage); + display.setCursor(13, 48); + display.print(current); + display.setCursor(13, 32); + display.print(voltage); + if (use_bitmaps) { + display.setCursor(16, 0); + display.drawBitmap(0, 0, temperature_bitmap, 16, 16, WHITE); + display.setFont(&DSEG7_Classic_Mini_Regular_11); + display.setCursor(16, 16); + } else { + display.setCursor(0, 16); + } + display.print(analogReadTemp(), 1); + display.print(" C°"); + if (use_bitmaps) { + display.drawBitmap(65, 0, clock_bitmap, 16, 16, WHITE); + display.setCursor(82, 16); + } else { + display.setCursor(82, 16); + } + display.drawFastVLine(62, 0, 16, WHITE); + if (uptime_min < 10) { + display.print("0"); + } + display.print(uptime_min); + display.print(":"); + display.setFont(&DSEG7_Classic_Mini_Regular_11); + if (uptime_sec < 10) { + display.print("0"); + } + display.print(uptime_sec); + display.display(); + } else { + display.clearDisplay(); + display.drawFastHLine(0, 16, 128, WHITE); + display.setTextColor(WHITE); + display.setTextSize(1); + display.setCursor(0, 56); + display.setFont(NULL); + display.print("W:"); + display.setCursor(0, 44); + display.print("A:"); + display.setCursor(0, 32); + display.print("V:"); + display.setFont(NULL); + display.setCursor(13, 56); + display.print(wattage); + display.setCursor(13, 44); + display.print(current); + display.setCursor(13, 32); + display.print(voltage); + if (use_bitmaps) { + display.setCursor(16, 0); + display.drawBitmap(0, 0, temperature_bitmap, 16, 16, WHITE); + display.setFont(NULL); + display.setCursor(16, 8); + } else { + display.setCursor(0, 8); + } + display.print(analogReadTemp(), 1); + display.print(" C"); + if (use_bitmaps) { + display.setCursor(16, 0); + display.drawBitmap(0, 0, temperature_bitmap, 16, 16, WHITE); + display.setCursor(16, 16); + } else { + display.setCursor(0, 8); + } - if(use_bitmaps){ - display.drawBitmap(65,0,clock_bitmap,16,16,WHITE); - display.setCursor(82,8); - } - else{ - display.setCursor(94,8); - } - display.drawFastVLine(62,0,16,WHITE); - if(uptime_min<10){ - display.print("0"); - } - display.print(uptime_min); - display.print(":"); - display.setFont(NULL); - if(uptime_sec < 10){ - display.print("0"); - } - display.print(uptime_sec); - display.display(); + if (use_bitmaps) { + display.drawBitmap(65, 0, clock_bitmap, 16, 16, WHITE); + display.setCursor(82, 8); + } else { + display.setCursor(94, 8); + } + display.drawFastVLine(62, 0, 16, WHITE); + if (uptime_min < 10) { + display.print("0"); + } + display.print(uptime_min); + display.print(":"); + display.setFont(NULL); + if (uptime_sec < 10) { + display.print("0"); + } + display.print(uptime_sec); + display.display(); } } -int calculateUSBScore(){ //returns score depending on usb voltage, gets displayed as 7segg-chan - if(voltage < 3 || voltage > 8){ - if(counter == 0){ - return -1 ; +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 { - 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){ + } 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){ + } 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 ; + } 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 { +void runCounter(int time) { + if (millis() > old_millis_blink + time) { + if (counter == 0) { + counter = counter + 1; + } else { counter = 0; } - old_millis = millis(); + old_millis_blink = 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(); +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 +} + +//draws splash screen +void drawSplash() { + if (draw_splash){ + if (use_version_splash){ + display.drawBitmap(0, 0, placeholder, 128, 64, WHITE); + } + else{ + display.drawBitmap(0, 0, placeholder, 128, 64, WHITE); + } + } +} +