简体   繁体   English

无法从 Arduino 代码到 LCD 的 output 电压

[英]Cant output Voltage from Arduino Code to LCD

Ok, SO I am trying to create a UV meter and output a UV index to a Nokia 5110 LCD.好的,所以我正在尝试创建一个紫外线计和 output 一个诺基亚 5110 LCD 的紫外线指数。 I am using an arduino Nano.我正在使用 arduino Nano。 Have a GY 8511 UV Sensor that I need to output the voltage and use the If/esle string at the end to output the UV index onto the diplay.有一个 GY 8511 UV 传感器,我需要 output 电压并在末尾使用 If/esle 字符串 output 将 UV 指数放到显示器上。

Currently I am outputting these values to the serial monitor so I can see what the code and sensor is seeing.目前我正在将这些值输出到串行监视器,以便我可以看到代码和传感器看到的内容。

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);

The one that I want to use in the If/else string at the end is the "ML8511 voltage";我想在最后的 If/else 字符串中使用的是“ML8511 电压”; this is a voltage from 1.00v-3.3v这是1.00v-3.3v的电压

Currently the code has voltage as what is being compared and output the UV Index but that is not the value I need.目前,该代码具有正在比较的电压和 output 紫外线指数,但这不是我需要的值。 I want to compare the values that are being output by我想比较 output 的值

  Serial.print(" / ML8511 voltage: ");
  Serial.print(outputVoltage);

I have tried to change "Voltage" to "outputVoltage", "UVlevel" I have tried to move the math to before that string... Im kinda lost at this point我试图将“Voltage”更改为“outputVoltage”,“UVlevel”我试图将数学移动到该字符串之前......我在这一点上有点迷失

Here is the code... Its a mess, I know, I am not a code person and I am struggling through this trying to get this to work so please try to be nice...这是代码......它一团糟,我知道,我不是一个代码人,我正在努力解决这个问题,试图让它工作,所以请试着表现得很好......

Hopefully you guys can help me out and its a simple fix.希望你们能帮助我,这是一个简单的修复。

#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;
}

So I got the correct output to work... Not sure what clicked, and I thought that I was doing this already but here is the part of the code that finally got what I wanted to be output correctly.所以我得到了正确的 output 来工作......不知道点击了什么,我认为我已经这样做了,但这是最终得到我想要的 output 的代码部分。

It was changing voltage to outputVoltage (since thats the value I wanted) but not sure what it worked.它正在将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM