Hello all
I need to make many classes with same method and properties but with out using instant definition
because I do not want the variables inside the class to change. So I need make clone of class.
thanks for fast reply.
My project is to detect if analog input is greater than limit value for 5 sec and if its greater than anther limit for 10 sec and for 15 sec also.
the first problem is how to pass the the variable value (CurAMP) to the class ,as in my sketch its always zero.
the second problem is when I hold the value of millis() in static variable it will be change whenever use the class (even for anther limit or time ).
int CurAMP; //main analog input
class xTimer
{
public:
xTimer(int MainIn,int Uper,int Lower);
boolean TrgTimerM(int TimeM); //if IN greater than Ref for specific period (TimeM) return true
private:
int _MainIn,_Uper,_Lower;
};
xTimer::xTimer(int MainIn,int Uper,int Lower)
{
_MainIn = MainIn;
_Uper = Uper;
_Lower = Lower;
;}
boolean xTimer::TrgTimerM(int TimeM) //if more than for spasific period (TimeM) return true
{
static unsigned long StartingTime; // hold value of previous millis()
if (_MainIn > _Uper) {if((millis()-StartingTime) > (TimeM *1000)){return true;} else{return false;};}
else { StartingTime = millis();return false;}
;}
xTimer CurntLimit(CurAMP,500,400);
xTimer CurntLimit2(CurAMP,600,400);
xTimer CurntLimit3(CurAMP,700,400);
void setup() {
Serial.begin(9600);
}
void loop() {
CurAMP=analogRead(A0);
if (CurntLimit.TrgTimerM(5)){ Serial.print("The value more than 500 for 5 sec ");}
if (CurntLimit2.TrgTimerM(10)){ Serial.print("The value more than 600 for 10 sec");}
if (CurntLimit3.TrgTimerM(15)){ Serial.print("The value more than 700 for 15 sec");}
Serial.print("(");
Serial.print(CurAMP);
Serial.println(")");
delay(250);
}
the first problem is how to pass the the variable value (CurAMP) to the class ,as in my sketch its always zero.
You declared it as a global variable. Then, you pass it's value to the constructors, which copy the value to _MainIn. From the on, the class methods use the value of _MainIn (which was 0 when the constructor was called). The class instances are not bound to the variable CurAMP and they don't access the current value of CurAMP, so, the behavior you are observing is exactly what I expect.
Your class instances are going to need to periodically read the value of CurAMP, a static class member, or, the value of a pin they know about. To do that, you will need to have the class set up a timer interrupt to happen as needed, and in the timer callback function, update a static variable.
CurAMP should be removed from your code. The class instances should NOT be accessing a global variable.
thanks you guide me to the right way ,but about the static variable which accumulate the time it will change by any instant that deal with the class and this is not right ,any instant must have its Unique
variable to accumulate time separately. otherwise each instant will change the values of anther one.
A static in a class works a bit different then in a function. A static in a class mean, same for all objects of this class. The normal class variables don't get lost every loop because you don't destroy the object. So that's what you want to use. (like you do for uper (which is really a lower threshold)).
My edit:
int CurAMP; //main analog input
class xTimer{
public:
xTimer(int uper);
boolean TrgTimerM(int source, unsigned int secondsPassed); //if IN greater than Ref for specific period (secondsPassed) return true
private:
int _MainIn,_uper,_Lower;
unsigned long startingTime;
};
xTimer::xTimer(int uper){
_uper = uper;
}
boolean xTimer::TrgTimerM(int source, unsigned int secondsPassed){ //if more than for spasific period (secondsPassed) return true
if (source > _uper) {
return (millis() - startingTime) > (1000UL * secondsPassed);
}
else{
startingTime = millis();
}
return false;
}
xTimer curntLimit(500);
xTimer curntLimit2(600);
xTimer curntLimit3(700);
void setup() {
Serial.begin(9600);
}
void loop() {
int curAMP = analogRead(A0);
if (curntLimit.TrgTimerM(curAMP, 5)) Serial.print("The value more than 500 for 5 sec ");
if (curntLimit2.TrgTimerM(curAMP, 10)) Serial.print("The value more than 600 for 10 sec");
if (curntLimit3.TrgTimerM(curAMP, 15)) Serial.print("The value more than 700 for 15 sec");
Serial.print("(");
Serial.print(CurAMP);
Serial.println(")");
delay(250);
}
I
changed some variable names to something logic
fixed the millis overflow problem.. (1000UL part)
(non const) variables don't start with a capital
fixed indentation (it was a mess!)
Removed the lower variable because it was not yet in use. Now you may edit it in again
Static variables inside a class are usually used for class properties that are shared among all the instantiations (objects). A classic example is a variable to keep track of how many such objects exist.
For an object, if you want to change one of its values apart from the constructor, a common way is to create a set() method for it.
All the (non static) variables of a class are linked to the object. So if you make the object a global (xTimer curntLimit(500); outside functions) you never destroy the object so it lives on and so does all the variables.
Would you create the object inside the loop() the object would live for one iteration of loop. So all the variables would also only live for one iteration of the loop.
I thought lower limit because you check if it passed that threshold. So to check within a certain range with lower and upper level I would expect if( value > lower && value < upper). (Higher then lower and lower then upper). Application can be different but I think the name is a bit confusing then.