1. 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.
    Dismiss Notice

How to set a alarm for my real tiime clock?

Discussion in 'Microcontrollers' started by IsaacChuah90, Oct 3, 2011.

  1. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    Not quite, but close. Instead of changing the if-statement conditions, just move the motion logic inside the if-statement:
    Code (text):

    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: Oct 17, 2011
  2. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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.:)
     
  3. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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: Oct 17, 2011
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0

    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 (text):
    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: Oct 18, 2011
  6. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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: Oct 18, 2011
  7. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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 (text):

    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: Oct 19, 2011
  8. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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".
     
  9. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    Ok Rusttree.I try to figure it out.Will update what I have done later.
     
  10. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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 (text):
    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: Oct 19, 2011
  11. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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 (text):

    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 (text):

        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: Oct 19, 2011
  12. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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.:)
     
  13. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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.
     
  14. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    Ok Rusttree.I got it.Thanks.:)
     
  15. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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: Oct 21, 2011
  16. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    The only way to keep the clock stable is build it on a PCB board right?
     
  17. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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.
     
  18. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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.
     

    Attached Files:

    • alarm.c
      File size:
      15.3 KB
      Views:
      59
    • i2c.c
      File size:
      5.9 KB
      Views:
      64
    • i2c_rtc.c
      File size:
      10.7 KB
      Views:
      59
    • 010.JPG
      010.JPG
      File size:
      1.5 MB
      Views:
      63
    • 025.JPG
      025.JPG
      File size:
      2 MB
      Views:
      54
    • 002.JPG
      002.JPG
      File size:
      1.7 MB
      Views:
      58
    Last edited: Oct 21, 2011
  19. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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.
     
  20. Rusttree

    Rusttree Member

    Joined:
    May 28, 2006
    Messages:
    276
    Likes:
    2
    Location:
    Phoenix, AZ
    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.
     
  21. IsaacChuah90

    IsaacChuah90 New Member

    Joined:
    Oct 3, 2011
    Messages:
    66
    Likes:
    0
    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: Oct 25, 2011

Share This Page