Timer to execute specific code every 2 seconds and run everything else on loop

Having difficulty getting the Blink without delay example to run any code outside of the if statement

I need to delay the arduino from sending data to a client over TCP, it runs a little too fast, I need it to shoot out its sensor data about every 2 seconds or so.

I’ve modified the Blink without delay code to run off of the serial for testing, if I remove the comment lines from where I’ve written Serial.print(“123”); then it never enters the if loop. However, if I place a Serial.print(previousMillis); line outside of the if statement, it shows me that previousMillis gets updated which I think is very strange because it should only update that variable pending that the if statement is executed.

So basically this code will just print a huge series of “123” and will never print “Test” or the variable ‘a’.

I can’t seem to get both codes running at the same time for somewhat of a multithreaded effect.

long previousMillis = 0;
long interval = 2000;
int a = 60;


void setup() {
  // put your setup code here, to run once:

Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
  
//  Serial.println("123");
  
unsigned long currentMillis = millis();

if (currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;
 Serial.println("Test");
 Serial.println(previousMillis);
 Serial.println(a);
  
}



}

I appreciate any info. Thank you very much.

Use unsigned long types

That was incredibly foolish of me to not using unsigned for previousMillis thank you.

My code still does not branch off unfortunately, I keep comparing the code to the one found here :

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Even with the new code having an unsigned long that can keep with up with currentMillis it still doesn't quite get me to the point of being able to run a bunch of code quicker and another specific set of code at a slower rate.

unsigned long previousMillis = 0;
unsigned long interval = 500;
int a = 60;


void setup() {
  // put your setup code here, to run once:

Serial.begin(115200);

}

void loop() {
  // put your main code here, to run repeatedly:
  
  Serial.println("123");
  
unsigned long currentMillis = millis();

if (currentMillis - previousMillis > interval) {
 previousMillis = currentMillis;
 Serial.println("Test");
 Serial.println(previousMillis);
 Serial.println(a);
  
}



}

I feel like this should be easily accomplished with what Blink Without Delay provides.

I don't see anything wrong with your code.

123 123 123 123 Test 9012 60 123 123 123 123

You must realize you will get a lot of Serial.println("123"); happening in 500mS.

Try unsigned long interval = 50; to speed things along.

Thanks much for your help Larry, you're right it is working fine now. Thank you once again I really appreciate it.

you have written a timer and you are using it simply as a single instance timer.

Now if the "if" of the timer was to add a number instead of being used in a if statement lets say a unsigned int called time_delay++; then you could use that in lots of places and in many different arguments.

simply way in the beginning is to make the interval 1000 then its a 1 second count timer and theres no real limit to how many int's you add just reset the int's to equal zero when you have finished using it

don't make a int to replace every place you use to make a delay

for a one off like you are using just use the if time_delay =>2 //do something then reset timer so it will do it every 2 seconds

example of a timed run would be to use a switch case (look under references)

switch(time_delay)

case:4 //do something executes after 4 seconds break; case 8// do something else executes after 8 seconds break; case 12//do the last thing then reset time_delay to zero time_delay=0; executes after 12 seconds break;

want the pattern not to repeat for 60 seconds then don't reset until it hits ==60

want it to hold until something happens then throw in a max number like if >1000 = 1000 that way the time_delay will never roll over until something in the program resets it to zero.

these are just simple ideas that show how things can run at different speeds and times with out having to write lots of millis timers

@gpop1
Better flesh that out a bit more :wink:

thoughts. 1) regarding your current code, it looks to me like you are only calling millis() once when you declare currentMillis = millis(), so it never changes value. I'd declare currentMillis outside of the loop and set it using millis() at the top of the loop.

2) you should check out the timer library and Blink2. I've been using the timer object for a while and really like it. You can set it to call a function every interval....and lots more. Each timer object can track 10 timers. If you will be needing to do things asynchronously then timer is the way to go.

good luck.....jimf

It might be useful to initialize your "previousMillis"

Your original code works OK. What I suspect is happening is that you fail to see the printing of “Test” because the “123” is being printed so fast and so many times.

Try substituting

    for (int x = 0; x < 500; x++)
    {
      Serial.println("\tTest");
    }

for the single printing of “Test” and you will see it.