#include <TimerOne.h>
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
const int BUZZER_PIN = 8;
const int GAS_SENSOR_PIN = A0;
const int ALARM_THRESHOLD = 1024 * 0.8;
bool mode = false;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600);
Timer1.initialize(1500000);
Timer1.attachInterrupt(buz);
}
int TMP_VALUE = -1;
void loop() {
int sensorValue = analogRead(GAS_SENSOR_PIN);
int normalizedValue = map(sensorValue, 210, 1010, 0, 1023);
if (normalizedValue != TMP_VALUE)
{
Serial.print("Уровень света: ");
Serial.print(normalizedValue);
Serial.print(" (");
Serial.print(map(normalizedValue, 0, 1023, 0, 100));
Serial.println("%)");
TMP_VALUE = normalizedValue;
}
int color = map(normalizedValue, 0 , 1023, 0, 255);
updateLED(color);
if (normalizedValue > ALARM_THRESHOLD && mode==true)
{
tone(BUZZER_PIN, 100, 200);
delay(200);
tone(BUZZER_PIN, 1000, 200);
delay(200);
} else
{
noTone(BUZZER_PIN);
}
}
void updateLED(int value) {
int red = 255 - value;
int green = value;
int blue = 255;
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
void buz()
{
mode = !mode;
}