Hey,
I just don't get.
I want to create a class that handles the execution conditions of several function, depending on the execution time provided at initialization as const.
I want to be able to temporary modify the execution time cycle in the code at any given time without modifying the initial time.
soooo.
if i run this code with out any input like this it executes at the right time, with additional serial.print statements i also get percentage is 100 and adjustedCycleTime= 1500.
if(TestOne.isCycleElapsed()){
Serial.println("executed");
}
But when i change the input e.g. to 99 it should execute every 1485 milliseconds. I used unsigned long instead of float because i wanted to keep the overhead as low as possible and the precision does not matter for my application.
here is my code, i really don't get it.
class intervallExecution{
public:
unsigned long currentCycleTime = 0;
const int PERIOD_cycleTime_const;
//int PERIOD_cycleTime_variable;
intervallExecution(const int PERIOD_cycleTime_const_) : PERIOD_cycleTime_const(PERIOD_cycleTime_const_){};
void showPeriod(){
Serial.println(PERIOD_cycleTime_const);
}
bool isCycleElapsed(int percentage = 100) { // Modified method to take percentage as an argument
if (percentage < 0 || percentage > 100) {
Serial.println("Percentage must be between 0 and 100.");
return false;
}
unsigned long adjustedCycleTime = (PERIOD_cycleTime_const * percentage) / 100; // Adjust cycle time based on percentage
if (millis() - currentCycleTime > adjustedCycleTime) {
currentCycleTime = millis();
return true;
} else return false;
}
private:
};
intervallExecution TestOne(1500);
int test;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(TestOne.isCycleElapsed(99)){
Serial.println("executed");
}
Serial.println("not executed");
delay(100);
}