Go Down

Topic: Different ways of initialization my object, gives different behaviour.(hung up) (Read 31 times) previous topic - next topic

kajnorman

I have made a class called ClockDisplay.

Initializing like seen below works beutifull:

Code: [Select]
#include <ClockDisplay.h>
ClockDisplay*  clockui; 
int seconds;

void setup() { 
  clockui= new ClockDisplay(12,11,10);
}

void loop() {
    delay(1000);
    clockui->Display(seconds/60,seconds%60);
    seconds++;
}


It Compiles downloads and runs (I can se the clock ticking on my display).

------------------------------------------------------------------------------------------

However this code gives me a headache...

Code: [Select]
#include <ClockDisplay.h>
ClockDisplay  clockui(12,11,10); 
int seconds;


void setup() { 
  Serial.begin (115200);
  Serial.println("setup running");
}


void loop() {
    delay(1000);
    clockui.Display(seconds/60,seconds%60);
    seconds++;
      Serial.println("loop running");
}



  • It Compiles and downloads without warnings
  • I inserted some printlns but they never execute. (nothing on the console(neither from setup nor loop))
  • So it looks like it somehow "hangs up" in my constructor.




The constructor of my object looks like this:
(It wraps an ledControl...)

Code: [Select]

ClockDisplay::ClockDisplay(int dataPin, int clkPin, int csPin) {
   lc= new LedControl(dataPin,clkPin,csPin,2);
  for(int address=0;address<2;address++) {
    /*The MAX72XX is in power-saving mode on startup*/
    lc->shutdown(address,false);
    /* Set the brightness to a medium values */
    lc->setIntensity(address,8);
    /* and clear the display */
    lc->clearDisplay(address);
    delay(100);
    lc->setAll(address);
    delay(100);
    lc->clearDisplay(address);
  };
}



I am looking forward for any kind of help, that may show me in what direction to go :-)

Delta_G

Your constructor contains lines that shouldn't be run before the hardware has been set up by the init() function in main.  If you put it up there at global scope then you are asking your constructor to run before hardware is ready. 

Constructors are only for setting initial values of variables or things like that.  You should never be doing anything with hardware there.  You certainly don't call delay there.   The delay will likely hang things because it is waiting for the timer 0 interrupt to advance millis and that interrupt hasn't been turned on yet.  So it waits forever for something that isn't going to happen.

If you need to setup hardware for your class then you need a begin method that you can call from setup like the HardwareSerial class has with Serial.begin()
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

kajnorman

Thanx alot Delta_G.

This was somehow expected, but very usefull. I need to read up on init function called from main.
I feel a bit embarassed by calling delay() before it is initialised,  :-| 

Can You recomend a source for this kind of information ?

Delta_G

Can You recomend a source for this kind of information ?

No better source than simply looking at the source code in the core libraries.  Start with main.cpp and work your way out from there. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up