Hi everyone! I am new to Arduino and I am currently working on an air quality station. It uses temp, humidity, CCS811 and PM sensors. It also has a buzzer, display, and button.
My plan is to read the sensors in loop every ~5 minutes. The display shows constantly the Air quality and temperature (for example). This is updated in the loop every 5 minutes.
When the user presses the button, the display shows individual readings, i.e. he presses the button once and the display shows the last measured temperature, he presses it again and the display shows the humidity, etc.
Here's where I'm not sure, if I am on the right track. I am using interrupt with the button. In my work in progress, when pressing the button, the display shows the last measured value by the PM sensor. However, I would like this value to be displayed for a certain amount of time, for example 5 seconds (then the display returns to showing the air quality). I am wondering how to achieve this? To my knowledge I can't use delay in the interrupt. Any ideas?
/*Definitions--------------------------------------------------------------------------------------------------------------------------------------------*/
/*Buzzer-----------------------------------------------------*/
int buzzer = 2;
/*Button-----------------------------------------------------*/
//const byte interruptPin = 3;
//volatile int buttonState = 0;
const byte interruptPin = 3;
volatile byte state = LOW;
//int buttonState = 0;
/*Display-----------------------------------------------------*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
/*T&H-----------------------------------------------------*/
#include <Wire.h>
#include "DFRobot_SHT20.h"
DFRobot_SHT20 sht20;
int t; //new
int h; //new
/*CO2-----------------------------------------------------*/
//DFRobot_CCS811 CCS811(&Wire, /*IIC_ADDRESS=*/0x5A);
#include "DFRobot_CCS811.h"
DFRobot_CCS811 CCS811;
#include <Wire.h>
int co2; //new
int tvoc; //new
/*Dust-----------------------------------------------------*/
#define COV_RATIO 0.2 //ug/mmm / mv
#define NO_DUST_VOLTAGE 400 //mv
#define SYS_VOLTAGE 5000
const int iled = 4; //drive the led of sensor
const int vout = 0; //analog input
int pm; //new
float density, voltage;
int adcvalue;
int Filter(int m)
{
static int flag_first = 0, _buff[10], sum;
const int _buff_max = 10;
int i;
if(flag_first == 0)
{
flag_first = 1;
for(i = 0, sum = 0; i < _buff_max; i++)
{
_buff[i] = m;
sum += _buff[i];
}
return m;
}
else
{
sum -= _buff[0];
for(i = 0; i < (_buff_max - 1); i++)
{
_buff[i] = _buff[i + 1];
}
_buff[9] = m;
sum += _buff[9];
i = sum / 10.0;
return i;
}
}
/*Setup--------------------------------------------------------------------------------------------------------------------------------------------*/
void setup()
{
Serial.begin(9600);
/*Button-----------------------------------------------------*/
/*pinMode(interruptPin, INPUT);
attachInterrupt(0, pin_ISR, CHANGE);*/
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin),buttonPress , FALLING); // trigger when button pressed, but not when released.
/*T&H-----------------------------------------------------*/
sht20.initSHT20(); // Init SHT20 Sensor
delay(100);
sht20.checkSHT20(); // Check SHT20 Sensor
/*CO2-----------------------------------------------------*/
while(CCS811.begin() != 0){
Serial.println("failed to init chip, please check if the chip connection is fine");
delay(1000);
}
/*dust-----------------------------------------------------*/
pinMode(iled, OUTPUT);
digitalWrite(iled, LOW); //iled default closed
Serial.begin(9600); //send and receive at 9600 baud
}
/*LOOP--------------------------------------------------------------------------------------------------------------------------------------------*/
void loop()
{
/*T&C-----------------------------------------------------*/
float humd = sht20.readHumidity(); // Read Humidity
float temp = sht20.readTemperature(); // Read Temperature
Serial.print(" Temperature: ");
Serial.print(temp, 1);
Serial.print("C");
Serial.print("\t Humidity: ");
Serial.print(humd, 1);
Serial.println("%");
t=(temp, 1); //new
h= (humd, 1); //new
/*CO2-----------------------------------------------------*/
if(CCS811.checkDataReady() == true){
Serial.print("CO2: ");
Serial.print(CCS811.getCO2PPM());
Serial.print("ppm, TVOC: ");
Serial.print(CCS811.getTVOCPPB());
Serial.println("ppb");
} else {
Serial.println("Data is not ready!");
}
co2=(CCS811.getCO2PPM()); //new
tvoc=(CCS811.getTVOCPPB()); //new
if ((((CCS811.getCO2PPM()) >=500) && ((CCS811.getCO2PPM())<= 1000)) || (((CCS811.getTVOCPPB()) >=50) && ((CCS811.getTVOCPPB())<= 750))) /*/Alarm 4 - notification || means logical OR */
{
tone (buzzer, 600);
delay (180);
tone (buzzer, 400);
delay (150);
noTone (buzzer);
}
delay(1000);
/*Dust-----------------------------------------------------*/
digitalWrite(iled, HIGH);
delayMicroseconds(280);
adcvalue = analogRead(vout);
digitalWrite(iled, LOW);
adcvalue = Filter(adcvalue);
voltage = (SYS_VOLTAGE / 1024.0) * adcvalue * 11;
if(voltage >= NO_DUST_VOLTAGE)
{
voltage -= NO_DUST_VOLTAGE;
density = voltage * COV_RATIO;
}
else
density = 0;
Serial.print("The current dust concentration is: ");
Serial.print(density);
Serial.print(" ug/m3\n");
pm = (density);//new
/*if ((pm >=75) && (pm<= 115)) //Alarm 4 - notification
{
tone (buzzer, 600);
delay (180);
tone (buzzer, 400);
delay (150);
noTone (buzzer);
}
if ((pm >115) && (pm<= 150)) //Alarm 3 - notification
{
tone (buzzer, 600);
delay (100);
noTone (buzzer);
delay (60);
tone (buzzer, 600);
delay (120);
noTone (buzzer);
delay (60);
tone (buzzer, 600);
delay (140);
noTone (buzzer);
delay (60);
tone (buzzer, 400);
delay (200);
noTone (buzzer);*/
/*Display-----------------------------------------------------*/
lcd.begin(8, 2); //column and row
lcd.print("Air Q");
/* lcd.setCursor(0,1);//column and row
lcd.print(temp,1);
delay(2000);
lcd.begin(8, 2); //column and row
lcd.print("Humidity");
lcd.setCursor(0,1);//column and row
lcd.print(humd, 1);
delay(2000);
lcd.begin(8, 2); //column and row
lcd.print("CO2 level (ppm)");
lcd.setCursor(0,1);//column and row
lcd.print(CCS811.getCO2PPM());
delay(1000);
*/
delay(1000);
}
/*Press button and show different values--------------------------------------------------------------------------------------------------------------------------------------------
lcd.begin(8, 2); //column and row
lcd.print("Temp (C)");
lcd.setCursor(0,1);//column and row
lcd.print(temp,1);
delay(2000);
*/
void buttonPress() {
lcd.begin(8, 2); //column and row
lcd.print(density);
}