PaulS:
Here is my energyMeter.cpp code:
Did you bother trying to compile that before you posted it?
Yes, I compiled it and I get some error message that I am currently trying to sort out.
PaulS:
Why are you (trying) to define all those global variables in the source file? A class is SUPPOSED to encapsulate data, not provide more opportunities to screw things up.
I am going to create 16 of these objects + 22 other objects in my sketch hence i would like to move every test sketch i have made to its own class instead of copying it in 38 times.
So whats the best way to define constant for an object, and the second thing how can I make constant shared between objects but still define them when i create the object?
PaulS:
By convention, all capital letter names are reserved for constants. Constants never appear on the left of an equal sign (except when first declared and valued).
So these are all constants:
long PULSE_FACTOR // Nummber of blinks per KWH of your meeter
boolean SLEEP_MODE // Watt-value can only be reported when sleep mode is false.
long MAX_WATT // Max watt value to report. This filetrs outliers.
int DIGITAL_INPUT_SENSOR // Usually the interrupt = pin -2 (on uno/nano anyway)
int CHILD_ID // Id of the sensor child
unsigned long SEND_FREQUENCY
So when I create the object I want to initialize these CONSTANT that never changes in my update method which are called inside the loop in my main sketch.
Currently I only want to set CHILD_ID when I create the object, the rest of the constant should be optional.
Coming from vb background what I am used to is:
EnergyMeter power(CHILD_ID:=1,MAX_WATT=10000);//initialize an instance of the class
where I just specify which one of the optional parameter I would like to set.
So all these CONSTANT should be only constants for that object, but MAX_WATT and PULSE_FACTOR could be shared across objects, if they are not given as input the constructor for the objects.
I am trying to read up on how c++ threats constants, and what volatile do and what should be defined in the header file and what should be defined inside/outside the constructor. But I am a bit lost and the official class tutorial for arduino does not threat constants at all: http://www.arduino.cc/en/Hacking/LibraryTutorial
and these are variables(that changes in my update method) for the class object:(small letters..)
double ppwh
boolean pcReceived = false;
volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0;
volatile unsigned long watt = 0;
unsigned long oldPulseCount = 0;
unsigned long oldWatt = 0;
double oldKwh;
unsigned long lastSend;
In normal arduino code you pub both variables and constant at the top of your sketch, however it seems to be different in cpp files.