seafox21:
Terribly sorry, here it is!
This one is with Rob's library, with continuous run, no interruption but incorrect data from the DHT sensor
//--DHT---------
#include <dht.h>
#define DHT22_PIN 4
#define DHTTYPE DHT22
dht DHT;
int serialvalue; // value for serial input
int started = 0; // flag for whether we've received serial yet
float prevhumidity;
float prevtemperature;
long previousMillisDHT = 0; //store last time the DHT sensor was updated
long intervalDHT = 2000;
float t;
float h;
//----End of DHT--------
//-----Pulse Sensor----
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
int blinkPin = 12; // pin to blink led at each beat
int fadePin = 5; // pin to do fancy classy fading blink at each beat
int fadeRate = 0; // used to fade LED on with PWM on fadePin
// Volatile Variables, used in the interrupt service routine!
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
// Regards Serial OutPut -- Set This Up to your needs
static boolean serialVisual = false; // Set to 'false' by Default. Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse
//----End of Pulse Sensor define---
void setup()
{
Serial.begin(115200); // open the arduino serial port
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
//-----Pulse Sensor-----
pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat!
pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat!
//Serial.begin(115200); // we agree to talk fast!
interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
// IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
// UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
// analogReference(EXTERNAL);
//-----End of Pulse Sensor-----
DHT.read22(DHT22_PIN);
}
void loop()
{//---Pulse Sensor-----
serialOutput() ;
if (QS == true){ // A Heartbeat Was Found
// BPM and IBI have been Determined
// Quantified Self "QS" true when arduino finds a heartbeat
digitalWrite(blinkPin,HIGH); // Blink LED, we got a beat.
fadeRate = 255; // Makes the LED Fade Effect Happen
// Set 'fadeRate' Variable to 255 to fade LED with pulse
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
}
ledFadeToBeat(); // Makes the LED Fade Effect Happen
//delay(20); // take a break
//-----End of Pulse Sensor
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
int sensorValue2 = analogRead(A2);
int sensorValue3 = analogRead(A3);
int sensorValue4 = analogRead(A4);
int sensorValue5 = analogRead(A5);
int sensorValue6 = BPM;
unsigned long currentMillis = millis();
if(currentMillis - previousMillisDHT >= intervalDHT)
{
//save the last time DHT is read
previousMillisDHT = currentMillis;
//-------filtering the flickering 0 results of the DHT sensor
//-------filtering the flickering 0 results of the DHT sensor
h = DHT.humidity;
if(h == 0)
{
h = prevhumidity;
} else {
prevhumidity = h;
}
t = DHT.temperature;
if(t == 0)
{
t = prevtemperature;
} else {
prevtemperature = t;
}
//-----end of filter
}
if(Serial.available()) // check to see if there's serial data in the buffer
{
serialvalue = Serial.read(); // read a byte of serial data
started = 1; // set the started flag to on
if (serialvalue==14)
{
digitalWrite(13, HIGH);
}
if (serialvalue==13)
{
digitalWrite(13, LOW);
}
}
if(started) { // loop once serial data has been received
Serial.print(sensorValue); // print the first input
Serial.print(" "); // print a space
Serial.print(sensorValue1); // print the second input
Serial.print(" "); // print a space
Serial.print(sensorValue2); // print the third input
Serial.print(" "); // print a space
Serial.print(sensorValue3); // print the fourth input
Serial.print(" "); // print a space
Serial.print(sensorValue4); // print the second input
Serial.print(" "); // print a space
Serial.print(sensorValue5); // print the second input
Serial.print(" "); // print a space
Serial.print(sensorValue6); // print the second input
Serial.print(" "); // print a space
Serial.print(h);
Serial.print(" ");
Serial.print(t);
Serial.println(); // print a line-feed
}
}
void ledFadeToBeat(){
fadeRate -= 15; // set LED fade value
fadeRate = constrain(fadeRate,0,255); // keep LED fade value from going into negative numbers!
analogWrite(fadePin,fadeRate); // fade LED
}
Have a look at Rob's example here:
You need to read the dht every loop(), not once in setup() (remove the read from setup()).
Like:
loop()
{
int chk = DHT.read22(DHT22_PIN);
/*
switch (chk)
{
case DHTLIB_OK:
//Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
Serial.print("Ack High error,\t");
break;
default:
Serial.print("Unknown error,\t");
break;
}
*/
// DISPLAY DATA
h = DHT.humidity;
}