0

im quite new to Arduino coding. Currently im trying to create an alarm and when the alarm ring, the user will just need to remove the door sensor once to stop the alarm and listen for the next alarm. What i have is Real Time clock module to keep track of the time.

So i have tried to code on my own but failed and here's the outcome: After the alarm ring, and i remove the door sensor from each other, it stopped buzzing (Which is correct partly) but once i put it back together, it continued to ring for the alarm duration (Which was set as 1 min)

Here's the code i had:

#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
DS3231  rtc(SDA, SCL);
Time  t;
#define buz 11      //This is Buzzer that is plug into pin 11 (Change it accordingly to what you will be putting it in the arduino)
int Hor;            // This is declaring the alarm in Hours
int Min;            // This is declaring the alarm in Minutes
int Sec;            // This is declaring the alarm in Seconds
const int sensor = 10;    // Door sensor connected to Pin 10
int state; // 0 close - 1 open switch

void setup()
{  
  Wire.begin();
  rtc.begin();
  Serial.begin(9600);
  pinMode(buz, OUTPUT);
  lcd.begin(16,2);     
  lcd.setCursor(0,0);
  lcd.print("Alarm");
  lcd.setCursor(0,1);
  lcd.print("Test");
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(1, 1, 2014);   // Set the date to January 1st, 2014
  delay(2000);
  pinMode(sensor, INPUT_PULLUP);
}

void loop()
{
  t = rtc.getTime();
  Hor = t.hour;
  Min = t.min;
  Sec = t.sec;
  lcd.setCursor(0,0);
  lcd.print("Time: ");
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0,1);
  lcd.print("Date: ");
  lcd.print(rtc.getDateStr());

  alarm1();
  alarm2();
  delay(1000); 
}

void Buzzer()
{
  digitalWrite(buz,HIGH);
  delay(500);
  digitalWrite(buz, LOW);
  delay(500);
}

void alarm1() //E.g. This is the first alarm for the medicine
{
  state = digitalRead(sensor);
  if( Hor == 20 && (Min == 11 || Min == 12) && state == 0) //Comparing the current time with the Alarm time
  {   
      Buzzer();
      lcd.clear();
      lcd.print("1st Alarm ON");
      lcd.setCursor(0,1);
      lcd.print("Morning Medicine");
     } 
  else if ( Hor == 20 && (Min == 11 || Min == 12) && state == 1)
  {
    noTone(buz);
  }
  delay(200);
} 

void alarm2()
{
  if( Hor == 19 && (Min == 48 || Min == 49) && state == 0) //Comparing the current time with the Alarm time
  {   
      Buzzer();
      lcd.clear();
      lcd.print("1st Alarm ON");
      lcd.setCursor(0,1);
      lcd.print("Morning Medicine");
     } 
  else
  {         //Once user open the door sensor, the alarm will stop buzzing
    noTone(buz);
    alarm1();
  }
  delay(200);
} 

Just to clarify that the variable state i took from

http://www.instructables.com/id/How-to-Use-a-Magnetic-Door-Switch-Sensor-With-Ardu/

So to summarised my problem, i am trying to get the system to read the opening of the door sensor once so that the alarm will stop and listen to the next alarm regardless if i put it back the door sensor or not Really hope someone can help me thanks for you kind attention

  • 1
    Please, provide a link to the libraries you are using. There are tons of libraries called “DS3231”, which are not compatible with one another. – Edgar Bonet Nov 22 '17 at 18:34
2

As often, the answer to this question is “Implement a ‘Finite State Machine.’”. In this instance, you need at least three distinct states:

  • ARMED: the buzzer is not active, but it is only a matter of time...
  • ON: bzzzz... bzzzz... bzzzz...
  • STOPPED: the user has stopped the alarm.

Each of your alarms should have a start time and a stop time (say, two minutes after the start time). Then, the possible state transitions are:

  • ARMEDON: when we hit the start time of an alarm
  • ONSTOPPED: when the user stops the alarm
  • ONARMED: when we hit the stop time of the alarm
  • STOPPEDARMED: when we hit the stop time of the alarm

Notice that, once the user stops the alarm, the only way for the buzzer to start buzzing again is by going through the transitions STOPPEDARMEDON. The first transition happens at the alarm's stop time, whereas the second one happens at the next alarm's start time.

You did use a kind of state variable in your code, but the problem is you failed to realize that ARMED and STOPPED should be distinct states. If you fuse these states together then, when the user stops actively “stopping” the alarm, and if it's still start time, the system transitions immediately back to the ON state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.