I have an Arduino sketch that is intended to do many things: Read from a DHT11 (temp & humidity) sensor, a KY-026 (infrared) sensor - ALL GOOD - but is also intended to read Serial responses written by an external programming language through a COM port. This last part worked well separately, before trying to combine everything in one sketch.
I have not further tested, but I believe that command Serial.read() takes the values written by both sensors with the command Serial.print(), instead of taking the values from COM port.
I attach my code:
#include <dht11.h>
#define DHT11PIN 4
int led = 13; // define the LED pin
int analogPin = A0; // KY-026 analog interface
int analogVal; //analog readings
dht11 DHT11;
void setup()
{
pinMode(led, OUTPUT);
pinMode(analogPin, INPUT);
Serial.begin(9600);
pinMode(12, OUTPUT);
}
void loop()
{
int incomingByte;
//*********** LIGHT LED ON-OFF FROM COM PORT***********//
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
}
if(incomingByte = 0x01){
digitalWrite(12, HIGH);
}
else if(incomingByte = 0x00){
digitalWrite(12, LOW);
}
//*********** LIGHT LED ON-OFF FROM COM PORT ***********//
//*********** DHT-11 ***********//
int chk = DHT11.read(DHT11PIN);
Serial.print("-");
Serial.print("H");
Serial.print((float)DHT11.humidity, 2);
Serial.print("-");
Serial.print("T");
Serial.print((float)DHT11.temperature, 2);
Serial.print("-");
delay(1000);
//*********** DHT-11 ***********//
//*********** KY-026 ***********//
// Read the digital interface
analogVal = analogRead(analogPin);
if(analogVal <= 30) // if flame is detected
{
digitalWrite(led, HIGH); // turn ON Arduino's LED
}
else
{
digitalWrite(led, LOW); // turn OFF Arduino's LED
}
// Read the analog interface
analogVal = analogRead(analogPin);
Serial.print("-");
Serial.print("F");
Serial.print(analogVal);// print analog value to serial
delay(1000);
//*********** KY-026 ***********//
}
What do you think of this?
Any comments will be much appreciated!