Output device on fire alarm is't acting as it should be

Hello.

I'm building home fire alarm device by using Arduino pro mini 5v, 2 analog sensors and buzzer. Everything seems to be fine except buzzer reaction. I need this device to buzz every time when value on both or one analog ports exceeds programmed value. Board is going to sleep every 8 seconds and waking up by watchdog timer.

At the moment it's buzzes each time when it's waking up. If sensor values shows 'Flame detected' or 'Smoke detected' it's not buzzes at all what is strange for me. Please can I ask you to take a look into this and to shed some light on this issue?

Thank you!

Here is the code:

//****************************************************************
/*
 * Watchdog Sleep Example 
 * Demonstrate the Watchdog and Sleep Functions
 * Photoresistor on analog0 Piezo Speaker on pin 10
 * 
 
 * KHM 2008 / Lab3/  Martin Nawrath nawrath@khm.de
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 
 */
//****************************************************************

#include <avr/sleep.h>
#include <avr/wdt.h>

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

int nint;
int pinLed=13;
int pinPiezoSpeaker=9;
int pinFlameSensor=A0;
int pinSmokeSensor=A3;

volatile boolean f_wdt=1;

void setup()
{

  Serial.begin(9600);
  pinMode(pinLed,OUTPUT);
  pinMode(pinPiezoSpeaker,OUTPUT);
  pinMode(pinFlameSensor,INPUT);
  pinMode(pinSmokeSensor,INPUT);
  Serial.println("nightingale");

  // CPU Sleep Modes 
  // SM2 SM1 SM0 Sleep Mode
  // 0    0  0 Idle
  // 0    0  1 ADC Noise Reduction
  // 0    1  0 Power-down
  // 0    1  1 Power-save
  // 1    0  0 Reserved
  // 1    0  1 Reserved
  // 1    1  0 Standby(1)

  cbi( SMCR,SE );      // sleep enable, power down mode
  cbi( SMCR,SM0 );     // power down mode
  sbi( SMCR,SM1 );     // power down mode
  cbi( SMCR,SM2 );     // power down mode

  setup_watchdog(9);
}

byte del;
int cnt;
byte state=0;
int light=0;


//****************************************************************
//****************************************************************
//****************************************************************
void loop(){


  if (f_wdt==1) {  // wait for timed out watchdog / flag is set when a watchdog timeout occurs
    f_wdt=0;       // reset flag
    
  
    
    {
  {
  Serial.print ("Flame Sensor Value: ");
  int x = analogRead (pinFlameSensor); // reading flame sensor value
  Serial.println (x); // write flame sensor value to x
  
    if (x < 100)
  {
    Serial.println(" FLAME DETECTED!");
    digitalWrite (pinPiezoSpeaker, HIGH); // sets the buzzer on
  }
  else
  {
    digitalWrite (pinPiezoSpeaker, LOW); // sets the buzzer off
  }
  //delay (1000);
  }
  {
  Serial.print ("Smoke Sensor Value: ");
  int y = analogRead (pinSmokeSensor); // reading smoke sensor value
  Serial.println (y); //write smoke sensor value to y
  
  
  if (y >= 750)
  {
    Serial.println(" SMOKE DETECTED!");
    digitalWrite(pinPiezoSpeaker, HIGH);   // sets the buzzer on
  }
  else
  {
  digitalWrite(pinPiezoSpeaker, LOW);    // sets the buzzer off
  }


  //delay (1000);
} 
    

    nint++;
    digitalWrite(pinLed,1);  // let led blink

    Serial.print("sleep " );
    Serial.println(nint );
    delay(2);               // wait until the last serial character is send
    digitalWrite(pinLed,0);


    pinMode(pinLed,INPUT); // set all used port to intput to save power
    pinMode(pinPiezoSpeaker,INPUT); // set all used port to intput to save power

    system_sleep();

    pinMode(pinLed,OUTPUT); // set all ports into state before sleep
    pinMode(pinPiezoSpeaker,OUTPUT); // set all ports into state before sleep


  }

}
}
void system_sleep() {

  cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF

  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
  sleep_enable();

  sleep_mode();                        // System sleeps here

    sleep_disable();                     // System continues execution here when watchdog timed out 
    sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON

}

//****************************************************************
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {

  byte bb;
  int ww;
  if (ii > 9 ) ii=9;
  bb=ii & 7;
  if (ii > 7) bb|= (1<<5);
  bb|= (1<<WDCE);
  ww=bb;
  Serial.println(ww);


  MCUSR &= ~(1<<WDRF);
  // start timed sequence
  WDTCSR |= (1<<WDCE) | (1<<WDE);
  // set new watchdog timeout value
  WDTCSR = bb;
  WDTCSR |= _BV(WDIE);


}
//****************************************************************  
// Watchdog Interrupt Service / is executed when  watchdog timed out
ISR(WDT_vect) {
  f_wdt=1;  // set global flag
}

Here is serial monitor output for flame sensor:

nightingale
49
Flame Sensor Value: 970
Smoke Sensor Value: 111
sleep 1
Flame Sensor Value: 68
FLAME DETECTED!
Smoke Sensor Value: 113

Board is going to sleep every 8 seconds and waking up by watchdog timer.

Why? Surely you have power available most of the time. The Arduino, in a critical application like a fire detector, should stay awake.

I wouldn't buy one of your devices if it slept most of the time.

PaulS:

Board is going to sleep every 8 seconds and waking up by watchdog timer.

Why? Surely you have power available most of the time. The Arduino, in a critical application like a fire detector, should stay awake.

I wouldn't buy one of your devices if it slept most of the time.

Firstly this is device I'm building for personal use, I don't have plans to sell it. Secondly, it's OK enough if alarm system will search for fire and smoke every 8 seconds and it's will save batteries. Possibly it could work 1-3 years without recharging.

Hi r2d2

What happens if you remove or comment out all the code to do with sleep mode and let the sketch run continuously? Does the buzzer sound correctly?

Also, it looks like you have an LED that goes on or off when the sketch is running or asleep? Is that working ok?

Regards

Ray

Hackscribble:
Hi r2d2

What happens if you remove or comment out all the code to do with sleep mode and let the sketch run continuously? Does the buzzer sound correctly?

Also, it looks like you have an LED that goes on or off when the sketch is running or asleep? Is that working ok?

Regards

Ray

Thank you for the answer. Yes, LED is blinking every 8 seconds. And code without sleeping looks like this and working perfectly:

PS it was made for LED blinking but it's working for buzzer, I've checked it out.

void setup ()
{
  Serial.begin(9600);
  pinMode(A3, INPUT); 
  pinMode (13, OUTPUT);
}

void loop ()
{
  Serial.print ("Sensor Value: ");
  int x = analogRead (A3);
  Serial.println (x);

  if (x < 50)
  {
    Serial.println(" FLAME DETECTED!");
    digitalWrite (13, HIGH);
  }
  else
  {
    digitalWrite (13, LOW);
  }
  delay (1000);
}

Hi r2d2

I'm not sure what to suggest next. I'm no expert in sleep mode.

Only thing I would try is adding some println statements before and after the pinMode statements in the code below, just to check that when the system resumes from sleep, it is setting the pins to output. Having said that, if the LED carries on working after the sleep, presumably they are being executed.

system_sleep();
// println here
pinMode(pinLed,OUTPUT); // set all ports into state before sleep
pinMode(pinPiezoSpeaker,OUTPUT); // set all ports into state before sleep
// println here

Sorry I can't help more

Ray

Hackscribble:
Hi r2d2

I'm not sure what to suggest next. I'm no expert in sleep mode.

Only thing I would try is adding some println statements before and after the pinMode statements in the code below, just to check that when the system resumes from sleep, it is setting the pins to output. Having said that, if the LED carries on working after the sleep, presumably they are being executed.

system_sleep();

// println here
pinMode(pinLed,OUTPUT); // set all ports into state before sleep
pinMode(pinPiezoSpeaker,OUTPUT); // set all ports into state before sleep
// println here




Sorry I can't help more

Ray

Thank you. I've changed sketch and find that output also strange. Second println appears with delay. This is Ok or symptom of issue with code? I've made video with this behavior. http://youtu.be/q7BGA_EBU-Y

Was able to fix it by myself.

  1. Use tone() instead of digitalWrite
  2. comment this line:
    system_sleep();
    Serial.println("test1");

    pinMode(pinLed,OUTPUT); // set all ports into state before sleep
    //pinMode(pinPiezoSpeaker,OUTPUT); // set all ports into state before sleep
    Serial.println("test2");

  }

Thanks to everyone.