Hi there, I must say I am quite new to the whole arduino world, therefore I have a question.
Setup: MQ2-gassensor, 2 red leds, 2 green leds, buzzer, 5 220 Ohm resistors.
Project description: Gas sensor sensses the quantity of gas in a room. If this is below threshold value then the green leds will be on. If the quantity of gas is above the threshhold value of 350, the green leds go off, red leds go on and buzzers goes on vice versa.
What I want to reach next is a fade in the leds by using a "for loop". Treshold > 350, green leds fade off, red leds fade on, buzzers starts making sound. Treshold < 350, red leds fade off, green leds fade on, buzzer shuts off.
Does someone know how to write a "for loop" with the fade into the leds into my existing code? Thanks a lot!!
Code:
int redLed1 = 12;
int redLed2 = 11;
int greenLed1 = 10;
int greenLed2 = 9;
int buzzer = 8;
int smokeA1 = A5;
// Your threshold value
int sensorThres = 350;
void setup() {
pinMode(redLed1, OUTPUT);
pinMode(redLed2, OUTPUT);
pinMode(greenLed1, OUTPUT);
pinMode(greenLed2, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA1, INPUT);
Serial.begin(9600);
}
void loop() {
int analogSensor = analogRead(smokeA1);
Serial.print("Pin A1: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(redLed1, HIGH);
digitalWrite(redLed2, HIGH);
digitalWrite(greenLed1, LOW);
digitalWrite(greenLed2, LOW);
tone(buzzer, 1000, 2000);
}
else
{
digitalWrite(redLed1, LOW);
digitalWrite(redLed2, LOW);
digitalWrite(greenLed1, HIGH);
digitalWrite(greenLed2, HIGH);
noTone(buzzer);
}
delay(100);
}