#include <Wire.h>
#include <>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define LED_PIN 13
#define BUTTON_PIN 5
const int PM25_THRESHOLD = 50; // Adjust this threshold as needed
int pm25Values[10];
int pm25Index = 0;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
}
void loop() {
if (readPMSdata()) {
int pm25Value = 25; // Simulated PM2.5 value (for demonstration purposes)
pm25Values[pm25Index] = pm25Value;
pm25Index = (pm25Index + 1) % 10;
int minPM25 = getMinValue(pm25Values, 10);
int maxPM25 = getMaxValue(pm25Values, 10);
int avgPM25 = getAverageValue(pm25Values, 10);
displayPM25Stats(minPM25, maxPM25, avgPM25);
adjustLED(pm25Value);
if (pm25Value > PM25_THRESHOLD) {
activateLED();
}
if (digitalRead(BUTTON_PIN) == LOW) {
// No fan or motor in the simulator, no action needed for the button press
delay(500); // Debounce delay
}
}
}
boolean readPMSdata() {
// Simulated readPMSdata function for receiving PM2.5 data (for demonstration purposes)
return true; // Simulated data successfully read
}
int getMinValue(int arr[], int size) {
int minVal = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
int getMaxValue(int arr[], int size) {
int maxVal = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
int getAverageValue(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
return sum / size;
}
void displayPM25Stats(int min, int max, int avg) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Min: ");
lcd.print(min);
lcd.print(" Max: ");
lcd.print(max);
lcd.setCursor(0, 1);
lcd.print("Avg: ");
lcd.print(avg);
}
void adjustLED(int pm25Value) {
int brightness = map(pm25Value, 0, 1000, 0, 255);
analogWrite(LED_PIN, brightness);
}
void activateLED() {
digitalWrite(LED_PIN, HIGH);
delay(5000); // Simulated LED activation for 5 seconds
digitalWrite(LED_PIN, LOW);
}