Arduino sketch hangs

Hi,
I am developing dozing pumps system. It is finished and working. Currently I am adding data about when each pump was fired and the program runs in some endless loop

Problem is with the method, that has to search last time the pump fired.

LastTimeOn *findLastTimeOn(Pump *pump)

The serial data is crazy.

This is serial data:

START
clearing.. bbb
foundDS!
CHECK4CHECK6foundDS!
CHECK4CHECK6foundDS!
CHECK4CHECK6foundDS!
CHECK4CHECK6foundDS!
CHECK4CHECK6HAHA!
17
18
HAHA2!
Found pump with correct hour and minute!
CHECK1STARTING pump!
clearing.. bbb
TEST1 0.00Found pump with correct hour and minute!
CHECK1guuuuuuuuuuuuuuuuuuuuuuujuuuuuuuuuuuuuuuuuuu(endless uuuuuuu text)
[code]
[b]I attach the sketch, [/b]

[/code]

Why it is saying uuuuuuuuuuuuuuuu endlesly…

LastTimeOn *findLastTimeOn(Pump *pump) {

    Serial.print("CHECK1");

    if (pump->lastTimeOn == NULL) {
        Serial.print("CHECK1return");
        return NULL;
    }

    LastTimeOn *lastTimeOn = pump->lastTimeOn;
    while (lastTimeOn->next != NULL && lastTimeOn->hour != NULL && lastTimeOn->hour >= 0) {
        if (lastTimeOn->next != NULL) {
            Serial.print("CHECK1bTR" + lastTimeOn->hour);
        } else {
            Serial.print("CHECK1bFL" + lastTimeOn->hour);
        }
        lastTimeOn = lastTimeOn->next;
        if (lastTimeOn->next != NULL) {
            Serial.print("CHECK1cTR" + lastTimeOn->hour);
        } else {
            Serial.print("CHECK1cFL" + lastTimeOn->hour);
        }
    }
    Serial.print("CHECK3" + lastTimeOn->hour);
    return lastTimeOn;
}

Most important classes

class LastTimeOn{
public:
    //char time[30];
   // char hour[30];
    unsigned int dayOfWeek;
    unsigned int hour;
    unsigned int minute;
    //LastTimeOn(char time1[], char hour1[]);
    LastTimeOn(unsigned int minute1, unsigned int hour1, unsigned int dayOfWeek1);
    LastTimeOn();
    LastTimeOn *next;
};

LastTimeOn::LastTimeOn(unsigned int minute1, unsigned int hour1, unsigned int dayOfWeek1) {
   /* strcpy(time,time1);
    strcpy(hour,hour1);*/
    minute = minute1;
    hour = hour1;
    dayOfWeek = dayOfWeek1;
    next = NULL;

};
LastTimeOn::LastTimeOn() {
    next = NULL;
    minute = NULL;
    hour = NULL;
    dayOfWeek = NULL;
}

class Pump {
public:

    unsigned int pompNo;
    boolean alwaysOff;
    float oneMiliSeconds_100;
    DaySettings *nextDay;
    Pump *next;
    Pump(unsigned int no);
    LastTimeOn *lastTimeOn;
    Pump();
};

Pump::Pump(unsigned int no)
{
    //  nextDay  = nextDayy
    alwaysOff = false;
    oneMiliSeconds_100 = 1.0;
    pompNo = no;
    lastTimeOn = NULL;
}

Pump::Pump() {
    lastTimeOn = NULL;
}

TouchScreenMenuTestCUSTOM103.ino (46.1 KB)

I attach the sketch

Oh no you didn't

I edited, see it now.

arduHenry: I edited, see it now.

First of all, don't make changes to existing posts that make comments on the original seem wrong. It will just confuse other readers. Add new data in a new Reply.

Second, post a complete program. If the problem was in the part of the code where you think it is you would probably have found it.

...R

Ok, sorry, now I’ve attached whole library folder. Arduino arduino-1.5.5-r2, in 1.8 it doesnt compile. The sketch is in

libraries\TouchScreenMenuTestCUSTOM103

libraries2.zip (1.03 MB)

Serial.print("CHECK3" + lastTimeOn->hour);

That's not how you concatenate strings in C++. You should use two separate print statements.

arduHenry: Ok, sorry, now I've attached whole library folder

Please make it easy to help you. Just post your .ino file. Add it as an attachment if it is too long to include in your Reply.

People (including me) don't like downloading strange ZIP files.

...R

But you have the ino file attached in the first post.. I think I've found the problem, wrong way of creating new LastTimeOn object:

should be:

pump->lastTimeOn = new LastTimeOn(minute, hour, dayOfWeek);

instead of:

pump->lastTimeOn = &LastTimeOn(minute, hour, dayOfWeek);

and the strange serial behaviour was tha couse of concatenating strings in a wrong way. Thanks!

> arduHenry:
> I think I’ve found the problem, wrong way of creating new LastTimeOn object:
>
> should be:
>
>
> </sub> <sub>> pump->lastTimeOn = new LastTimeOn(minute, hour, dayOfWeek);</sub> <sub>>
if creating a new heap object that isn’t going to exist for the entire life of the program, be sure to call delete.
your code is too complex to see what you are really doing wrong.
Have you tried saving a simple timestamp when the pump is last run?

Timestamp could be easier, but I went that way to compare lastTimeOn data with each pump settings and draw the main panel with minutes and hours (green means before run - settings time doesnt match lastTimeOn, red means after run - a pump has lastTimeOn time that matches settings for that day) -> JPG: https://pasteboard.co/H8dZPFi.jpg

I forgot about deletion... Now I am trying to remove all LastTimeOn info for each pump every midnight so that the front panel where you can see which pump where fired would turn green again.

In case of a queue with nested objects behind next pointer and no back pointer, do I have to remove all child objects ? delete pump->lastTimeOn or this:

void clearLastTimeOn(Pump *pump1) {
    while (pump1->lastTimeOn != NULL) {
        LastTimeOn *lastTimeOn = pump1->lastTimeOn;
        while (lastTimeOn->next != NULL) {
            if (lastTimeOn->next->next == NULL){

            }
            lastTimeOn = lastTimeOn->next;

        }
        lastTimeOn = NULL;
    }
}

Previous version of my dozer: https://www.youtube.com/watch?v=Z1bpOX8MpJI

arduHenry: Previous version of my dozer: https://www.youtube.com/watch?v=Z1bpOX8MpJI

Doser code that works: https://github.com/delta-G/Disco_Doser

arduHenry: In case of a queue with nested objects behind next pointer and no back pointer, do I have to remove all child objects ?

again, if the object is meant to exist the entire lifetime of the program, then deleting behind new is not necessary.

if not, and you are dynamically creating objects then failure to use delete will result in a memory leak.