Arduino: modular structure

This commit is contained in:
Mikkeli Matlock
2026-01-25 19:05:17 +09:00
parent f51b165503
commit 6d043b7439
3 changed files with 50 additions and 24 deletions

View File

@@ -1,35 +1,19 @@
// Vehicle Voltage Monitor
// Reads 12V-14.4V battery via voltage divider on A0
// Outputs to Serial for PC monitoring
// Smart Serow - Main
// Motorcycle telemetry hub
// Pin definitions
const int PIN_VBAT = A0; // Vehicle voltage input (via divider)
// Voltage divider constants
// Divider: 100k upper (to Vin), 47k lower (to GND)
// Vout = Vin * (47k / (100k + 47k)) = Vin * 0.3197
// So Vin = Vout / 0.3197
// At 12V: ADC sees 3.84V | At 14.4V: ADC sees 4.60V
const float DIVIDER_RATIO = 47.0 / (100.0 + 47.0); // ~0.3197
const float ADC_REF = 5.0;
const int ADC_MAX = 1023;
#include "voltage.h"
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
voltage_init();
}
void loop() {
// Read and calculate voltage
int rawAdc = analogRead(PIN_VBAT);
float vDivider = (rawAdc / (float)ADC_MAX) * ADC_REF;
float vBattery = vDivider / DIVIDER_RATIO;
// Output to serial
Serial.print("ADC: ");
Serial.print(rawAdc);
Serial.print(" | V_bat: ");
Serial.print(vBattery, 2);
// Report battery voltage
Serial.print("V_bat: ");
Serial.print(voltage_read(), 2);
Serial.println("V");
// Heartbeat blink