#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
SoftwareSerial mySerial(3, 2); // RX, TX
// 初始化 LCD 引脚
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//for Timer
unsigned long previousMillis = 0;
long interval = 2000;
//for voltage sensor
int analogInput = A0;
float R1 = 30000.0; //
float R2 = 7500.0; //
//for smoothing
const int numReadings = 20;
float readings[numReadings]; // the readings from the analog input
char buff[5];
void setup() {
pinMode(analogInput, INPUT);
Serial.begin(9600);
Serial.println("Type AT commands!");
mySerial.begin(9600);
// 初始化 LCD
lcd.begin(16, 2);
lcd.print("Voltage Reader");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
float volt = readAvg();
Serial.println(volt, 1);
sendData(volt);
// 在 LCD 上显示电压
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.setCursor(9, 0);
lcd.print(volt, 1);
lcd.print(" V");
}
}
float readCorrectVoltage() {
// read normal Arduino value
int adcVal = analogRead(A0);
// read correct supply voltage
float supply = readVcc() / 1000.0;
float voltage = adcVal * 5.0 / 1024.0; //AS's way
float voltCorrected = supply / 5 * voltage; //AS's way
return voltCorrected;
}
long readVcc() {
long result;
// Read 1.1V reference against AVcc
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1126400L / result; // Calculate Vcc (in mV); 1126400 = 1.1*1024*1000
return result;
}
float readNominalVoltage() {
int value = analogRead(analogInput);
float vout = (value * 5.0) / 1024.0; // see text
float vin = vout / (R2 / (R1 + R2));
return vin;
}
float readAvg() {
float total = 0.0;
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0.0;
}
for (int readIndex = 0; readIndex < numReadings; readIndex++) {
readings[readIndex] = readCorrectVoltage();
total = total + readings[readIndex];//incremental total
delay(1);
}
float averageV = total / numReadings; // the average
return averageV;
}
float read_running_average() {
int total = 0;
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
for (int readIndex = 0; readIndex < numReadings; readIndex++) {
readings[readIndex] = analogRead(analogInput);
total = total + readings[readIndex];//incremental total
}
int average = total / numReadings; // the average
float vout = (average * 5.0) / 1024.0; // see text
float vin = vout / (R2 / (R1 + R2));
return vin;
}
void sendData(float volt) {
dtostrf(volt, 4, 1, buff);
String data = "#" + String(buff) + "~";
mySerial.print(data);
}