Go Down

Topic: Invalid Use Of custom library class function (Read 50 times) previous topic - next topic

gis667en11

Hello everyone! I'm working on making my first library. It's called TimerON, and it mimics the behavior of a Rockwell PLC TON instruction. Anyways, here's the source files:

Header File

Code: [Select]
#ifndef TimerON_h
#define TimerON_h

#include "Arduino.h"

class TimerON
{

public:
 TimerON(unsigned long preset_millisecond, unsigned long inputcurrentMillis);
 bool EnableIn = 0;
 unsigned long Accumulated = 0;
 bool Done = 0;
 bool Timing = 0;
 void Reset();

private:
 bool EnableInMemory = 0;
 unsigned long enableStartMillis = 0;
};


#endif

CPP File

Code: [Select]
#include "Arduino.h"
#include "TimerON.h"

TimerON::TimerON(unsigned long preset_millisecond, unsigned long inputcurrentMillis)
{
  // If the timer is not enabled, clear the Done bit
  if(!EnableIn) {
    Done = 0;
  }

  // If the timer is being enabled for the first time now, set the starting Millis
  if(EnableInMemory == 0 && EnableIn == 1) {
    enableStartMillis = inputcurrentMillis;
  }

  // Remember previous scan's EnableIn value
  EnableInMemory = EnableIn;

  // If the timer is enabled, and not done, calculate the accumulated millis and set the Timing bit
  if(EnableIn && !Done) {
    Accumulated = inputcurrentMillis - enableStartMillis;
    Timing = 1;
  }

  if(Accumulated >= preset_millisecond) {
    Done = 1;
    Timing = 0;
  }
}

void TimerON::Reset()
{
Done = 0;
Timing = 0;
EnableInMemory = 0;
}


My Program

Code: [Select]
#include <TimerON.h>

unsigned long currentMillis = 0;
TimerON DelayStart(0,0);

void setup() {
  DelayStart.EnableIn = 1; 
}

void loop() { 
  currentMillis = millis(); 
  DelayStart.TimerON(3000,currentMillis);
}


My Error Message

Code: [Select]
Arduino: 1.8.9 (Windows 7), Board: "Adafruit Feather M0, Arduino, Off"

C:\Users\nbutler\Documents\Arduino\CB\CB.ino: In function 'void loop()':

CB:12:14: error: invalid use of 'TimerON::TimerON'

   DelayStart.TimerON(3000,currentMillis);

              ^

exit status 1
invalid use of 'TimerON::TimerON'


I have no idea why this isn't working. Any help is really appreciated.

Thanks!

UKHeliBob

Code: [Select]
DelayStart.TimerON(3000,currentMillis);
What is DelayStart ?

I cannot see an object of that name.
What did I miss ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

arduino_new

TimerOn() is a constructor. Constructor is called when the object is created.

Either set your variables when you create the object or create a new public method to set those variables.

gis667en11

DelayStart is an instance of TimerON, created before setup(). I was foolishly trying to have my constructor function also be the function that does work and requires inputs. So, I was trying to define DelayStart like this:

Code: [Select]
TimerON DelayStart(0,0);

I separated those two things so there's now an instance of TimerON that is created first, and then a function defined within that instance called Run() that takes those two millisecond time inputs and executes the timer logic.

Header File

Code: [Select]
#ifndef TimerON_h
#define TimerON_h

#include "Arduino.h"

class TimerON
{
 
public:
 TimerON();
 void Run(unsigned long preset_millisecond, unsigned long inputcurrentMillis);
 bool EnableIn = 0;
 unsigned long Accumulated = 0;
 bool Done = 0;
 bool Timing = 0;
 void Reset();
 
private:
 bool EnableInMemory = 0;
 unsigned long enableStartMillis = 0;
 
};

#endif


CPP File

Code: [Select]
#include "Arduino.h"
#include "TimerON.h"

TimerON::TimerON()
{
  
}

void TimerON::Reset()
{
 Done = 0;
 Timing = 0;
 EnableInMemory = 0;
}

void TimerON::Run(unsigned long preset_millisecond, unsigned long inputcurrentMillis) {
 // If the timer is not enabled, clear the Done bit
  if(!EnableIn) {
    Done = 0;
  }

  // If the timer is being enabled for the first time now, set the starting Millis
  if(EnableInMemory == 0 && EnableIn == 1) {
    enableStartMillis = inputcurrentMillis;
  }

  // Remember previous scan's EnableIn value
  EnableInMemory = EnableIn;

  // If the timer is enabled, and not done, calculate the accumulated millis and set the Timing bit
  if(EnableIn && !Done) {
    Accumulated = inputcurrentMillis - enableStartMillis;
    Timing = 1;
  }

  if(Accumulated >= preset_millisecond) {
    Done = 1;
    Timing = 0;
  }
}


My Program

Code: [Select]
#include <TimerON.h>

unsigned long currentMillis = 0;
TimerON DelayStart;

void setup() {
  DelayStart.EnableIn = 1;  
}

void loop() {  
  currentMillis = millis();  
  DelayStart.Run(3000,currentMillis);
}


This compiles just fine, though I can't test it right now. Does this look ok?

Go Up