I am working on this Disaster Alerting prototype where I have four inputs from different sensors (a Water Level Sensor, Flame Sensor, an IR sensor for a rpm based anemometer, and a Piezzovibration sensor for Earthquakes). A red LED turns on when there are detected values which exceed the threshold level, else the green LED turns on. In addition, there is also a buzzer which is a universal output for all of the sensors, which is triggered when there are also detected values which exceed the threshold level. For my water level sensor, the RGB led turns into different colors and the buzzer turns on depending on the conditions.
The interaction between the three sensors are good, but when the Water Level sensor is inserted, the code does not work properly. The red color (which is for the values above the highest range) is then expressed by the RGB LED even if the sensor is not yet submerged in water.
How do I make it work together? Thanks in advance!
Here is the code:
const int detectwind =8;
const int detectearth=A0;
const int detectfire=12;
const int windRled =49;
const int windGled =48;
const int buzzer = 32;
const int earthRled=43;
const int earthGled=42;
const int fireRled=41;
const int fireGled=40;
unsigned long firePrevious=0;
unsigned long earthPrevious=0;
unsigned long windPrevious=0;
unsigned long buzzingPrevious=0;
int interval = 0;
unsigned long timeIntervals = 30000;
unsigned long currentMillis=0;
unsigned long previousMillis=0;
int average = 0;
int sum=0;
int earthReading=0;
int buzzing_ON=0;
unsigned long TIME_interval = 10000; //Buzzer sounds for 3 minutes
int fireReading=0;
int wind_disaster=0;
int fire=0;
int earthquake=0;
void setup() {
pinMode(windRled,OUTPUT);
pinMode(windGled,OUTPUT);
pinMode(earthRled,OUTPUT);
pinMode(earthGled,OUTPUT);
pinMode(fireRled,OUTPUT);
pinMode(fireGled,OUTPUT);
pinMode(detectwind,INPUT);
pinMode(detectfire,INPUT);
Serial.begin(9600);
}
void loop() {
currentMillis = millis();
wind_detection();
earthquake_detection();
fire_detection();
if (average >= 1)
{
wind_disaster=1;
buzzing_ON=1;
windPrevious=currentMillis;
digitalWrite(windRled,HIGH);
digitalWrite(windGled,LOW);
}else if (wind_disaster==0)
{
digitalWrite(windRled,LOW);
digitalWrite(windGled,HIGH);
}
if (earthReading >= 30)
{
earthquake=1;
earthPrevious=currentMillis;
buzzing_ON=1;
digitalWrite(earthRled,HIGH);
digitalWrite(earthGled,LOW);
} else if(earthquake==0)
{
digitalWrite(earthRled,LOW);
digitalWrite(earthGled,HIGH);
}
if (fireReading == HIGH)
{
fire=1;
firePrevious=currentMillis;
buzzing_ON=1;
digitalWrite(fireRled,HIGH);
digitalWrite(fireGled,LOW);
} else if (fire== 0)
{
digitalWrite(fireRled,LOW);
digitalWrite(fireGled,HIGH);
}
if (buzzing_ON == 1)
{
buzzer_RING();
}
if ((buzzing_ON==1) && (currentMillis-buzzingPrevious>=TIME_interval))
{
noTone(buzzer);
buzzing_ON=0;
noTone(buzzer);
}
if (currentMillis-firePrevious >= TIME_interval)
{
fire=0;
}
if (currentMillis-earthPrevious >= TIME_interval)
{
earthquake=0;
}
if (currentMillis-windPrevious >= TIME_interval)
{
wind_disaster=0;
}
}
void wind_detection() {
Serial.print("TIME=");
Serial.println(currentMillis-previousMillis);
Serial.println(sum);
int windReading = !digitalRead(detectwind);
Serial.print("WIND READING =");
Serial.println(windReading);
if ((currentMillis-previousMillis <= timeIntervals) && (interval == 0) && (windReading == HIGH))
{
sum= sum+1;
interval=1;
}
else if ((currentMillis-previousMillis <= timeIntervals) && (interval == 1) && (windReading == LOW))
{
interval=0;
}
else if (currentMillis-previousMillis > timeIntervals)
{
average = sum/60;
previousMillis = currentMillis;
Serial.print("AVERAGE=");
Serial.println(average);
sum=0;
}
delay(10);
}
void earthquake_detection(){
earthReading=analogRead(detectearth);
Serial.print("EARTHQUAKE READING =");
Serial.println(earthReading);
}
void fire_detection()
{
fireReading=digitalRead(detectfire);
}
void buzzer_RING()
{
tone(buzzer,250);
buzzingPrevious=currentMillis;
}