0
votes

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
1
Can you summarise what you've attempted so far, and what's working/not working, what it's doing, etc? We can help answer specific questions, but not solve entire projects.roelofs
Hi Thanks for the quick response! Everything is working fine except the piezo buzzer. The servo motor starts turning 180 degrees when the flame sensor detects fire and the leds turn on as well. The problem is that the buzzer has to go off when the flame sensor detects fire but only after 10 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 seperate from eachother.Yannick van Wieringen
Thanks for the additional information! Perhaps update (edit) your question with this information, so people don't have to read through the comments?roelofs

1 Answers

1
votes

millis will return the time passed since your board rebooted. So how do you know 15 minutes have passed? You remember the time you started waiting and from time to time you check if the moment of that time plus 15 minutes has passed.

So in order to trigger a buzzer 15 minutes after fire was detected:

fire detected -> store current timestamp (millis()) in a variable. Let's name it startTime. in a loop frequently check if the current time (millis()) is >= startTime + 900000. if so, start the buzzer, else continue checking your fire sensor input to go LOW.

I'm sure you know how to pour that into code!