My alarm clock sketch - finally. Testers wanted.

Folks,

I have finally got it working and am "happy" with it as is.

Alas there are a couple of bugs which I can't find.

Sorry I can't work out how to include the PHI-2 and alarm clock libraries.

I have gone through and tidied it up as best I can for now.

It needs a 20x4 LCD screen to work.

Talk through:
You have to set the time and "activate" the ifdef() line/s so it sets the time in the RTC.
Then you again disable this so it runs normally.

You get a "boot screen" which is the version number then a second boot screen which shows you data about things set.

Then the clock comes to the screen.

Pressing the LEFT button it turns on the "Blanket" power relay.
Pressing this again turns it off.
The right button does the "Light" power relay.
(Same here for subsequent press of that button)

Alas sometimes when either of these buttons are pressed the date is corrupted for a couple of seconds then restores.

It can work for weeks then one day when either of these buttons are pressed, it locks up and hangs.

V10afya_Wba_x.zip (22.3 KB)

#define turn_light_on() CS.digitalWrite(1,HIGH);

Please, don't do this.

#define turn_light_on() CS.digitalWrite(1,HIGH) is far less likely to cause you grief.

You've got a lot of constant strings that should be in flash and not gobbling-up RAM.

AWOL,

Thanks very much for finding that error.

It was not clearly explained how to do it.

(Live and learn)

Yeah, I get warnings about the strings.

I shall have to learn how to fix that at some stage. But now it does seen 100% more stable, and that is good.

#define turn_light_on() CS.digitalWrite(1,HIGH)

How about:

void turn_light_on()
  {
  CS.digitalWrite(1,HIGH);
  }

Turning what should be functions into defines is not good practice.

And why on earth is Arduino.h part of your sketch? That is a system-supplied file.

Nick,

It is "there" because somewhere in the original - which I didn't write - it was there.

I haven't taken it out, because I didn't know it wasn't needed.

AWOL, and others,

Ok, that error has been addressed and it has been re-compiled.

Weird thing: It seems more unstable than before.

Although before it had the problem with the display being corrupted when I pressed the left/right button to activate the light or "blanket" output, now it seems to be crashing more frequently.

When? Well alas when I am using the light or "blanket" outputs.
The "blanket" output is now used for my phone charger. So if/when the phone needs to be recharged, as I am going to bed, I plug it in and press the left button and activate that power socket.

In the morning it is turned off automatically for me.

But now and then if the phone needs a "top up" I press the left button then the enter button.
This turns on the "blanket" output for "n" minutes. n being set in the program by the user. I think it is about 30 minutes.
All in all it works, but now and then the program crashes.
This also happens with the light output as well. Just turning it on/off as I am going to bed.

As far as I can see I am doing things right with variables, but obviously I am missing something.

Oh, this is uploaded to a Freetronics arduino called an "Eleven" which is an Uno compatible.

Anyone?

If anyone is willing to look, I have found another "problem" which has me stumped.

When an alarm goes off, it prints the number of minutes left along the top line.

To be "smart" I put some lines in like:
if (time_to_go) < 10 print("minutes to go . ") // this adds a space at the end of the line.
if (time_to_go) <2 print ("Minute to go. ") // as there is now a missing "S" from the minute/s line.

Weird thing is that it prints in TWO places on the screen.

At position 0,2 (top line, 2 places in) and position 2,0. Second row, at the left.

But ONLY when there is ONE MINUTE to go.

I shall look at the code again myself as maybe I will see the problem this time, but previous attempts have failed.

Thanks in advance.

This is an exert from the code which is the "problem":

/*
    This routine is a "countdown" timer for the alarms.
    It counts down the value ART - Alarm Run Time.
*/

int alarm_timer(int boo)
{
    static int run_flag;
    static int ART;
    static int min_called;
    char msg[18];
    int rtc[7];
    RTC.get(rtc,true);
    run_flag = boo;
    int x = 2;
    int y = 0;
    if (run_flag == 0)
    {
        ART = 0;
        return(ART);
    }
    if (run_flag == 1)
    {
        if (ART == 0)
        {
           ART = alarm_run_time;
           min_called = rtc[1];
         }
        if (ART > 0)
        {
           if (min_called != rtc[1])
           {
               min_called = rtc[1];
               ART = ART - 1;
           }
           lcd.setCursor(x,y);
           lcd.print(ART);
        }
        if (ART < 10)
        {
           lcd.print(" minutes to go. ");
        }
        if (ART > 9)
        {
           lcd.print(" minutes to go.");
        }
        if (ART == 1)
        {
           lcd.print(" minute to go. ");
        }
        return (ART);
    }
}

The routine is called with 0 or 1.

0 tells it to return how many minutes remain and 1 tells it to count down.
Yes, I can see a problem with 1 when "ART" == 0. But that isn't the problem I am seeing.

At the start you can see I set X and Y for where on the screen I want the text displayed.
All is fine until ART == 1.
Then it is displayed in two places:
0,2 and 2,0

I have changed the code a little bit to try and tidy up one of the IF() loops where it is working on the value of ART.

This is the "new" version:

        if (ART > 0)
        {

           if (min_called != rtc[1])
           {
               min_called = rtc[1];
               ART = ART - 1;
           }
           lcd.setCursor(x,y);
           lcd.print(ART);
           if (ART > 9)
           {
              lcd.print(" minutes to go.");
           }
           if (ART < 10)
           {
              lcd.print(" minutes to go. ");
           }
           if (ART == 1)
           {
              lcd.print(" minute to go. ");
           }
        }
        return (ART);
    }
}

Where - towards the bottom - I have put the "minute to go. " bit inside the if (ART > 0) loop.

I can't see the operational differences between the two.

Recompiling gives a different size - sort of understandable - but I don't know which (if either) is more correct.

Please.

          if (ART < 10)
           {
              lcd.print(" minutes to go. ");
           }
           if (ART == 1)
           {
              lcd.print(" minute to go. ");
           }

Isn't 1 less than 10? So it will print both lines.

Granted.

Hmmmm....... Ok.

I have noticed some "problems" with how the characters wrap from one line to the "next" which is actually not the next but another line down again.

But the text isn't a full line.

The message is printed at 0,2 and 2,0.

It is a 20 x 4 display.

If it was printing both messages, wouldn't they be one after the other and the second message be "wrapped" over two lines?

As it is, the same message is printed along the top line and along the "third" (line 2 zero counting) of the display.

But thanks for that info. Somehow I Missed that problem. (oops.)

So I guess I could change the line to

           if (ART < 10 && ART > 1)

and it should remove that problem - right?

What about "else"?

if (ART == 1)
  lcd.print(" minute to go. ");
else if (ART > 9)
  lcd.print(" minutes to go.");
else
  lcd.print(" minutes to go. ");

Thanks Nic.

I seem to be having a lot of blond moments these days.

Shall do as you suggested.

Ok,

I have found time to get a video of the other (on going) problem where the program hangs when the outputs are manually switched.

There are 2 outputs.
1 for the light and 1 for the "blanket".
The second one is now a "general purpose" one, but anyway.

This is the link:

Quick explanation of the display:
Time (obvious)
R.D.O. - this means the alarm is skipped for that day.
On the right there are - and "bell" symbols. These are indications of which alarms are set.
The "bolt" and "bulb" are indications that the outputs are manually active.
6 indicates the day set to skip.
the 16 (on the right) indicates the week and day now.
Then there is the date (bottom line centre) with day of week.

Please watch and see if you can work out what is going on.

It was thought that power spikes are the cause, but if "left alone" the alarms go off as requested and it doesn't have any problems.

But pressing the buttons (left/right arrows) turning the outputs on/off the problem happens.

Anyone?

Just in case:

The clock and the two outputs are on the I2C bus.

I remember putting resistors (pull up) on it.

I don't remember the values just now. I can get them by looking. But that means pulling apart the clock.
No big, but anyway.

Looking at the video, I am now starting to think that the bus is being corrupted and that is why it hangs.

The cable isn't that long and I can't say for the RF noise around it.

What I may try is removing the resistors and see what happens.

But I am still wanting someone else's thoughts on the problem.

It isn't that I want to "double post" but I have done work and I think that I have found where the problem is: With the I2C bus.

This is my "original" post:
http://forum.arduino.cc/index.php?topic=176854
My alarm clock sketch - finally. Testers wanted

I posted a video of the problem happening and now watching it myself I notice that the digits displayed are like the ones I saw when I was first making this project when the I2C bus addresses were set wrong.

So thinking about it I wonder:
Are the clock and the other IC on the bus talking at the same "speed"?

Because it would seem that it only plays up when I am forcing the events to happen.
If left to the program's functioning, it works ok.

I pulled apart the project and noted that I HAD 4k7 pull up resistors to the +5 line at the REMOTE end of the bus.

Removing them didn't fix the problem. :frowning:

So would that mean they should be SMALLER (lesser) values?

But the clock works fine all other time. Which makes me ask about the speeds.

The code is in that other post. Alas libraries are not included, but are common and easy to get.

Any help welcome.

Hi again.

I have been looking through the code with other "problems'.

There is a "alarm defer" option so that if you wake up before the alarm, you can press the button to make it skip that alarm for that ONE time and do what you have to do as per usual.

There is also a "Skip day" which allows a particular day to be skipped every two weeks.

Recently I have been waking up early and the "one time skip alarm" seems to be working.
I usually am not around to check this but weekends are a good time to notice the alarm doesn't go off.

Alas just recently I have noticed a small bug with the logic:
IF it is the day BEFORE the skip day, and I wake up early and press the DEFER button, the alarm still goes off.

Any other day it doesn't.

I am looking at the code and can't really see anything which would make this happen.
The lines are pretty well discreet in what they are doing, so I am suspicious that I am missing something else with the "loops" of how the logic is grouped.

I can post the relevant parts of the code if someone doesn't mind looking, to save you looking through the WHOLE code.

Thanks in advance.