// 3-х канальный вольтметр для тестера компьютерных БП
//
#include <LiquidCrystal.h>
#define NUM_SAMPLES 15
#define DIV_1 1.0076
#define DIV_2 2.0015
#define DIV_3 3.0000
//#define DIV_4 11.0718
#define V_REF 4.796
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sum[4] = { 0 };
unsigned char sample_count = 0;
float voltage[4] = { 0.0 };
char l_cnt = 0;
const int buttonPin = 7;
int oldValue = HIGH;
int tag = 0;
int hold = 0;
float ain = 0;
float bin = 0;
float cin = 0;
float axx = 0;
float bxx = 0;
float cxx = 0;
float adxx = 0;
float bdxx = 0;
float cdxx = 0;
void setup() {
lcd.begin(16, 2);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES) {
// sample each channel A2 to A5
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
sum[l_cnt] += analogRead(A2 + l_cnt);
}
sample_count++;
delay(10);
}
// calculate the voltage for each channel
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
voltage[l_cnt] = ((float)sum[l_cnt] / (float)NUM_SAMPLES * V_REF) / 1024.0;
}
int newValue = digitalRead(buttonPin); //опрашиваем пин кнопки
if (newValue != oldValue) {
if (newValue == LOW) // если кнопка нажата
{
if (hold == 0) {
axx = voltage[0] * DIV_1, 2; //сохраняем в переменные текущие значения напряжений хх
bxx = voltage[1] * DIV_2, 2;
cxx = voltage[2] * DIV_3, 2;
hold = 1;
lcd.setCursor(0, 0);
lcd.print(" Save Uxx ");
delay(1000);
}
ain = voltage[0] * DIV_1, 2; //сохраняем в переменные текущие значения напряжений хх
bin = voltage[1] * DIV_2, 2;
cin = voltage[2] * DIV_3, 2;
tag++; //увеличиваем переменную тэг на 1
delay(200);
if (tag > 3) tag = 1; // если тэг больше 1 делаем его 1 закольцовываем меню в нулевое tag больше не ходим
}
oldValue = newValue;
}
if (tag == 3) {
adxx = ((ain * 100) / axx) - 100;
bdxx = ((bin * 100) / bxx) - 100;
cdxx = ((cin * 100) / cxx) - 100;
//adxx = ((ain - axx)/((ain + axx)/2))*100;
//bdxx = ((bin - bxx)/((bin + bxx)/2))*100;
//cdxx = ((cin - cxx)/((cin + cxx)/2))*100;
lcd.setCursor(0, 0);
lcd.print(axx);
lcd.print(" ");
lcd.setCursor(6, 0);
lcd.print(bxx);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(cxx);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(adxx, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(bdxx, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(cdxx, 1);
lcd.print(" ");
}
if (tag == 2) {
lcd.setCursor(0, 1);
lcd.print(ain);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(bin);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(cin);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(axx);
lcd.print(" ");
lcd.setCursor(6, 0);
lcd.print(bxx);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(cxx);
lcd.print(" ");
}
if (tag == 1) //Если тэг равен 1 пишем в первую строку данные из переменных которые запомнились
{ //в момент нажатия кнопки
lcd.setCursor(0, 0);
lcd.print(axx);
lcd.print(" ");
lcd.setCursor(6, 0);
lcd.print(bxx);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(cxx);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(voltage[0] * DIV_1, 2);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(voltage[1] * DIV_2, 2);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(voltage[2] * DIV_3, 2);
lcd.print(" ");
}
if (tag == 0) //Если тэг равен 0 пишем в первую строку названия напряжений
{
lcd.setCursor(0, 0);
lcd.print("3,3V ");
lcd.setCursor(6, 0);
lcd.print("5,0V ");
lcd.setCursor(11, 0);
lcd.print("12.0V ");
lcd.setCursor(0, 1);
lcd.print(voltage[0] * DIV_1, 2);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(voltage[1] * DIV_2, 2);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(voltage[2] * DIV_3, 2);
lcd.print(" ");
}
// тут пишем вторую строку
sample_count = 0;
for (l_cnt = 0; l_cnt < 4; l_cnt++) {
sum[l_cnt] = 0;
}
}