I'm new to Arduino and I'm working on project for school. I'm trying to create a device that can detect fire and give direct feedback to the user in form of light, movement, and sound.
I'm running into a couple of issues with my code. What I would like to accomplish is that my device give direct feedback when it detects fire in form of light (LED's) and movement (servo) but not in sound.
Everything is working fine except the piezo buzzer and the led lights. The servo motor starts turning 180 degrees when the flame sensor detects fire but the leds arent shining. I don't quite understand why my leds arent shining.. is there something wrong with my code. Alse the problem with the buzzer is that the buzzer has to go off when the flame sensor detects fire but only after 15 minutes. Now it just goes off in an instant. Also the delay function for the servo seems to have influence on the duration of the piezo buzzer. I would like to keep that separate from each other. I'm working with the delay function now but it doesn't seem to work. I know I have to use the millis()
function but I don't know how to change my delay function to millis.
The code:
#include <Servo.h>
Servo myservo;
const int servoPin = D8; // Servo pin op D8
const int flamePin = D6; // Flame sensor pin op D6
const int buzzerPin = D5; // Piezo buzzer op D5
const int ledPin1 = D3; // Led licht 1 op D3
const int ledPin2 = D7; // Led licht 2 op D7
int Flame = HIGH; // De waarde HIGH wordt gekoppeld aan wanneer de vlam aan staat
void setup() {
pinMode(flamePin, INPUT); // Flame sensor is input
myservo.attach(servoPin); // Servo is ouput
pinMode(buzzerPin, OUTPUT); //Piezo buzzer is output
pinMode(ledPin1, OUTPUT); //Led 1 is output
pinMode(ledPin2, OUTPUT); //led 2 is output
} //setup
void loop() {
Flame = digitalRead(flamePin); // Flame waarde wordt gekoppeld aan de waarde die de flamePin leest
if (digitalRead(flamePin) == HIGH) { // Wanneer de flame sensor vuur waarneemt
myservo.write(180); // Draait de servo 180 graden
delay(50); // Doet er 50ms over om naar de positie te komen
noTone(D5);
delay(50000);
tone(D5, 261, 50000);
digitalWrite(ledPin1, HIGH); //Led 1 gaat branden zodra vuur wordt waargenomen
digitalWrite(ledPin2, HIGH); //Led 2 gaat branden zodra vuur wordt waargenomen
}
else if (Flame == LOW){ // Wanneer de flame sensor geen vuur waarneemt
myservo.write(0); // Draait de servo niet
noTone(D5);
digitalWrite(ledPin1, LOW); //Led 1 staat uit
digitalWrite(ledPin2, LOW); //Led 2 staat uit
}
}//loop