What is your supply voltage to the PIC? 4V may be enough to trigger a digital high on RC6. According to table 17.2 on page 178 of your datasheet, you need to achieve 0.8Vdd or more for the Schmitt Trigger to register high.
Back to your code. You put this if-conditional:
Code:
if (motion==1)
{
buffer=1;
}
else if (motion==0)
{
buffer=0;
}
What's the value in the "else if" part? Do you really need it?
Rusttree,the supply voltage is 5V.I'm not sure the "else if" part is necessary needed for me or not because I'm not very good in writing coding,this is the coding I tried to write for it.Can you correct me?Any way to write recommended?
Rusttree,I think the "else if" part is not needed for me.I write it for testing purpose.What I want to achieve is when the alarm is activated,I can use the sensor to switch off the alarm.
That may be a problem. The digital input on RC6 needs to be at least 0.8Vdd. If Vdd is 5V, then 0.8Vdd = 4V. But you're saying the sensor typically registers just under 4V, so it may not trigger a digital high on the pin. So you have to fix that before you fix your code.
You have to two options:
1. Learn the ADC (Analog to Digital Converter) module on the PIC. There are tons of tutorials online to teach you that. This would require no external components.
2. Implement a amplifier of some kind. For example, a transistor would work well here.
Rusttree,just now I have measured the voltage of the sensor again.The voltage is greater than 4V after I have adjusted it.It is because there is a part for me to adjust the sensitivity,it can affect the voltage of the sensor.So now still need to implement an amplifier for it?
Nope, as along as it consistently reads above 4V, you're good. You can use it as a digital input on RC6.
So back to the code, you're correct, the "else if" part is unnecessary. After you wave your hand over the sensor, there's no need to set "buffer" back to 0. "buffer" will stay equal to 1 after you take your hand away, which will prevent the buzzer from activating again.
Now, there's one more thing to consider. As you have it now, the alarm will deactivate any time you wave your hand near the sensor. Even if the buzzer isn't on. Is that ok? Or do you want the sensor to deactivate the alarm only when the buzzer is on?
I hope the sensor can deactivate the alarm only when the buzzer is on.Any thing need to modify?Besides,I found one problem I'm facing now.As I set the alarm time to 1 minutes,when it reached 00:01,the buzzer does not on but after I turn off the voltage supply and turn on it again immediately,the buzzer is on.Is it unstable or is because there is a coding "if ((clkmin==alarmMin) && (clkhrs==alarmHrs) && (buffer==0))"?
If you want the alarm to deactivate only when the buzzer is on, then you'll have to move the logic that checks the sensor inside the if-statement that activates the alarm. In other words, only check the sensor when the alarm is on. See if you can figure out how to do that.
Post your latest code and let's see if we can troubleshoot this problem you're describing.
I have attached the latest code here.Everything I'm written is at the "Read RTC function" there.I have tested it many times,the alarm still not stable.Because as I said at the previous post,when the alarm time is reached the alarm is in a deactivated mode(buzzer off).Besides,I can't get the idea how to move the logic that checks the sensor.
Now, think about the conditions that are true when the alarm is on. The clock hours and alarm hours are equal, the clock minutes and alarm minutes are equal, and the motion sensor is inactive. Don't you already have an if-statement that checks for exactly those conditions? So if you moved the motion sensor logic into that if-statement, then the program will only check the motion sensor when the alarm is on.