Why is this not executing propertly?

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);

}

The first multiplication of this expression overflow int type range in case 1500 * 99 :

1 Like

ups, i thought the calculation takes place in

adjustedCycleTime 

okay i changed

const int     PERIOD_cycleTime_const;

to

const unsigned long PERIOD_cycleTime_const;

and it works. oh man thanks

Please mark the topic as solved

no one of his topics is marked as solved.

sorry i didn't know that and i'll click the solution from now on.
can i mark more answers as solutions?

No

And it would be nice if you would have marked @b707's solution instead of your own. You can undo and give honours where honours are due :wink: Though I don't think that most of us care too much; number of solutions provided would be nice on our resume when we apply for a job at the age of 70plus or so :rofl:

sry :smiley: did now, man iam getting old :smiley:

1 Like

If you get 1000 solution marks they send you an Arduino UNO.

a7

Let me check how far I am :smiley:
...
...
OOPS not even halfway :sob: