#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5 , 6, 7); //lcd(rs, e, d4, d5, d6, d7);
float temp_1,temp_2,temp_3,temp_4; //创建浮点型变量temp作为存储空间准备存放数据
void setup()
{
Serial.begin(9600); //使用9600的波特率进行串口通讯
lcd.begin(20, 4); //初始化LCD
//lcd.setCursor(3,1);
//lcd.print("voltage TEST");
}
void loop()
{
Vol_1();
Vol_2();
Vol_3();
Vol_4();
}
void Vol_1()
{
int V1 = analogRead(A0); //从A0口读取电压数据存入整数型变量V1,模拟口的电压测量范围为0-5V 返回的值为0-1024
float vol_1 = V1*10.20*(5.0 /1023.0); //将 V1的值换算成实际电压值存入浮点型变量 vol
if (vol_1 == temp_1) //过滤重复的数据,只有本次的电压值和上次不一时才进行输出
{
temp_1 = vol_1; //比较完成后,将这次的值存入比对比用的变量temp
}
else
{
Serial.print("vol_1=");
Serial.print(vol_1); //串口输出电压值,不换行
Serial.println(" V"); //串口输出字符V,并且换行
lcd.setCursor(0,0);
lcd.print("vol_1 = ");
lcd.setCursor(8,0);
lcd.print(vol_1);
lcd.setCursor(12,0);
lcd.print(" V");
temp_1 = vol_1;
delay(200); //输出完成后等待0.2秒钟,用于控制数据的刷新速度。
}
}
void Vol_2()
{
int V2 = analogRead(A1);
float vol_2 = V2*(5.0 / 1023.0);
if (vol_2 == temp_2)
{
temp_2 = vol_2;
}
else
{
Serial.print("vol_2=");
Serial.print(vol_2);
Serial.println(" V");
lcd.setCursor(0,1);
lcd.print("vol_2 = ");
lcd.setCursor(8,1);
lcd.print(vol_2);
lcd.setCursor(12,1);
lcd.print(" V");
temp_2 = vol_2;
delay(200);
}
}
void Vol_3()
{
int V3 = analogRead(A2);
float vol_3 = V3*(5.0 / 1023.0);
if (vol_3 == temp_3)
{
temp_3 = vol_3;
}
else
{
Serial.print("vol_3=");
Serial.print(vol_3);
Serial.println(" V");
lcd.setCursor(0,2);
lcd.print("vol_3 = ");
lcd.setCursor(8,2);
lcd.print(vol_3);
lcd.setCursor(12,2);
lcd.print(" V");
temp_3 = vol_3;
delay(200);
}
}
void Vol_4()
{
int V4 = analogRead(A3);
float vol_4 = V4*(5.0 / 1023.0);
if (vol_4 == temp_4)
{
temp_4 = vol_4;
}
else
{
Serial.print("vol_4=");
Serial.print(vol_4);
Serial.println(" V");
lcd.setCursor(0,3);
lcd.print("vol_4 = ");
lcd.setCursor(8,3);
lcd.print(vol_4);
lcd.setCursor(12,3);
lcd.print(" V");
temp_4 = vol_4;
delay(200);
}
} |