//sensor pin variables
int speakerPin = D0;
int coSensorPin = A0;
int ledPin = D1;
//time calculating variables
int warningnumber = 0;
long instancetime;
bool counterRunnning = false;
long delaybetweenwarnings = 30*1000;
//sensor readings and output variables
int coValue = 0;
int buzzertone[] = {2551,2273,2551,2273};
void setup()
{
Serial.begin( 9600 );
pinMode( coSensorPin, INPUT );
pinMode( speakerPin, OUTPUT );
pinMode( ledPin, OUTPUT );
Particle.variable("CO level", &coValue, INT );
Particle.variable("Warning number", &coValue, INT );
Particle.function("Reset Particle", resetParticle);
Particle.function("Stop Notifying", stopNotifying);
}
void loop()
{
readFromSensor();
if (coValue > 4 && warningnumber == 0)
{
startCounter();
activatewarning();
warningnumber = 1;
}
else if (coValue > 11 & warningnumber == 1)
{
if (counterRunnning == false)
{
startCounter();
activatewarning();
warningnumber = 2;
//Particle.publish("COdetected","second");
}
}
else if (coValue > 11 & warningnumber == 2 )
{
if (counterRunnning == false)
{
startCounter();
activatewarning();
activatewarning();
warningnumber++;
//Particle.publish("COdetected","third");
}
}
else if (coValue > 11 & warningnumber > 3)
{
if (counterRunnning == false)
{
startCounter();
activatewarning();
activatewarning();
warningnumber = 3;
//Particle.publish("COdetected","fourth");
}
}
if( checkIfCounterIsFinished() )
{
digitalWrite(ledPin, LOW);
counterRunnning = false;
}
//checking if warningnumber needs to be reset to 1
if( instancetime + 5*60*1000 < millis() )
{
warningnumber =1;
}
delay( 100 );
}
void readFromSensor()
{
coValue = analogRead(coSensorPin);
Serial.println( coValue );
}
void activatewarning()
{
digitalWrite(ledPin, HIGH);
for(int i=0; i<4; i++)
{
tone(speakerPin, buzzertone[i]);
delay(1000);
}
noTone(speakerPin);
}
void startCounter()
{
counterRunnning = true;
instancetime = millis() ;
}
bool checkIfCounterIsFinished()
{
if( counterRunnning == false )
{
return false;
}
if( instancetime + delaybetweenwarnings < millis() )
{
return true;
}
return false;
}
int resetParticle(String command)
{
if(command == "YES")
{
warningnumber = 1;
instancetime = 0;
counterRunnning = false;
coValue = 0;
for(int i=0; i<3;i++)
{
digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
delay (500);
}
return 1;
}
return 0;
}
int stopNotifying(String command)
{
if(command == "YES")
{
warningnumber = -1;
instancetime = 0;
counterRunnning = false;
coValue = 0;
for(int i=0; i<3;i++)
{
digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
delay (500);
}
tone(speakerPin, buzzertone[1],8);
return 1;
}
return 0;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .