CLASS - stuck beyond help.

I am helping another person with a sketch which is based on my sketch/alarm clock.

I've asked "LUIDR" but haven't got a reply yet.

But when I take apart the program I am stumped where "CLOCK1" comes from.

Yes, that is really "CLOCK" but as it is ...... used it becomes appended with the 1.

Where I am now in my "journey" I am at a cross road. Some advocate I learn C only. To get a good ground knowledge of the syntax and how things work.

I agree, but others are saying C++. But with that comes the problems of CLASS etc. Which may be beyond me at this point.
Yeah, so why I am doing this you ask? Well, I pretty well have little choice. Although I have my sketch working, I would like to help someone with what I know.

Yes, this is a real "hack job" with what I have done to it to (try) and help the guy.

Surprisingly we are making ground!

To get around it I have written a new alarm routine which is called and it returns a variable to do the "alarm_is_on" thingy.

I don't know how to call any of the sub-set of commands in the "ALARM_CLOCK" which is declared in the file ALARM_CLOCK.H
(I am putting things like names in uppercase for clarity only - I hope.)

I don't understand these lines like:
byte alarm_clock::run()

This is a sub-command of alarm_clock, but unless I am already "in" alarm_clock, (don't know what that means really, but indulge me) I can't call it.

I can see how it is invoked, but I don't know how to invoke it (for example) from one of MY routines.

class alarm_clock
{
public:
alarm_clock(boolean noo);
byte run(); // Returns which alarm is triggered, 0-Max_alarms, 255 means no alarm is triggered.
byte set_alarm(byte alarm_num, byte hr, byte mnt, byte dow); // Returns 255 if parameters are invalid. Returns 0 if valid.
void turn_on(byte alarm_num);
void turn_off(byte alarm_num);
void alarm(int active_alarm);
boolean within(byte a, byte dow);

I can see that this makes "alarm_clock" and it allows me to do things like:
run()
set_alarm(....)
turn_on(byte)
etc.

Well, it sort of work.s

How do I turn on alarm # 1?
I am guessing

turn_on(1);

But that doesn't work.
A couple of efforts on syntax yield little extra that errors:

void foo()
{
turn_on(1);
}

My_routines:759: error: 'turn_on' was not declared in this scope

Which I would expect.

I don't mind trying things, but I don't know how to phrase the question to "Mr GOOGLE" so am trying the human path.

Coffee_Maker_Alarm_7.zip (14.2 KB)

I can see that this makes "alarm_clock" and it allows me to do things like:

No. It defines a class of objects. It is saying, "every alarm clock in my system knows how to run, can be turned on and off, etcetera."

This creates a single alarm clock (creates an instance) named oneonmydesk...

alarm_clock oneonmydesk(false);

Once you have created an alarm clock then you can send it commands...

oneonmydesk.run(); // tell my alarm clock to run

oneonmydesk.set_alarm( 1, 2, 3, 4 ); // ask my alarm clock to change the alarm settings

Ok, I am starting to understand.

But in the code/sketch: Where is CLOCK1 named?

Not wanting to be difficult, you may need to use examples specific to the sketch so I can see where they are happening then I can expand/speculate on how to use them other ways.

:slight_smile:

In "example_menu" there is this bit of code:

  int temp1;
  while (1)
  {

    clock1.run();
    if (!clock1.alarm_is_on)
    {
      temp1=wait_on_escape(1000);

So ok, what I understand:
do forever:
clock1.run() -- do the clock. Still a bit vague, because I still can't find where it is declared.

If "alarm_is_on" is not set (from clock1) do what is in the { }'s.

so to access the ALARM_CLOCK function "alarm_is_one", I pre-cede it with "clock1."

Right?

But still can't find where CLOCK1 happens.

lost_and_confused:
But in the code/sketch: Where is CLOCK1 named?

Coffee_Maker_Alarm_7.ino line #155.

From what I understand you meant, I tried this:

void foo()
{
  clockl.turn_on(1);
}

My_routines:759: error: 'clockl' was not declared in this scope

So I am still missing something.
(Oh and that is a ONE not a lower case L - I don't like this font for that reason of ambiguity.)

lost_and_confused:
clock1.run() -- do the clock. Still a bit vague, because I still can't find where it is declared.

It's not. I doubt Example_menu.ino will compile.

If "alarm_is_on" is not set (from clock1) do what is in the { }'s.

Exactly.

so to access the ALARM_CLOCK function "alarm_is_one", I pre-cede it with "clock1." Right?

Yes.

But still can't find where CLOCK1 happens.

It doesn't. Having all those dot-ino files in the same directory was very likely a mistake. The only one that should be in that directory is Coffee_Maker_Alarm_7.ino.

Greatest apologies.

I even did a search for that and it didn't find it.

:frowning:

Coding badly,

Thanks very much for taking the time to walk me through this.

Yeah, originally they were .PDE files I think.

But somewhere they became .ino files.

The only one I try to compile is the "coffee_maker_V7.ino" file.

I suspect the IDE changes the names of the files from .PDE to .INO for reasons only known to itself.

I seem to be still stuck with something.

  int temp1;
  while (1)
  {

    clock1.run();
    if (!clock1.alarm_is_on)
    {

Ok, these two lines - of interest - are CLOCK1.blah_blah
So it knows the commands are a sub-set of clock1

But in the file alarm_clock there is this:

byte alarm_clock::run()
{
  int rtc[7];
  render_RTC(clock_style);
  RTC.get(rtc,true);
  for (int i=0;i<Max_alarms;i++)
  ///////////////////////////////
  {
    if ((alarms[i].hr==rtc[2])&&(alarms[i].mnt==rtc[1])&&(rtc[0]==0)&&within(i,rtc[3]))
    {
      alarm_is_on=true;
    }
  }
  if (alarm_is_on) alarm();

So, ok "clock1.run()" - as it is now known - is called. It had to be called clock1.run() and couldn't just be run() for reasons mentioned and understood. (I think.)

But then there are the lines:
alarm_is_on = true;
and
if (alarm_is_on) alarm();

Why don't they have to have the clock1 pre-fix?
Now, before you say what I am sure you are going to say, I shall re-word the question:
Why don't they have to be prefixed with alarm_clock?

I don't see where the code goes "inside" the class and therefore can "see" the sub-set of commands without the prefixes.

Ok, sorry it probably falls under the title of "still outside your understanding". I accept that. But I am just wondering.

Update:

Got to the next level of understanding.

Curse the IDE!

The font used IS problematic with how certain letters/characters are displayed.

l and 1 are the same.

Yeah, silly mistake and should be easy to see if you know what you are looking for.

Alas where I was, I wasn't and didn't.

Got things working a lot better and have removed the "my_alarm" routine as now I can use the existing one.

Progress!

Thanks to all who helped!

You can change the font. It's just an entry in the preferences file. This:

editor.font=Lucida Console,plain,12

works fine on Windows, and differentiates between 1 and l (see image attached). Change the 12 if you prefer a different size. Experiments have shown that TrueType fonts are supported, if you like that sort of thing.

There is a separate entry for OS X:

editor.font.macosx=Monaco,plain,10

There doesn't seem to be an entry for Linux, so I presume it uses one of the others.

lost_and_confused:
But then there are the lines:
alarm_is_on = true;
and
if (alarm_is_on) alarm();

Why don't they have to have the clock1 pre-fix?

Technically, they do. In the call to run, there is an implied variable named "this". In your example, this and clock1 are the same. So this line of code...

alarm_is_on = true;

...is really...

this->alarm_is_on = true;

...which in your example is essentially...

clock1.alarm_is_on = true;

Why don't they have to be prefixed with alarm_clock?

alarm_clock is a class. In C++ a class is like a encyclopedia definition. It tells you what something is, how it behaves, what attributes it has but it is not the actual thing. An instance (clock1) is the actual thing. You wouldn't go to your encyclopedia (alarm_clock) to change alarm settings. You would go to one of your alarm clocks (clock1).

Would you believe I was looking for that and couldn't find it.

I think I may need my eyes tested.

dxw00d:
You can change the font. It's just an entry in the preferences file. This:

editor.font=Lucida Console,plain,12

works fine on Windows, and differentiates between 1 and l (see image attached). Change the 12 if you prefer a different size. Experiments have shown that TrueType fonts are supported, if you like that sort of thing.

There is a separate entry for OS X:

editor.font.macosx=Monaco,plain,10

There doesn't seem to be an entry for Linux, so I presume it uses one of the others.