tips on using millis ? small question

I have been having some issues in learning how to code since i discovered that the delay function is more of a curse than a blessing. Now, i saw some tutorials about millis and i don’t know how to implement them in my code, the blink rate for my Led’s is not the same, i need something like 700 on 2300 off, and i don’t know how to do that. I tried setting up different intervals (interval1=700 ; interval2=2300) and i’m still blocked in it. Can someone offer a sample of this type of code, i need something like this :
Green1=2300 → Blue1=700 ->Green2=2300 ->Blue2=700 ->Green3=2300 → Blue3=700.
You can just send me a pice of code that includes 2 Led’s and i will try to figure out myself how to expand it, i just don’t know how to set the time high and time low on each one of them.

I have also tried using this type of arrangement in my code just to see if it works, and it does keep the display on while running the LED loop, but is it a good idea or not?

Best regards

sketch_sep27a.ino (757 Bytes)

The OP’s code:

#include <U8g2lib.h>
#include <U8x8lib.h>

int green=13;
int blue=12;
int gravity=800;
int motor=2500;

#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);

void setup(){ 
  u8g2.begin();
  pinMode (green,OUTPUT);
  pinMode (blue,OUTPUT);
}

void loop() {					
  delay(1000);
  digitalWrite (green,HIGH);   u8g2.clearBuffer ();
  delay(motor);
  digitalWrite (green,LOW);    u8g2.setFont(u8g2_font_ncenB08_tr);
  digitalWrite (blue,HIGH);    u8g2.drawStr(0,10,"Traian e sefu meu");
  delay(gravity);
  digitalWrite (blue,LOW);     u8g2.sendBuffer();

}

People tend to make the millis thing out to be more complicated than it is. All you're doing is looking at two times, the time something started and the time it is now, and subtracting to see how long it has been. It's something you do every single day in your head. But for some reason writing that down just baffles folks.

const byte ledPin = 13;
boolean ledState = LOW;
int onTime = 500;
int offTime = 1000;
unsigned long lastSwitchTime = 0;


void setup() {

 pinMode(13, OUTPUT);

}

void loop() {

  unsigned long currentTime = millis();

  // if the led is ON and has been for more than onTime
  if((ledState == HIGH) && (currentTime - lastSwitchTime >= onTime)){
    //turn it off
    ledState = LOW;
    lastSwitchTime = currentTime;    
  }
  // if the led is off and has been for more than offTime
  if((ledState == LOW) && (currentTime - lastSwitchTime >= offTime)){
    ledState = HIGH;
    lastSwitchTime = currentTime;
  }
  // write the ledState to the led
  digitalWrite(ledPin, ledState);
}

First of all, that seems way easier than everything i've tried. Second of all, by my logic now all i have to do is add 5 more LEDs to that code (like const byte ledPin 1/2/3/4/5/ etc and pinMode for each) and than write " if " the command for each one. Seems legit, i will try it out.

Thank you so much for this, i was close to 8 hours on this matter and for the last 5 of them i knew it was not supposed to be that hard. I will look up the term "boolean" since i don't know what it is, and keep learning until i'm good enough to help others.

Thank's again, Traian

TraianGrg: First of all, that seems way easier than everything i've tried. Second of all, by my logic now all i have to do is add 5 more LEDs to that code (like const byte ledPin 1/2/3/4/5/ etc and pinMode for each) and than write " if " the command for each one. Seems legit, i will try it out.

That's an awful lot of code to write. You could make use of arrays and only have to write that stuff out once and iterate through the array of led pins (and possibly an array of times if they're all different) to work each one. To make it even easier you could put the led pin, the on and off times, and the last switched time into a struct or a class and write the code once instead of over and over. But that would require some reading to understand how structs and classes work.

I am a good student usually, i don't lose patience, so i will probably spend this night learning structures and analyzing your reply to see what knowledge i can steal, because as much as i want this code to work perfectly, i also want it to be my own. And.. not to be rude, but your code compiles,uploads, and when it gets to the arduino it won't work. Don't know why yet

TraianGrg: it won't work. Don't know why yet

Which one? And for what definition of "won't work"? What happens?

Oops. Typo in #2:

Just fixed

unsigned long currentTime = millis;

to

unsigned long currentTime = millis();

Yes, i fixed it like 1 min after i sent you that reply, i tried to tell you earlier, but the website said i exceeded my post limit in a period of 5 minutes or so. Anyway, thank you a lot for your advice. I am on YouTube now, trying to get something out of those iterations you've mentioned. I have looked this up ( https://www.arduino.cc/en/Tutorial/ForLoopIteration ) but it doesn't seem to be what you meant, since it's using delays. My deadline for this project is 4 days, so i guess i've got it. You're pretty lucky for knowing all that, i have had so much good ideas for games and programs, even an OS in the past, but they went by while i was telling myself that i can't code. Now that i have to, i realize it's not that hard. At least at first.

TraianGrg: I have looked this up ( https://www.arduino.cc/en/Tutorial/ForLoopIteration ) but it doesn't seem to be what you meant, since it's using delays.

Don't look for complete code solutions. Instead, go learn just what a for loop does. Once you understand what a for loop does, you will understand what you need to put inside it. Go look at just how arrays work. Look at an example using a for loop to iterate through an array. You don't care about what they do with it in the for loop, just the for loop itself and how they use the variable to index the array one at a time. You should be looking for concepts, not code.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique.

Using millis() is much the same as using your kitchen wall clock to time the cooking of a chicken. Using delay() is the equivalent of sitting watching the oven for 90 minutes - which you would not dream of doing. Instead you note the time when the chicken goes in the oven and periodically check the clock to see if the 90 minutes has elapsed.

...R