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
Leddisplay *pRightDigit
variable outside of the scope of these functions and in the global space. – Dave X Jun 29 '16 at 4:47setup(){} loop(){ int a = 0; while(1) { a++; } }
– Talk2 Jun 29 '16 at 5:34loop
! The will interrupt the current loop code, wherever it currently is, run the ISR, and then return back to where it was in theloop
. The only thing that is handled after theloop
is, the arduino code check is there is any serial data available, and calls the functionserialEvent
if it is defined. If you aren't using this function (which is kind of silly anyways), you can use afor(;;;)
orwhile(1)
without any problems. – Gerben Jun 29 '16 at 8:59