Go Down

Topic: Initializing an array of a custom class (Read 14 times) previous topic - next topic

dochawk

I worked my way through creating a library, and can now successfully include it.

This one is for LEDs--uninteresting, but a useful step on the way to relay management.

I create both a base dhDevice class and then a dhLed class:

Code: [Select]

#ifndef Test_h
#define Test_h

#include "Arduino.h"


class dhDevice
{
public:
       dhDevice();
};

class dhLed: dhDevice
{
        public:
                dhLed(byte thePin);
                //void init(byte outPin);
                bool status();
                void turnOn(unsigned long turnOffTime=-1);
                void turnOff();
                unsigned long offTime();
                void update();
                byte pinReport();
        private:
                bool myStatus=false;
                unsigned long myOffTime;
                byte myOutPin;



};



#endif




and

Code: [Select]

// dhMegaLib.  dochawk's library of useful things for atMega projects.
//
// Copyright MMXIX


// a root device class from which to derive others

#include "dhMegaLib.h"

dhDevice::dhDevice(){}

dhLed::dhLed(byte thePin) : myOutPin(thePin){ }

void dhLed::turnOn(unsigned long turnOffTime){
              digitalWrite(myOutPin,HIGH);
              if (turnOffTime<-1){
                      myOffTime=turnOffTime;
              } else {
                      //set to leave on indfinitely
                      myOffTime=-1;

              }
      }

void dhLed::turnOff(){
              digitalWrite(myOutPin,HIGH);
              myStatus=0;
      }

unsigned long dhLed::offTime(){
              return myOffTime;
      }

void dhLed::update(){
              //update the current lights
              //for the moment, this assumes millis() will never overflow
              if( (myStatus==true) && (millis()>=myOffTime)) {
                      turnOff();
              }
        }

byte dhLed::pinReport() {return myOutPin;}


These were done with vi, so the formatting  may be a bit different than usual.

And then a simple sketch to load them up:


Code: [Select]


#include "dhMegaLib.h"


dhLed myLeds[2]=(5, 6, 7);


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("begun");

  for (byte i = 0; i <= 2; i++) {
    Serial.print(i); Serial.print(": "); Serial.println(myLeds[i].pinReport());
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}



But when I check the serial monitor, I get

Code: [Select]
begun
0: 7
1: 7
2: 0


I would expect, however, to see 5,6,7.

I'm betting that I'm missing something rather basic here . . .

gfvalvo


Go Up