好的,所以我正在尝试创建一个紫外线计并将紫外线指数输出到诺基亚 5110 LCD。我正在使用 arduino Nano。有一个 GY 8511 紫外线传感器,我需要输出电压并在末尾使用 If/esle 字符串将紫外线指数输出到显示器上。
目前我正在将这些值输出到串行监视器,以便我可以看到代码和传感器看到的内容。
Serial.print("output: ");
Serial.print(refLevel);
Serial.print("ML8511 output: ");
Serial.print(uvLevel);
Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);
Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.print(uvIntensity);
我想在最后的 If/else 字符串中使用的是“ML8511 电压”;这是1.00v-3.3v的电压
目前,代码有电压作为被比较的电压并输出紫外线指数,但这不是我需要的值。我想比较正在输出的值
Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);
我试图将“Voltage”更改为“outputVoltage”,“UVlevel”我试图将数学移动到该字符串之前......我现在有点迷路了
这是代码......它一团糟,我知道,我不是一个代码人,我正在努力解决这个问题,试图让它工作,所以请试着表现得很好......
希望你们能帮助我,这是一个简单的修复。
#include <Adafruit_PCD8544.h>
#include <LCD5110_Graph.h>
LCD5110 lcd(8,9,10,12,11);
extern unsigned char BigNumbers[];
extern uint8_t splash[];
extern uint8_t ui[];
//////////////////////////////////////////////////////////////////////////////////
int UVOUT = A0; //Output from the sensor
int REF_3V3 = A1; //3.3V power on the Arduino board
/////////////////////////////////////////////////////////////////////////////////
String UV = "0";
void setup() {
//////////////////////////////////////////////////////////////////
Serial.begin(9600);
pinMode(UVOUT, INPUT);
pinMode(REF_3V3, INPUT);
Serial.println("ML8511 example");
//////////////////////////////////////////////////////////////////
lcd.InitLCD();
lcd.setFont(BigNumbers);
lcd.clrScr();
lcd.drawBitmap(0, 0, splash, 84, 48);
lcd.update();
delay(3000);
}
void loop() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int uvLevel = averageAnalogRead(UVOUT);
int refLevel = averageAnalogRead(REF_3V3);
//Use the 3.3V power pin as a reference to get a very accurate output value from sensor
float outputVoltage = 3.3 / refLevel * uvLevel;
float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
Serial.print("output: ");
Serial.print(refLevel);
Serial.print("ML8511 output: ");
Serial.print(uvLevel);
Serial.print(" / ML8511 voltage: ");
Serial.print(outputVoltage);
Serial.print(" / UV Intensity (mW/cm^2): ");
Serial.print(uvIntensity);
Serial.println();
delay(100);
////////////////////////////////////////////////////////////////////////////////////////////////////
int stringLength = 0;
UV = readSensor();
lcd.clrScr();
lcd.drawBitmap(0, 0, ui, 84, 48);
stringLength = UV.length();
printUV(stringLength);
lcd.update();
delay(150);
}
//////////////////////////////////////////////////////////////////////////////////////
//Takes an average of readings on a given pin
//Returns the average
int averageAnalogRead(int pinToRead)
{
byte numberOfReadings = 8;
unsigned int runningValue = 0;
for(int x = 0 ; x < numberOfReadings ; x++)
runningValue += analogRead(pinToRead);
runningValue /= numberOfReadings;
return(runningValue);
}
//The Arduino Map function but for floats
//From: http://forum.arduino.cc/index.php?topic=3922.0
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/////////////////////////////////////////////////////////////////////////////////////////
void printUV(int length)
{
switch(length)
{
case 1: lcd.print(UV,38,19); break;
case 2: lcd.print(UV,24,19); break;
default: lcd.print(UV,0,19); break;
}
}
String readSensor()
{
String UVIndex = "0";
int sensorValue = 0;
sensorValue = analogRead(0); //connect UV sensor to Analog 0
int voltage = (sensorValue); //* (3.3 / 1023.0))*1000; Voltage in miliVolts
Serial.print("LCD OUTPUT: ");
Serial.print(voltage);
delay(100);
if(voltage<50)
{
UVIndex = "0";
}else if (voltage>50 && voltage<=250)
{
UVIndex = "0";
}else if (voltage>250 && voltage<=350)
{
UVIndex = "1";
}
else if (voltage>350 && voltage<=400)
{
UVIndex = "2";
}else if (voltage>400 && voltage<=500)
{
UVIndex = "3";
}
else if (voltage>500 && voltage<=600)
{
UVIndex = "4";
}else if (voltage>600 && voltage<=700)
{
UVIndex = "5";
}else if (voltage>700 && voltage<=800)
{
UVIndex = "6";
}else if (voltage>800 && voltage<=900)
{
UVIndex = "7";
}
else if (voltage>900 && voltage<=1000)
{
UVIndex = "8";
}
else if (voltage>1000 && voltage<=1100)
{
UVIndex = "9";
}
else if (voltage>1100 && voltage<=1200)
{
UVIndex = "10";
}else if (voltage>1200)
{
UVIndex = "11";
}
return UVIndex;
}