无法从 Arduino 代码输出电压到 LCD

问题描述

好的,所以我正在尝试创建一个紫外线计并将紫外线指数输出到诺基亚 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);

我尝试将“电压”更改为“输出电压”、“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,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,ui,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,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;
}

解决方法

所以我得到了正确的输出......不确定点击了什么,我以为我已经这样做了,但这里是代码的一部分,最终得到了我想要正确输出的内容。

它正在将 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  

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...