[英]Arduino code anomalies - LCD fails with multiple 'if' statements
[英]Cant output Voltage from Arduino Code to LCD
好的,所以我正在嘗試創建一個紫外線計和 output 一個諾基亞 5110 LCD 的紫外線指數。 我正在使用 arduino Nano。 有一個 GY 8511 UV 傳感器,我需要 output 電壓並在末尾使用 If/esle 字符串 output 將 UV 指數放到顯示器上。
目前我正在將這些值輸出到串行監視器,以便我可以看到代碼和傳感器看到的內容。
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的電壓
目前,該代碼具有正在比較的電壓和 output 紫外線指數,但這不是我需要的值。 我想比較 output 的值
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;
}
所以我得到了正確的 output 來工作......不知道點擊了什么,我認為我已經這樣做了,但這是最終得到我想要的 output 的代碼部分。
它正在將voltage
更改為outputVoltage
(因為那是我想要的值),但不確定它的工作原理。
int voltage = outputVoltage;
Serial.print("LCD OUTPUT: ");
Serial.print(outputVoltage);
delay(100);
if(outputVoltage<1.0)
{
UVIndex = "0";
}else if (outputVoltage>1.0 && outputVoltage<=1.015)
{
UVIndex = "0.0";
}else if (outputVoltage>1.015 && outputVoltage<=1.03)
{
UVIndex = "0.1";
}
else if (outputVoltage>1.03 && outputVoltage<=1.045)
{
UVIndex = "0.2";
}else if (outputVoltage>1.045 && outputVoltage<=1.06)
{```
as you may notice in the new code, I want to be able to output to the LCD from 0.0 to 10 in increments of 0.1. Is there a (relatively) easy (For a beginner) to sort of use a lookup table or something? The datasheet for the sensor shows a linear relationship between voltage and the UVI I am trying to output.
Thanks
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.