new code
This commit is contained in:
142
Voltage_Reader.h
Normal file
142
Voltage_Reader.h
Normal file
@@ -0,0 +1,142 @@
|
||||
#ifndef VOLTAGE_READER_H
|
||||
#define VOLTAGE_READER_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "Config.h"
|
||||
|
||||
class VoltageReader {
|
||||
private:
|
||||
float filteredVoltage = 0.0;
|
||||
bool firstReading = true; // فلگ برای اولین خواندن
|
||||
const float alpha = 0.3; // ضریب فیلتر را کمی بیشتر کردم
|
||||
|
||||
public:
|
||||
VoltageReader() {
|
||||
init();
|
||||
}
|
||||
|
||||
void init() {
|
||||
pinMode(VOLTAGE_DIVIDER_PIN, INPUT_ANALOG);
|
||||
analogReadResolution(12); // تنظیم رزولوشن ADC به 12-bit
|
||||
delay(100); // کمی بیشتر صبر کن
|
||||
|
||||
// چند بار خواندن برای تخلیه خازنهای داخلی
|
||||
for (int i = 0; i < 10; i++) {
|
||||
analogRead(VOLTAGE_DIVIDER_PIN);
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
|
||||
// خواندن ولتاژ بدون فیلتر
|
||||
float readRawVoltage() {
|
||||
int samples = 32; // تعداد نمونهها را بیشتر کردم
|
||||
long sum = 0;
|
||||
|
||||
for (int i = 0; i < samples; i++) {
|
||||
sum += analogRead(VOLTAGE_DIVIDER_PIN);
|
||||
delayMicroseconds(20);
|
||||
}
|
||||
|
||||
int rawValue = sum / samples;
|
||||
|
||||
// تبدیل به ولتاژ
|
||||
float voltageAtPin = (rawValue * ADC_REF_VOLTAGE) / ADC_RESOLUTION;
|
||||
|
||||
// محاسبه ولتاژ اصلی
|
||||
float actualVoltage = voltageAtPin * VOLTAGE_DIVIDER_RATIO;
|
||||
|
||||
return actualVoltage;
|
||||
}
|
||||
|
||||
// خواندن با فیلتر
|
||||
float readVoltage() {
|
||||
float currentVoltage = readRawVoltage();
|
||||
|
||||
// اگر اولین بار است، فیلتر را با مقدار فعلی مقداردهی کن
|
||||
if (firstReading) {
|
||||
filteredVoltage = currentVoltage;
|
||||
firstReading = false;
|
||||
} else {
|
||||
// اعمال فیلتر Low-pass
|
||||
filteredVoltage = (alpha * currentVoltage) + ((1 - alpha) * filteredVoltage);
|
||||
}
|
||||
|
||||
return currentVoltage; // actual voltage برمیگردانیم
|
||||
}
|
||||
|
||||
// دریافت ولتاژ فیلتر شده
|
||||
float getFilteredVoltage() {
|
||||
return filteredVoltage;
|
||||
}
|
||||
|
||||
// تست سلامت مدار
|
||||
bool testCircuit() {
|
||||
Serial1.println("\n🔌 Testing voltage divider circuit...");
|
||||
|
||||
// چند بار خواندن برای اطمینان
|
||||
float sum = 0;
|
||||
int readings = 10;
|
||||
|
||||
for (int i = 0; i < readings; i++) {
|
||||
sum += readRawVoltage();
|
||||
delay(50);
|
||||
}
|
||||
|
||||
float avgVoltage = sum / readings;
|
||||
|
||||
Serial1.print("Average voltage: ");
|
||||
Serial1.print(avgVoltage, 2);
|
||||
Serial1.println("V");
|
||||
|
||||
Serial1.print("Raw ADC value: ");
|
||||
Serial1.println(analogRead(VOLTAGE_DIVIDER_PIN));
|
||||
|
||||
// ولتاژ در پین
|
||||
float pinVoltage = avgVoltage / VOLTAGE_DIVIDER_RATIO;
|
||||
Serial1.print("Voltage at PA0 pin: ");
|
||||
Serial1.print(pinVoltage, 2);
|
||||
Serial1.println("V");
|
||||
|
||||
if (avgVoltage > 4.5 && avgVoltage < 5.5) {
|
||||
Serial1.println("✅ Circuit OK (within expected 5V ±10%)");
|
||||
return true;
|
||||
} else {
|
||||
Serial1.print("❌ Expected ~5V, got ");
|
||||
Serial1.print(avgVoltage, 2);
|
||||
Serial1.println("V");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// نمایش اطلاعات دیباگ
|
||||
void debugInfo() {
|
||||
float rawVoltage = readRawVoltage();
|
||||
int rawADC = analogRead(VOLTAGE_DIVIDER_PIN);
|
||||
float pinVoltage = rawVoltage / VOLTAGE_DIVIDER_RATIO;
|
||||
|
||||
Serial1.println("\n📊 Voltage Debug Info:");
|
||||
Serial1.print("Raw ADC value: ");
|
||||
Serial1.println(rawADC);
|
||||
Serial1.print("Voltage at PA0 pin: ");
|
||||
Serial1.print(pinVoltage, 3);
|
||||
Serial1.println("V");
|
||||
Serial1.print("Actual voltage (raw): ");
|
||||
Serial1.print(rawVoltage, 3);
|
||||
Serial1.println("V");
|
||||
Serial1.print("Filtered voltage: ");
|
||||
Serial1.print(filteredVoltage, 3);
|
||||
Serial1.println("V");
|
||||
}
|
||||
|
||||
// ریست فیلتر
|
||||
void resetFilter() {
|
||||
filteredVoltage = 0.0;
|
||||
firstReading = true;
|
||||
Serial1.println("✅ Voltage filter reset");
|
||||
}
|
||||
};
|
||||
|
||||
// ایجاد نمونه سراسری
|
||||
VoltageReader voltageReader;
|
||||
|
||||
#endif // VOLTAGE_READER_H
|
||||
Reference in New Issue
Block a user