Millis challenge (switching after a period)

Hello fellow programmers,

I cannot use delay because I need Arduino to listen for incoming data. So I am using mills()

  1. Question about beeping and blinking without delay (or any other switching after some period)
    I have an RGB LED and buzzer that goes off and on at a certain time.

Simple code would be:

If(condition){
beep;
light;
delay(500);
beep off;
light off;
delay(500);
} repeat when the condition is right

My solution is:

if(state == 0){
    if(currenttime - prevtime >= timeint){
    light;
    beep;
    prevtime = currenttime;
    state = 1;
    }
  }
  else if(stav == 1){
    if(currenttime - prevtime >= timeint){
    light stop;
    beep stop;
    prevtime = currenttime;
    stav = 0;
    }   
  }

Is there some proper way, easier or correct way to do this? I am happy it works, but it looks like it came from some beginner like me.

Presumably state and stav are actually the same boolean or byte/int variable and you just mistyped the name.

If so, then I suggest that you use false or LOW in the comparison and you don't need to test its value with else if because if it is not false or LOW then it must be true or HIGH so a simple else will suffice

I also hope that your light and beep functions are non blocking otherwise the millis() timing will not work accurately, if at all

Thank you for your fast reply.

Yes, stav and state are the same, I just kept one in Czech Language and did not see it.

I am using FAST_LED so I need to push RGB values in order to turn the light on and the buzzer is just a HIGH pin, so both are nonblocking.
I know how to use LOW on the buzzer but have no idea how to do it with RGB or other instances, when I want some code to run for a specific period and then stop for some period.

The code works, but I am wondering if there is a proper way to do this, my solution even though it's working seems to me like a workaround.

Hello lord_business

Here comes a sketch takeing the BLINKWITHOUTDELAY example for your project.

Start the serial monitor of the IDE and see what happens.

Check it - test it and make mods to your project needs.

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

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

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    //-------------------------------------------------------------------------
    // here is your current code

    if (ledState == 0)
    {
      Serial.println(F("light"));
      Serial.println(F("beep"));
    }
    else
    {
      Serial.println(F("light stop"));
      Serial.println(F("beep stop"));
    }
    // here is your current code
    //-------------------------------------------------------------------------


    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Can you see why the advice is to post a complete sketch so that questions/problems can be seen in context ?

Please post your full sketch or a shorter but complete sketch tha tillustrates your question

Oh sorry, I am new here and this makes sense.

Here is the complete section:

void program() {

unsigned long currenttime = millis();

  if(stav == 0){
    if(currenttime - prevtime1000_5 >= timeint100){
    digitalWrite(SpeakerPin, LOW);
    r = 255;
    g = 0;
    b = 0;
    light();
    prevtime1000_5 = currenttime;
    stav = 1;
    }
  }
  else if(stav == 1){
    if(currenttime - prevtime1000_5 >= timeint100){
    digitalWrite(SpeakerPin, HIGH);
    FastLED.clear();
    FastLED.show();
    prevtime1000_5 = currenttime;
    stav = 2;
    }   
  }
  else if(stav == 2){
    if(currenttime - prevtime1000_5 >= timeint100){
    digitalWrite(SpeakerPin, LOW);
    r = 255;
    g = 255;
    b = 255;
    light();
    prevtime1000_5 = currenttime;
    stav = 3;
    } 
  }
  else if(stav == 3){
    if(currenttime - prevtime1000_5 >= timeint100){
    digitalWrite(SpeakerPin, HIGH);
    FastLED.clear();
    FastLED.show();
    prevtime1000_5 = currenttime;
    stav = 0;
    } 
  }
}

Thank you,

I now see that I could make it like this:

if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

  if (ledState == 0) {
  ledState = 1;
  } 
  else if (ledState == 1) {
  ledState = 2
  } 
  else if (ledState == 2) {
  ledState = 3
  }
  else if (ledState == 3) {
  ledState = 0
  }
}

That way I don't need to write if(currenttime - prevtime >= timeint), all the time just once.

Thank you

Hello lord_business

Using the SWITCH/CASE instruction get a more convient sketch by using names for the states.

But not the the whole sketch

Hello lord_business

Using the SWITCH/CASE instruction get a more convient sketch by using names for the states.

Thank you,

this is what I meant, just did not know how it works. It looks way neater, I needed to learn how to do this, but looks way better. So I will do:

if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
  switch(State) {
    case red:
    r = 255;
    g = 0;
    b = 0;
    light();
    State = green;
    break;
  
    case green:
    r = 0;
    g = 255;
    b = 0;
    State = blue;
    break;

    case blue:
    r = 0;
    g = 0;
    b = 255;
    State = red;
    break;
  }
}

Hope I got the idea correctly :smiley:

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