Using Millis in Classes

Hi I am quite new to the arduino, and i have run into a problem:)

I'm looking to use the Millis inside of different classes for timed events, the millis works fine in the .ino file but when i open/or enter the .h file the class the serial stops...

I have surfed the internet but i couldn't find anything about my problem

is there a way to keep the serial running whne you enter a class?

Sure. Serial and millis() are used in classes all the time.

1 Like

Not real sure what you are asking.

1 Like

XY problem, I'm betting in the absence of useful things like code.

I never heard of it referred to as the XY problem, but I have certainly wasted years if not decades of my time wirking with those people.

Sure, lots of ways. It should run anyway, unless you have done something to disable it, like disable interrupts.

Serial can mean hardware or software.

Do you have

#include <Arduino.h>

at/near the top of the .h files you write?

Thanks for the reply

Yes I have:)

So what is your problem?

The Bounce2 library uses millis() in its classes:

Are you using millis() in a non-blocking way?

Or are you using millis() in a delay()-equivalent way like while(millis()-lastMillis < interval){....}? Because that would be silly and block other things.

This might actually go somewhere if the OP would actually show us the class in question.

3 Likes

Please, post your sketch along with brief textual description of what it does.

jdam04

Welcome to the best Arduino forum ever :slight_smile:

Simply use a copy of millis() in currentMillis as a timestamp for each run of the loop().

Let us create a Class based Sketch where the LED1 and LED2 of Fig-1 will blink independently at 2-sec and 4-sec intervals respectively with equal On/Off periods. The millis() function, residing within a Class will controll the intervals. The Serial Monitor spills the messages that the LED1/LED2 have changed states. Pressing SW1 will stop the blinking of the LEDs and the activities of Serial Monitor.


Figure-1:

Sketch:
1. Create a Class based simple Sketch to blink only LED1 at 2-sec inerval. The millis() function, residing within a Class controls the interval period.
.... pending
Figure-1:

Hi Everyone thanks for all the replies
I sat down and created a new simpler sketch for a test
Which is shown under
(because the one i am working on is quite big with some led control and so on)

I messed around a little and figured out that it was the while loop, that "stopped, or disabled" the serial communication (or whatever it did) Swapped it out with a if-statement, and all worked fine:)

can anyone tell me why this is?

anyways thanks for all the answers, and sorry for the inconvinence

Main.Ino

#include <Arduino.h>
#include <JC_Button.h>


unsigned long previousTime = 0;
unsigned long currentTime = 0;

bool FastBlink_isRunning = false;
bool SlowBlink_isRunning = false;

//Setting Up Buttons
  Button button1(23, 25, true, true);
  Button button2(25, 25, true, true);

#include "FastBlink.h"

void setup() {
  
   //Activating Buttons
  button1.begin();
  button2.begin();
  
  Serial.begin(9600);
}

void loop() {
  currentTime = millis();
  button1.read();
  button2.read();
  
  if (button1.wasReleased()){
    Serial.println("Button 1 Pressed");
    
    SlowBlink_isRunning = true;
    FastBlink_isRunning = false;
    
  }else if (button2.wasReleased()){
    Serial.println("Button 2 Pressed");
    FastBlink_isRunning = true;
    SlowBlink_isRunning = false;
    
    
  }
  runSlowBlink();
  runFastBlink();
  
  
}



void runFastBlink(){
  while(FastBlink_isRunning){
    
  FastBlink fastBlink= FastBlink();
  fastBlink.runPattern();

}
}
void runSlowBlink(){
    while(SlowBlink_isRunning){
      
    if(currentTime - previousTime >= 1500){
      Serial.println("SlowBlink");
      previousTime = currentTime;
    }
    }
  
} 

FastBlink.h

#include "Arduino.h"


class FastBlink {
  public:
    FastBlink(){
      // Blank constructor  
    };
    void runPattern();
  private:
};

void FastBlink::runPattern() {
  if(currentTime - previousTime >= 500){
      Serial.println("FastBlink");
      previousTime = currentTime;

  }
  

  
  
}

You mean the while() loop that's not in the code you posted so we don't know what it looked like or what you were doing with it?

I'd guess the answer is because that's what while() loops do.

1 Like

Where is your Class based sketch -- the one for which I have been waiting?

If you won't show us the class that has the problem, you're just wasting everyone's time. Good-bye.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.