I need help verifying my coding. I am using Arduino Uno with sensor DHT 22 and Sharp's GP2Y1010AU0F.
- My result is too slow. from my understanding is because of delay() and delayMicroseconds().
- the result is overlapping. if I want to stream the data, it will place the result in the same table.
/* DHT-22 sensor with 12c 16x2 LCD with Arduino uno
Temperature and humidity sensor displayed in LCD
based on: http://www.ardumotive.com/how-to-use-dht-22-sensor-en.html and
https://www.ardumotive.com/i2clcden.html for the i2c LCD library by Michalis Vasilakis
Recompile by adhitadhitadhit
Notes: use LCD i2c Library from link above, i'm not sure why but new Liquidcristal library from Francisco Malpartida isn't working for me
other thing, check your */
/////////////////////////////////////////////////////////////////////////////
// Sharp GP2Y1014AU0F Dust Sensor Demo
//
// Board Connection:
// GP2Y1014 Arduino
// V-LED Between R1 and C1
// LED-GND C1 and GND
// LED Pin 4
// S-GND GND
// Vo A5
// Vcc 5V
//
// Serial monitor setting:
// 9600 baud
/////////////////////////////////////////////////////////////////////////////
//Libraries
// Choose program options.
//#define PRINT_RAW_DATA
#define USE_AVG
#include <dht.h> // sensor library using lib from https://www.ardumotive.com/how-to-use-dht-22-sensor-en.html
//#include <LiquidCrystal_I2C.h> // LCD library using from https://www.ardumotive.com/i2clcden.html for the i2c LCD library
//#include <Wire.h>
dht DHT;
//Constants
#define DHT22_PIN 3 // DHT 22 (AM2302) - pin used for DHT22
// Arduino pin numbers.
const int sharpLEDPin = 4; // Arduino digital pin 7 connect to sensor LED.
const int sharpVoPin = A5; // Arduino analog pin 5 connect to sensor Vo.
// For averaging last N raw voltage readings.
#ifdef USE_AVG
#define N 100
static unsigned long VoRawTotal = 0;
static int VoRawCount = 0;
#endif // USE_AVG
// Set the typical output voltage in Volts when there is zero dust.
static float Voc = 0.6;
// Use the typical sensitivity in units of V per 100ug/m3.
const float K = 0.5;
//Variables
float hum; //Stores humidity value
float temp; //Stores temperature value
// Helper functions to print a data value to the serial monitor.
void printValue(String text, unsigned int value, bool isLast = false) {
Serial.print(text);
Serial.print("=");
Serial.print(value);
if (!isLast) {
Serial.print(", ");
}
}
void printFValue(String text, float value, String units, bool isLast = false) {
Serial.print(text);
Serial.print("=");
Serial.print(value);
Serial.print(units);
if (!isLast) {
Serial.print(", ");
}
}
void setup() {
pinMode(sharpLEDPin, OUTPUT);
Serial.begin(9600);
// delay(2000);
// Serial.println("");
//Serial.println("=================");
}
void loop() {
int chk = DHT.read22(DHT22_PIN);
//Read data and store it to variables hum and temp
hum = DHT.humidity;
temp = DHT.temperature;
digitalWrite(sharpLEDPin, LOW);
// Wait 0.28ms before taking a reading of the output voltage as per spec.
delayMicroseconds(280);
// Record the output voltage. This operation takes around 100 microseconds.
int VoRaw = analogRead(sharpVoPin);
// Turn the dust sensor LED off by setting digital pin HIGH.
digitalWrite(sharpLEDPin, HIGH);
// Wait for remainder of the 10ms cycle = 10000 - 280 - 100 microseconds.
delayMicroseconds(9620);
// Print raw voltage value (number from 0 to 1023).
#ifdef PRINT_RAW_DATA
printValue("VoRaw", VoRaw, true);
Serial.println("");
#endif // PRINT_RAW_DATA
// Use averaging if needed.
float Vo = VoRaw;
#ifdef USE_AVG
VoRawTotal += VoRaw;
VoRawCount++;
if (VoRawCount >= N) {
Vo = 1.0 * VoRawTotal / N;
VoRawCount = 0;
VoRawTotal = 0;
} else {
return;
}
#endif // USE_AVG
// Compute the output voltage in Volts.
Vo = Vo / 1024.0 * 5.0;
//Display
printFValue("Vo", Vo * 1000.0, "mV");
// Convert to Dust Density in units of ug/m3.
float dV = Vo - Voc;
if (dV < 0) {
dV = 0;
Voc = Vo;
}
float dustDensity = dV / K * 100.0;
printFValue("DustDensity", dustDensity, "ug/m3", true);
//Serial.println(" ");
//Print temp and humidity values to serial monitor
Serial.print(" ");
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
//delay(2000); //Delay 2 sec.
}
Thank You.