// Test for minimum program size.
// Edit AVRI2C_FASTMODE in SSD1306Ascii.h to change the default I2C frequency.
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
// 電壓參數
const int analogPin = A0; // 模擬輸入引腳
const float referenceVoltage = 5.0; // Arduino 的參考電壓 (5V)
const float voltageDividerRatio = 3.0; // 假設使用分壓器減少電壓到1/3 (15V -> 5V)
// 最大最小電壓初始化
float maxVoltage = 0.0;
float minVoltage = 15.0;
// Define proper RST_PIN if required.
#define RST_PIN -1
#define IO8 8 // using IO as load control
SSD1306AsciiAvrI2c oled;
int analogValue = 0; //value of the voltage, raw format
float voltage = 0; //voltage in edited format
//------------------------------------------------------------------------------
void setup() {
#if RST_PIN >= 0
//oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
oled.begin(&Adafruit128x32, I2C_ADDRESS);
#endif // RST_PIN >= 0
// Call oled.setI2cClock(frequency) to change from the default frequency.
//oled.setFont(CalLite24);
oled.setFont(Callibri11);
oled.clear();
//oled.println("0 V");
//oled.println("0 mA");
}
void loadon()
{
pinMode(IO8, OUTPUT);
digitalWrite(IO8, LOW);
}
void loadoff()
{
pinMode(IO8, INPUT);
}
float value2voltage(int analogValue) {
voltage = 0.0048 * analogValue;
return voltage;
}
//------------------------------------------------------------------------------
void loop() {
//oled.clear();
oled.home();
//delay(1000);
// 讀取模擬引腳的值
int analogValue = analogRead(analogPin);
// 將模擬值轉換成實際電壓值
float measuredVoltage = (analogValue * referenceVoltage / 1023.0) * voltageDividerRatio;
// 更新最大最小電壓
if (measuredVoltage > maxVoltage) {
maxVoltage = measuredVoltage;
}
if (measuredVoltage < minVoltage) {
minVoltage = measuredVoltage;
}
oled.print("V: ");
oled.print(measuredVoltage);
oled.println("V "); // 用空格清除多餘字符
oled.print("Max: ");
oled.print(maxVoltage);
oled.print("V ");
oled.print("Min: ");
oled.print(minVoltage);
oled.println("V ");
delay(500);
}