| 大家好,我使用arduino测量的电压值,在串口可以显示但是使用spinrtf函数时,出现的值完全不一样,想请教大家怎么解决,非常感谢
下面是代码:
int MQ_6;
int MQ_7;
#include <DHT.h>
//定义针脚
#define DHTPIN 7
//定义类型,DHT11或者其它
#define DHTTYPE DHT11
//进行初始设置
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal.h>
float solar_volt =0; // variable for solar panel voltage
float bat_volt=0; // variable for battery voltage
float sample1=0; // reading from Arduino pin A0
float sample2=0; // reading from Arduino pin A1
int pwm=6; // pwm out put to mosfet
int load=9; //load is connected to pin-9
int charged_percent =0;
LiquidCrystal lcd(12,11, 10, 5, 4, 3, 2);
int backLight = 13; // pin 13 will control the backlight
//int RED=7; // To indicate discharged condition of battery
int GREEN=8; // for charging and battery fully charged
void setup()
{
Serial.begin(9600);
pinMode(7,INPUT);
dht.begin(); //DHT开始工作
//TCCR0B = TCCR0B & 0b11111000 | 0x05; // setting prescaar for 61.03Hz pwm
Serial.begin(9600);
//pinMode(pwm,OUTPUT);
//pinMode(load,OUTPUT);
//pinMode(RED,OUTPUT);
//pinMode(GREEN,OUTPUT);
digitalWrite(pwm,LOW);
digitalWrite(load,LOW);
//digitalWrite(RED,LOW);
//digitalWrite(RED,LOW);
pinMode(backLight, OUTPUT); //set pin 13 as output
analogWrite(backLight, 150); //controls the backlight intensity 0-255
//lcd.begin(16,2); // columns, rows. size of display
//lcd.clear(); // clear the screen
}
void loop()
{
delay(2000);
// 读温度或湿度要用250毫秒
float h = dht.readHumidity();//读湿度
float t = dht.readTemperature();//读温度,默认为摄氏度
//Serial.print("Humidity: ");//湿度
//Serial.println(h);
//Serial.println(" % ");
//Serial.print("Temperature: ");//温度
//Serial.println(t);
//Serial.println(" ℃ ");
char s[1000];
//char b[200];
MQ_6 = t;
MQ_7 = h;
for(int i=0;i<150;i++)
{
sample1+=analogRead(A0); //read the input voltage from solar panel
sample2+=analogRead(A1); //read the battery voltage
delay(2);
}
sample1=sample1/150;
sample2=sample2/150;
solar_volt=(sample1*4.673* 3.127)/1000+0.23;
bat_volt=(sample2*4.673* 3.127)/1000+0.23;
sprintf(s,"{"wendu":%d,"shidu":%d,"solar_volt":%d,"bat_volt":%d}",MQ_6,MQ_7,solar_volt,bat_volt);
//sprintf(b,"{"shidu":%d}",MQ_7);//每隔1s发送一次字符数组s
Serial.println(s);
//Serial.println(b);
delay(1000);
//lcd.setCursor(16,1); // set the cursor outside the display count
//lcd.print(" "); // print empty character
/////////////////////////// VOLTAGE SENSING ////////////////////////////////////////////
// actual volt/divider output=3.127
//2.43 is eqv to 520 ADC
// 1 is eqv to .004673
//Serial.print("solar input voltage :");
Serial.println(solar_volt);
//Serial.print("battery voltage :");
Serial.println(bat_volt); |
有照片结合理解:
更多回帖