您好,我有一个arduino代码,可在nokia 5110液晶屏上显示电池百分比,pm2.5和10.0值。 Battery + 5v power booster)显示了错误的电池百分比值。 从电源升压器的in +引脚和引脚中,我正在读取电池是否正在充电或放电。然后从电池+ v和-v引脚读取电池的值并映射为百分比。 我在这里显示代码。
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <SoftwareSerial.h>
#include <SPI.h>
SoftwareSerial Serial12(10, 9); // serial ports RX, TX
int voltagePin = A2;
int chrgPin=8;
int total=0;
int limit=30;
int val;
int voltageValue[30];
int chargingValue;
int inputHigh = 0;
int inputLow = 0;
// Software SPI (slower updates, more flexible pin options):
// pin 13 - Serial clock out (SCLK)
// pin 11 - Serial data out (DIN)
// pin 4 - Data/Command select (D/C)
// pin 5 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(13, 11, 4, 5, 3);
// variable to caclulate checksum input variables
uint16_t inputChecksum = 0;
// sensor variables
uint16_t concPM1_0_CF1;
uint16_t concPM2_5_CF1;
uint16_t concPM10_0_CF1;
uint16_t concPM1_0_amb;
uint16_t concPM2_5_amb;
uint16_t concPM10_0_amb;
uint16_t rawGt0_3um;
uint16_t rawGt0_5um;
uint16_t rawGt1_0um;
uint16_t rawGt2_5um;
uint16_t rawGt5_0um;
uint16_t rawGt10_0um;
uint8_t errorCode;
uint16_t checksum;
void setup() {
Serial.begin(9600);
pinMode(voltagePin, INPUT);
pinMode(chrgPin, INPUT);
while (!Serial) {
}
Serial12.begin(9600);
while (!Serial12) {
}
while (Serial12.read()!=-1) {}; //clear buffer
display.begin();
display.setContrast(10);
display.display(); // show splashscreen
display.clearDisplay();// clears the screen and buffer
}
void discharging()
{
total=0;
for(int i=0;i<limit;i++)
{
voltageValue[i]= analogRead(voltagePin);
total=total+voltageValue[i];
}
val=total/limit;
//Serial.println(val);
val=map(val,758,865,0,100);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,40);
display.println("B%");
display.setCursor(64,40);
display.println(val);
//Serial.println(val);
delay(10);
}
void charging()
{
total=0;
for(int i=0;i<limit;i++)
{
voltageValue[i]= analogRead(voltagePin);
total=total+voltageValue[i];
}
val=total/limit;
val=map(val,758,861,0,100);
//Serial.println(val);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,40);
display.println("B%");
display.setCursor(64,40);
display.println(val);
delay(10);
}
bool pms7003ReadData() {
// while (Serial1.read()!=-1) {}; //clear buffer
if (Serial12.available() < 32) {
// Serial.println("hello");
if (Serial12.available() == 0) {
delay(5);
return 0;
};
if (Serial12.available() > 16) {
delay(5);
return 0;
};
if (Serial12.available() > 0) {
delay(30);
return 0;
};
delay(5);
return 0;
}
if (Serial12.read() != 0x42) return 0;
if (Serial12.read() != 0x4D) return 0;
inputChecksum = 0x42 + 0x4D;
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
if (inputHigh != 0x00) return 0;
if (inputLow != 0x1c) return 0;
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
concPM1_0_CF1 = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
concPM2_5_CF1 = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
concPM10_0_CF1 = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
concPM1_0_amb = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
concPM2_5_amb = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
concPM10_0_amb = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
rawGt0_3um = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
rawGt0_5um = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
rawGt1_0um = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
rawGt2_5um = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
rawGt5_0um = inputLow+(inputHigh<<8);
inputHigh = Serial12.read();
inputLow = Serial12.read();
inputChecksum += inputHigh + inputLow;
rawGt10_0um = inputLow+(inputHigh<<8);
inputLow = Serial12.read();
inputChecksum += inputLow;
inputLow = Serial12.read();
inputChecksum += inputLow;
errorCode = inputLow;
/* Serial.println("PMS7003 ");
Serial.print("PM1.0CF1 ");
Serial.println(concPM1_0_CF1);*/
/*Serial.print("PM25CF1 ");
Serial.print(concPM2_5_CF1,1);
Serial.print(" PM10CF1 ");
Serial.print(concPM10_0_CF1,1);*/
//Serial.print("PMS7003 PM1.0amb ");
// Serial.print("PMS7003_1 ");
//Serial.print(concPM1_0_amb,1);
//Serial.print(" PM2.5amb ");
//Serial.print(" ");
//Serial.print(concPM2_5_amb,1);
//Serial.print(" PM10_0_amb ");
//Serial.print(" ");
//Serial.print(concPM10_0_amb,1);
//Serial.println();
/* Serial.print("rawGt0_3um ");
Serial.println(rawGt0_3um);
Serial.print("rawGt0_5um ");
Serial.println(rawGt0_5um);
Serial.print("rawGt1_0um ");
Serial.println(rawGt1_0um);
Serial.print("rawGt2_5um ");
Serial.println(rawGt2_5um);
Serial.print("rawGt5_0um ");
Serial.println(rawGt5_0um);
Serial.print("rawGt10_0um ");
Serial.println(rawGt10_0um);*/
//Serial.print(version);
//Serial.print(errorCode);
inputHigh = Serial12.read();
inputLow = Serial12.read();
checksum = inputLow+(inputHigh<<8);
if (checksum != inputChecksum) {
//Serial.print(';');
//Serial.print(checksum);
//Serial.print(';');
//Serial.print(inputChecksum);
}
//Serial.print('\n');
delay(10); // higher will get you checksum errors
return 0;
}
void loop() {
chargingValue=digitalRead(chrgPin);
//Serial.println(chargingValue);
if(chargingValue==1)
{
pms7003ReadData();
display.setTextSize(1.5);
display.setTextColor(BLACK);
display.setCursor(0,5);
display.println("PM2.5");
display.setCursor(55,5);
display.println(concPM2_5_amb);
delay(10);
display.setCursor(0,20);
display.println("PM10.0");
display.setCursor(55,20);
display.println(concPM10_0_amb);
delay(10);
//Serial.print("charging_value : ");
//Serial.println(chargingValue);
charging();
display.display();
display.clearDisplay();
}
else
{
pms7003ReadData();
display.setTextSize(1.5);
display.setTextColor(BLACK);
display.setCursor(0,5);
display.println("PM2.5");
display.setCursor(55,5);
display.println(concPM2_5_amb);
delay(10);
display.setCursor(0,20);
display.println("PM10.0");
display.setCursor(55,20);
display.println(concPM10_0_amb);
delay(10);
//display.display();
//display.clearDisplay();
//Serial.print("Discharging_value : ");
//Serial.println(chargingValue);
discharging();
display.display();
display.clearDisplay();
}
}
我的液晶屏工作在3.3v 传感器在5v下工作 我正在通过增强器为arduino和传感器供电,并从arduino提供3.3v到LCD屏幕。
请帮助我找出问题所在。 谢谢