-1

I don't know if someone else asked this before.

I'm trying to print in Serial screen the number 1 - 6, ONE number every 5 seconds (I'm trying only with number 1 and number 2 for smaller and easier to undestand and modify code). I could use delay function, but i want to use millis function, because i don't want to block the code using delay in loop.

This code problem is a part of bigger project.

I tryied to print the numbers using different interval times (eventTime_1 and eventTime_2), but it didn't work. The code is


/* Two independant timed evenets */
const unsigned long eventTime_1 = 1000; // interval in ms
const unsigned long eventTime_2 = 5000;

unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;

void setup() {

  // To Do: Serial communication
  Serial.begin(9600);
}

void loop() {

  /* Updates frequently */
  unsigned long currentTime = millis();

  // To Do: Event 1 timing
  /* This is event 1 stuff */
  if ( currentTime - previousTime_1 >= eventTime_1 ) {
    Serial.println (" 1 ");
    //    Serial.println(currentTime);
    //    Serial.println(previousTime_1);
    /* Update the timing for the next event */
    previousTime_1 = currentTime;

  }

  // To Do: Event 2 timing
  /* This is event 2 stuff */
  if (currentTime - previousTime_2 >= eventTime_2 ) {

    Serial.println ("2");
    //    Serial.println( analogRead(tempSensor) );

    /* Update the timing for the next event */
    previousTime_2 = currentTime;

  }
}

As a result, prints 5 times the number 1 and after 5 seconds 1 time the number 2.

This is what i ecpect:

12:16:53.212 -> 1
12:16:58.225 -> 2
12:17:03.233 -> 1
12:17:08.238 -> 2
12:17:13.203 -> 1
12:17:18.272 -> 2
ThP...
  • 5
  • 6
  • 1
    So the expectation is to print `1` and `2` alternating every five second? That's not what your code is doing, it will print `1` *every* second and `2` every five second. Why not use a *single* "timer" to happen every five second, and print `1` or `2` alternating in that? You could easily use a boolean flag to tell if `1` or `2` should be printed, and reverse that flag each time. – Some programmer dude Oct 27 '21 at 08:55
  • 1
    On another note, please don't show images of text, copy-paste text *as text* into your questions. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 27 '21 at 08:55
  • 1
    you want what BlinkWithoutDelay basic example does – Juraj Oct 27 '21 at 08:56
  • I think millis returns a 16-bit int, so it wraps around to 0 after it gets to 65535. – user253751 Oct 27 '21 at 09:08
  • @Someprogrammerdude yes I want to print 1 and 2 alternating every five second. What do you mean " single "timer" ". Could you give an exaple? Also I'll modify my question to paste the result as a text. – ThP... Oct 27 '21 at 09:13
  • Just remove the whole "Event 1 timing" and only do "Event 2 timing". – Some programmer dude Oct 27 '21 at 09:17
  • @Someprogrammerdude but now only second part is executing every 5 seconds. – ThP... Oct 27 '21 at 09:21
  • @user253751, you think wrong. it is `unsigned long` – Juraj Oct 27 '21 at 09:34
  • In the "Event 2 timing" check a flag: If it's `false` the print `1`. If it's `true` then print `2`. Each time toggle this flag (from `false` to `true`, and from `true` to `false`). – Some programmer dude Oct 27 '21 at 09:46
  • @juraj, you are right, I was wrong. In fact, I just loaded OP's code into an Arduino, I can't reproduce what OP said.... – hcheung Oct 27 '21 at 10:02
  • @hcheung, OP wants something else than the code does. something much simpler ('blinking' between '1' and '2' every 5 seconds). I doubt OP wrote the sketch in question. – Juraj Oct 27 '21 at 11:14
  • @Someprogrammerdude thanks you very much. It's working for number 1 and 2. I'll post the code as anser in my question. Thanks again. – ThP... Oct 27 '21 at 12:23
  • @user253751 Not correct, it uses a 32-bit ulong on most parts I've used, so it's a little longer time to wrap -- but of course eventually does. – TomServo Oct 28 '21 at 01:04
  • @TomServo, the sketch in question handles the overflow – Juraj Oct 28 '21 at 05:57

1 Answers1

0

This is the final working code. From this code you can print 1 and 2 alternating every X second (x depends from you, in eventTime = X ; In my code is 5000).

// To Do: Variables for Timed Events

/* Create timed evenets */
const unsigned long eventTime = 5000;

unsigned long previousTime = 0;

/* Create a flag */
boolean flag1 = false;

void setup() {

  // To Do: Serial communication
  Serial.begin(9600);
}

void loop() {

  /* Updates frequently */
  unsigned long currentTime = millis();

  if (currentTime - previousTime >= eventTime ) {
    if (flag1 == false) {
      Serial.println("1");
      flag1 = true;
    }
    else if (flag1 == true) {
      Serial.println ("2");
      flag1 = false;
    }
    /* Update the timing for the next event */
    previousTime = currentTime;
  }
}

And the results are:

15:32:46.342 -> 1
15:32:51.402 -> 2
15:32:56.327 -> 1
15:33:01.325 -> 2
15:33:06.328 -> 1
15:33:11.325 -> 2
15:33:16.327 -> 1
ThP...
  • 5
  • 6
  • yes. it is like the basic example in Arduino IDE Examples menu - BlinkWithoutDelay – Juraj Oct 27 '21 at 13:22
  • @Juraj yes. I agree with you. But it was a little bit hidden (for me) in BlinkWithoutDelay. Never mind, the code now is working as I want (i modified for 1 to 6 later). Also thanks for your advice. I appriciate it. – ThP... Oct 27 '21 at 18:34
  • didn't you see my comment? – Juraj Oct 27 '21 at 18:58
  • Absoluterly, i saw your comment. I didn't understand why BlinkWithoutDelay was what i want. Now i can understand. Thanks again. I didn't know how to use it. Now, i know. – ThP... Oct 29 '21 at 05:48