user23455: I'm new to the Arduino world. The last time i was programming microprocessors, was back with the Z80 in assembly language.
I am also teaching myself C++ and seems to be going quite well.
I have decided to write my own 'library' for driving 7 segment LED's. The reason for writing my own, rather then using an existing one is to aid with the learning process. I'm quite pleased to say it works ( it may not be the most efficient code)
The books I have on C++ are great, but i do find sometimes the explanation of concepts seem to get lost.
what I would like to know, how do you go about initialising an object class in setup() and using it in loop()? I have this sneaky suspicion that it involves creating the object on the heap, and passing a pointer to the object over somehow.
I could just initialise it once in loop(), and make my own continuous loop afterwards, but there are other things done between the and of a loop() and the beginning of the next loop() (things like timer interrupts).
I will post the code im using, but not the library unless asked to, as it is quite a few lines.
# include "Leddisplay.h"
void setup() {

    byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };

    Leddisplay *pRightDigit = new Leddisplay(Digit);



}

void loop() {

    pRightDigit->setDigit(5);
    pRightDigit->displayDigit();

}

^^  Obviously this does not compile  ^^
# include "Leddisplay.h"
void setup() {





}

void loop() {


    byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
    Leddisplay *pRightDigit = new Leddisplay(Digit);

    pRightDigit->setDigit(5);
    pRightDigit->displayDigit();

    for (;;);   // or do.. while within which has other code to repeat
                // but wont allow anything to be executed between loop() iterations

}

^^  This does, and runs fine, but may not achieve what I want  ^^
I'm not sure if I have used the right wording, or even asked the right question.
Thank you for any help in advance.
Dave

user368: 
... how do you go about initialising an object class in setup() and using it in loop()?

You declare it in a global context so that it's accessible by both.
But that's not quite what you want here:
#include "Leddisplay.h"

byte Digit[] = {...};
Leddisplay RightDigit(Digit);

void setup() {
}

void loop() {
    RightDigit.setDigit(5);
    RightDigit.displayDigit();
}


user4143: What you have to understand is the concept of scope.
Everything (well, almost everything) in C and C++ is enclosed in { and }. Those define a scope, and everything defined within that scope is available to any other scopes defined within that scope.
You can also assume that the whole sketch is surrounded by { and }. This scope is called the global scope.  Anything defined within it is available anywhere in your sketch.
So by defining the object in the global scope it is available everywhere:
# include "Leddisplay.h"

Leddisplay *pRightDigit;

void setup() {
    byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
    pRightDigit = new Leddisplay(Digit);
}

void loop() {
    pRightDigit->setDigit(5);
    pRightDigit->displayDigit();
}

However the use of new etc is discouraged in low-memory systems. Better is to statically define the object:
# include "Leddisplay.h"

const byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
Leddisplay pRightDigit(Digit);

void setup() {
}

void loop() {
    pRightDigit.setDigit(5);
    pRightDigit.displayDigit();
}

Note that, when statically defined like this, things in the constructor aren't guaranteed to be run at the right time. They should be moved into a .begin() function that you then call from setup():
# include "Leddisplay.h"

const byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
Leddisplay rightDigit(Digit);

void setup() {
    rightDigit.begin();
}

void loop() {
    rightDigit.setDigit(5);
    rightDigit.displayDigit();
}


23455: <p>I'm new to the Arduino world. The last time i was programming microprocessors, was back with the Z80 in assembly language.</p>

<p>I am also teaching myself C++ and seems to be going quite well.</p>

<p>I have decided to write my own 'library' for driving 7 segment LED's. The reason for writing my own, rather then using an existing one is to aid with the learning process. I'm quite pleased to say it works ( it may not be the most efficient code)</p>

<p>The books I have on C++ are great, but i do find sometimes the explanation of concepts seem to get lost.</p>

<p>what I would like to know, how do you go about initialising an object class in setup() and using it in loop()? I have this sneaky suspicion that it involves creating the object on the heap, and passing a pointer to the object over somehow.</p>

<p>I could just initialise it once in loop(), and make my own continuous loop afterwards, but there are other things done between the and of a loop() and the beginning of the next loop() (things like timer interrupts).</p>

<p>I will post the code im using, but not the library unless asked to, as it is quite a few lines.</p>

<pre><code># include "Leddisplay.h"
void setup() {

    byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };

    Leddisplay *pRightDigit = new Leddisplay(Digit);



}

void loop() {

    pRightDigit-&gt;setDigit(5);
    pRightDigit-&gt;displayDigit();

}
</code></pre>

<p>^^  Obviously this does not compile  ^^</p>

<pre><code># include "Leddisplay.h"
void setup() {





}

void loop() {


    byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
    Leddisplay *pRightDigit = new Leddisplay(Digit);

    pRightDigit-&gt;setDigit(5);
    pRightDigit-&gt;displayDigit();

    for (;;);   // or do.. while within which has other code to repeat
                // but wont allow anything to be executed between loop() iterations

}
</code></pre>

<p>^^  This does, and runs fine, but may not achieve what I want  ^^</p>

<p>I'm not sure if I have used the right wording, or even asked the right question.
Thank you for any help in advance.
Dave</p>
 368: <blockquote>
  <p>... how do you go about initialising an object class in setup() and using it in loop()?</p>
</blockquote>

<p>You declare it in a global context so that it's accessible by both.</p>

<p>But that's not <em>quite</em> what you want here:</p>

<pre><code>#include "Leddisplay.h"

byte Digit[] = {...};
Leddisplay RightDigit(Digit);

void setup() {
}

void loop() {
    RightDigit.setDigit(5);
    RightDigit.displayDigit();
}
</code></pre>
 4143: <p>What you have to understand is the concept of <em>scope</em>.</p>

<p>Everything (well, almost everything) in C and C++ is enclosed in <code>{</code> and <code>}</code>. Those define a <em>scope</em>, and everything defined within that scope is available to any other scopes defined within that scope.</p>

<p>You can also assume that the whole sketch is surrounded by <code>{</code> and <code>}</code>. This scope is called the <em>global scope</em>.  Anything defined within it is available anywhere in your sketch.</p>

<p>So by defining the object in the <em>global scope</em> it is available everywhere:</p>

<pre><code># include "Leddisplay.h"

Leddisplay *pRightDigit;

void setup() {
    byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
    pRightDigit = new Leddisplay(Digit);
}

void loop() {
    pRightDigit-&gt;setDigit(5);
    pRightDigit-&gt;displayDigit();
}
</code></pre>

<p>However the use of <code>new</code> etc is discouraged in low-memory systems. Better is to <em>statically define</em> the object:</p>

<pre><code># include "Leddisplay.h"

const byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
Leddisplay pRightDigit(Digit);

void setup() {
}

void loop() {
    pRightDigit.setDigit(5);
    pRightDigit.displayDigit();
}
</code></pre>

<p>Note that, when statically defined like this, things in the constructor aren't guaranteed to be run at the right time. They should be moved into a <code>.begin()</code> function that you then call from <code>setup()</code>:</p>

<pre><code># include "Leddisplay.h"

const byte Digit[9] = { 22, 23 , 24 ,25, 26, 27, 28, 29, 2 };
Leddisplay rightDigit(Digit);

void setup() {
    rightDigit.begin();
}

void loop() {
    rightDigit.setDigit(5);
    rightDigit.displayDigit();
}
</code></pre>
