create new object

Hi there,

i just wrote my own library which is fine but i have a problem with creating a new object. I tried these two versions of creating the object whereby one works and one doesn’t:

#include <EEPROM.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarmsEdited.h>

void setup(){
  Serial.begin(9600);
  TimeAlarmsEdited tae;
  Serial.println("test");
}

void loop (){
 delay (1000); 
}

This one works fine but i can’t use the tae object in the loop methode…

#include <EEPROM.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarmsEdited.h>

TimeAlarmsEdited tae;

void setup(){
  Serial.begin(9600);
  Serial.println("test");
}

void loop (){
 delay (1000); 
}

When I try to create the TimeAlarmsEdited Object outside of both function my program is still compiling but not working anymore! The Serial Monitor doesn’t even show me the “test” message!
It would be great if somebody could help me!!!

Sigh.... May we please see the offending class code. Else you may take your problem to snippets-r-us.com

It appears that, in the code that fails, you forgot to include your header file. I suspect that Delta_G saw this as you failing to post "all your code" whereas I suspect you have posted the complete sketch but overlooked this simple mistake.

Sorry for that… I thought this is enough! I edited it so you can see all the includes… And here is the library code:
TimeAlarmsEdited.h

#include "Arduino.h"
#include <EEPROM.h>
#include <Time.h>
#include <DS1307RTC.h>


#ifndef TimeAlarmsEdited_h
#define TimeAlarmsEdited_h

#define NUMBER_OF_ALARMS 2


struct Alarm {
    bool active;
    bool rings;
    tmElements_t alarmTime;
};

class TimeAlarmsEdited
{
  public:
    TimeAlarmsEdited();                
    tmElements_t currentTime();        
    void setAlarm0(tmElements_t &tm);   
    void setAlarm1(tmElements_t &tm);
    void setAlarm0(byte hour, byte minute);
    void setAlarm1(byte hour, byte minute);
    void activateAlarm0(bool active);    
    void activateAlarm1(bool active);
    bool checkAlarms();        
    bool rings();                   
  private:
    Alarm Alarms[NUMBER_OF_ALARMS];
    void getEEPROMAlarms();
    bool alreadySync;               
    void syncClock();         
};

#endif

TimeAlarmsEdited.cpp

#include "TimeAlarmsEdited.h"

TimeAlarmsEdited::TimeAlarmsEdited(){
    alreadySync = false;
    Serial.println("Konstruktor von TimeAlarmsEdited aufgerufen");
    syncClock();
}

tmElements_t TimeAlarmsEdited::currentTime(){
    tmElements_t tm;
    breakTime(now(), tm);
    return tm;
}

void TimeAlarmsEdited::setAlarm0(tmElements_t &tm){
    Alarms[0].alarmTime = tm;
    EEPROM.write(0, tm.Hour);
    EEPROM.write(1, tm.Minute);
}

void TimeAlarmsEdited::setAlarm1(tmElements_t &tm){
    Alarms[1].alarmTime = tm;
    EEPROM.write(2, tm.Hour);
    EEPROM.write(3, tm.Minute);
}

void TimeAlarmsEdited::setAlarm0(byte hour, byte minute){
    Alarms[0].alarmTime.Hour = hour;
    Alarms[0].alarmTime.Minute = minute;
    Alarms[0].alarmTime.Day = 0;
    Alarms[0].alarmTime.Month = 0;
    Alarms[0].alarmTime.Second = 0;
    Alarms[0].alarmTime.Wday = 0;
    Alarms[0].alarmTime.Year = 0;
    EEPROM.write(0, hour);
    EEPROM.write(1, minute);
}

void TimeAlarmsEdited::setAlarm1(byte hour, byte minute){
    Alarms[1].alarmTime.Hour = hour;
    Alarms[1].alarmTime.Minute = minute;
    Alarms[1].alarmTime.Day = 0;
    Alarms[1].alarmTime.Month = 0;
    Alarms[1].alarmTime.Second = 0;
    Alarms[1].alarmTime.Wday = 0;
    Alarms[1].alarmTime.Year = 0;
    EEPROM.write(2, hour);
    EEPROM.write(3, minute);
}

void TimeAlarmsEdited::activateAlarm0(bool active){
    Alarms[0].active = active;
}
void TimeAlarmsEdited::activateAlarm1(bool active){
    Alarms[1].active = active;
}

void TimeAlarmsEdited::getEEPROMAlarms(){
    setAlarm0(EEPROM.read(0), EEPROM.read(1));
    setAlarm1(EEPROM.read(2), EEPROM.read(3));
}

void TimeAlarmsEdited::syncClock(){
    Serial.println("setClock()");
    tmElements_t tm;
    RTC.read(tm);
    setTime(makeTime(tm));
    if (timeStatus() != timeSet)
        Serial.println("Unable to sync with the RTC");
}

bool TimeAlarmsEdited::checkAlarms(){
    if (!alreadySync){
        if(minute() == 0){
            syncClock();
            alreadySync = true;
        }
    }
    else if (minute() == 59){
        alreadySync = false;
    }
    bool ret = false;
    for (int i = 0; i<NUMBER_OF_ALARMS; i++){
        if (minute() == Alarms[i].alarmTime.Minute){
            if (hour() == Alarms[i].alarmTime.Hour){
                Alarms[i].rings = true;
                ret = true;
            }
        }
    }
    return ret;
}

bool TimeAlarmsEdited::rings(){
    for (int i = 0; i<NUMBER_OF_ALARMS; i++){
        if(Alarms[i].rings == true)
            return true;
    }
    return false;
}

Thank you KenF for your help but I just forgot to insert the include here… I already had it in my code! Now you should have my whole code :slight_smile:

Take your Serial.prints out of your constructor. They do not belong there. Your constructor also calls synclock that also has serial.prints.

A better approach would be to remove this functionality from your constructor and create another function (such as begin). You can then call your begin function in a similar manner to that used by the Serial class. This gives you more control over exactly when the code gets executed.