Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to set a alarm for my real tiime clock?

Status
Not open for further replies.
Not quite, but close. Instead of changing the if-statement conditions, just move the motion logic inside the if-statement:
Code:
if ( (clkmin==alarmMin) && (clkhrs==alarmHrs) && (buffer==0) )
{ 
    buzzer=1;
    if (motion==1)
    {
        buffer=1;
    }
}
else 
{
    buzzer=0
}
The first time you move your hand near the sensor, buffer will be set to 1. After that, only the "else" part of the if-statement will execute, which turns off the buzzer.

A snooze feature is fairly simple. But how will you activate it? You need will need a second input of some kind.
 
Last edited:
Thank you very much Rusttree.That's mean need to create a new variable right?I would like to activate it when the first alarm is activated and the hand is near the sensor,then the alarm will switch off,but after 5 minutes the alarm will activate again and this is the last time for the alarm to activate.Just like the function of alarm clock in our cellphones.:)
 
Yes, you will need to create a new variable. This one will keep track of whether it's the first time or second time the alarm has activated. If the alarm is active and motion is sensed, it will first see if snooze has been activated yet. If it hasn't, then add 5 minutes to alarmMin and turn off the buzzer. If snooze has been activated already, just turn off the buzzer. The code changes necessary to do that can be done completely within the if-else-statement in post #81 above.

I recommend naming the new variable "snooze". Let's say, if it's equal to 0, then this is the first time the alarm has activated. If snooze is equal to 1, then it's the second time. You'll have to update the value of snooze when the motion sensor is triggered.
 
Last edited:
Ok.So now I have created a new variable for the snooze.But I still can't figure out how to add the snooze inside the code.Is it the code is write like this first?
Code:
if ( (clkmin==alarmMin) && (clkhrs==alarmHrs) && (bufffer==0) && (snooze==0) )
{
   buzzer=1;
   if (motion==1)
   {
      buffer=1;
   }
}
else
{
   buzzer=0;
}
After that the coding is just like alarmMin+=snooze like that?
 
Last edited:
Rusttree,the coding for the snooze==0 is it write at the same line in the if condition or I need to create another if statement for the snooze feature?I poor in coding,hope you don't mind.I will try figure it out for the logic.:)
 
Last edited:
You have to think through the logic of the if-statements. There's nothing secret about how they work. It's exactly the same logic you use in your head to make decisions.

It helps if you actually say it in full sentences:
Code:
if ( (clkmin==alarmMin) && (clkhrs==alarmHrs) && (buffer==0) -> "Does the current time match the alarm time and is the motion sensor inactive?"
{
    buzzer=1;                                                -> "Yes, so activate the buzzer."
    if ( motion==1 )                                         -> "Now, check the motion sensor.  Is it being activated by someone's hand?"
    {
        buffer=1;                                            -> "Yes, so set this flag variable to indicate the sensor is active."
    }
}
else                                                         -> "If the times do not match or the motion sensor has already been activated."
{
    buzzer=0;                                                -> "Then turn off the buzzer (or keep it off if it was already off)."
}
And remember, this code gets executed inside a loop, so the if-statements are being constantly checked over and over again.
 
Last edited:
So, if you were thinking through the logic in complete sentences, how would you describe how to add the snooze feature? Forget about the code for right now. Just describe it in words like I did in the post above.

I'll give you a hint. One of the lines will be: "Add 5 minutes to the alarm time and turn off buzzer".
 
Rusttree,so total it have 2 loops right?Just now I have try to put the snooze inside the first lopp,but I have found that we must go through first loop first which is switch on and switch off the buzzer then only can go through for the snooze feature.This is the way I have tried to write.I know I'm sure have the mistake.Hope you don't mind to correct me.I have a bit confuse now because if I write like this,how to let the PIC know the snooze is being activated by something?It is unlike motion,motion can sense my hand and it can tell the PIC motion==1,but snooze can be stimulated by what?I willl try my best to think again the logic later.
Code:
if ( (clkmin==alarmMin) && (clkhrs==alarmHrs) && (buffer==0) && (snooze==0) )    
{                                                                                                
   buzzer=1;                                                                                                                      
   if ( (motion==1) && (snooze==1) )                                                                                 
      {
         buffer=1;                                                                                                                 
         alarmMin+=5;                                                            
      }
else
{
      buzzer=0;
}
 
Last edited:
I wouldn't call an if-statement a loop, if that's what you mean. The "loop" in your code is the while-loop that starts at the top of the readrtc function.

Anyway, you made a good attempt. First, remove the "&& (snooze==0)" from the first part of the if-statement. I'll show you where to put that instead. We'll move the buffer condition down as well.

So when the buzzer is activated, it's either the first time or the second time that alarm has activated. If it's the first time, we want to do the snooze, which adds 5 minutes and turns off the alarm. You were very close with your code:
Code:
if ( (clkmin==alarmMin) && (clkhrs==alarmHrs) )
{
    buzzer=1;
    if ( (buffer==1) && (snooze==0) )
    {
        alarmMin+=5;
        snooze=1;
        buzzer=0;
    }
I replaced "motion" with "buffer" to keep your code consistent. In your code, "motion" and "buffer" are essentially the same thing, so use one or the other. Not both. The first time the alarm is activated, snooze is still 0, so that's what it should check for. Once inside the if-statement, then you're adding 5 minutes and starting the snooze. So then you set snooze equal to 1. Then, of course, you need to turn off the buzzer by setting it equal to 0. Notice you don't need to do anything with the "buffer" variable here. That's because you already take care of that higher in your code.

Next, you take care of the else case. This is the second time the alarm has activated. This time, you don't want to snooze and you just want the buzzer to turn off:
Code:
    else if ( (buffer==1) && (snooze==1) )
    {
        alarmMin=99;
        alarmHrs=99;
        buzzer=0;
    }
Here, I set alarmHrs and alarmMin to 99 because the clock will never match those values. Therefore, the alarm will not activate again. The buzzer turns off and that's it.
 
Last edited:
Now I only realize that I have done a big mistake because the code I wrote has the big difference with yours.When I try to write the code,I'm try to write a new if-else statement for it,after that I try to combine them together but lastly I almost get stuck there.So the "define" there I change to #define buffer RC6 and the "global variable" there I remove the unsigned char buffer=0; and add unsigned char snooze=0; right?Thank you Rusttree.You are a great mentor for me,you have taught me a lot of things.:)
 
You can change that #define if you want. Although the code will work just fine if you leave it as it was. Personally, if I were you, I'd make the variable name "motionSensor". More descriptive.
 
Rusttree,I have found something here.When I deactivated the first alarm and wait for the second alarm,is it I cannot touch the motion sensor?It is because when I wait for the second alarm,I have touched the motion sensor before,after that the second alarm no longer activated.Besides that,sometimes the second alarm does not activate after the first alarm activated and sometimes when the second alarm is activated and I touched the motion sensor but it can't deactivated the alarm.Is it the breadboard problem that cause the clock unstable?
 
Last edited:
No, for your low-frequency circuit, a breadboard is just fine. If it's not working on a breadboard, it won't work on a PCB.

You just have some bug in your code still. Post your new code and we'll see what we can find.
 
Ok Rusttree.I would say that the breadboard is unstable is because sometimes when I switch on the power,it will give me different output at the LCD with the same coding.So that's make me doubt that is the breadboard problem that makes the clock unstable.I have attached the pictures of different output that I faced before.Besides,I have also attached another 2 code for the function of DS1307 RTC which are i2c.c and i2c_rtc.c.
 

Attachments

  • alarm.c
    15.3 KB · Views: 126
  • i2c.c
    5.9 KB · Views: 118
  • i2c_rtc.c
    10.7 KB · Views: 114
  • 010.JPG
    010.JPG
    1.5 MB · Views: 142
  • 025.JPG
    025.JPG
    2 MB · Views: 112
  • 002.JPG
    002.JPG
    1.7 MB · Views: 125
Last edited:
Rusttree,I think the LCD will give different output with the same coding is the problem of hardware.Now I'm trying to fix the instability of my clock.At the post #94,the problem I stated is it related to the bug of the code?Hope to see your reply soon.Thanks.
 
You misunderstood my suggested code in post #90 above. The "else if" was for the embedded if-statement, not the outside if-statement. The way you have it now, the "else if" will only execute when the current time no longer equals the alarm time. So once you snooze, the buzzer will only turn off again 60 seconds after the snooze alarm activates. That's definitely not what you want.

Revert back to your last iteration of code and made that "else if" part of the embedded if-statement. Post it again and I'll check to make sure you got it.
 
Oh.Seem like I really misunderstood what you mean in the post #90 above.So now I need to make the correction for the "else if" part only?I will post the latest code after I have done the correction.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top