Blink LED 2 times with millis() and then restar cycle

Well, hello everyone. I need help on a project where I want to select some "settings", it's just for my knowledge, but I'm really struggling with how I make this work. Basically, the first configuration works well, the LED blinks once per second ok, but in the second configuration it should blink twice quickly, and then start the cycle again after a second, but I don't think how to do this without it looking weird or else the rhythm gets out of line after a few seconds. Please someone give me a tip. Here's my code so you can take a look...

#define progjump 17
#define botao 26
#define LED 25

int x=0;
bool estadoled = LOW;
int ledconfig=0;
unsigned long int tempA1 = 0;
unsigned long int tempA2 = 0;
unsigned long int tempA3 = 0;
unsigned long int tempA4 = 0;
unsigned long int tempA5 = 0;

void setup() {
 pinMode (botao, INPUT);
 pinMode (LED, OUTPUT);
 pinMode (progjump, INPUT);

 // digitalPinToInterrupt(nomedavar), nomedovoid, config;
 // configs:
 // LOW - aciona interrupĆ§Ć£o quando o estado do pino Ć© LOW, baixo.
 // CHANGE - aciona interrupĆ§Ć£o quando o estado do pino mudar.
 // RISING - aciona interrupĆ§Ć£o quando o estado do pino for de LOW para HIGH.
 // FALLING - aciona interrupĆ§Ć£o quando o estado do pino for de HIGH para LOW.
 // HIGH (Somente alguns arduinos.) - aciona interrupĆ§Ć£o quando o estado do pino Ć© HIGH, alto.

 Serial.begin (9600);
}

void piscatemp () {
  switch (ledconfig) {

    case 0:
      if (millis() - tempA1 >= 1000) {
      digitalWrite (LED, LOW);
      tempA1 = millis();
      }
      if (millis() - tempA2 >= 2000) {
      digitalWrite (LED, HIGH);
      tempA2 = millis();
  }
  break;

    case 1:
      if (millis() - tempA1 >= 400) {
      digitalWrite (LED, LOW);
      tempA1 = millis();
      }
      if (millis() - tempA2 >= 800) {
      digitalWrite (LED, HIGH);
      tempA2 = millis();
  }
  break;

    case 2:
      if (millis() - tempA1 >= 1000){
        digitalWrite (LED, LOW);
        tempA1 = millis();
      }
      if (millis() - tempA2 >= 1500){
        digitalWrite (LED, HIGH);
        tempA2 = millis();
      }
      if (millis() - tempA1 >= 1600){
        digitalWrite (LED, LOW);
        tempA1 = millis();
      }
      if (millis() - tempA2 >= 1700){
        digitalWrite (LED, HIGH);
        tempA2 = millis();
      }
      break;
}
}

void progtime() {

  while (digitalRead(progjump)) {
    if (digitalRead(botao)){
      if (millis() - tempA3 >= 500){
      Serial.println(ledconfig);
      ledconfig++;
      tempA3 = millis();
    }
      if (ledconfig==3){
      ledconfig = 0;
    }
    }
  switch (ledconfig) {
    case 0:
      if (millis() - tempA1 >= 1500) {
      tempA1 = millis();
      digitalWrite (LED, estadoled);
      estadoled = !estadoled;
      }
      break;
    case 1:
      if (millis() - tempA1 >= 1000) {
      tempA1 = millis();
      digitalWrite (LED, estadoled);
      estadoled=!estadoled;
      }
      if (millis() - tempA2 >= 1200) {
      tempA2 = millis();
      digitalWrite (LED, estadoled);
      estadoled=!estadoled;
      }
    break;
  }
}
}

void loop() {
  progtime();
  piscatemp();
}


  • Please explain what this is for.

  • A step by step explanation of your program requirements will help us (at least this old man :older_man:) understand your needs.

  • A State Machine often gets things organized and working.

code is confusing because

  • not clear whether a button is active (pressed) HIGH or LOW

  • looks like in progtime() 2 buttons need to be active at the same time

  • don't understand why the state of a button is a condition in a while loop

  • so many actions are conditional on some elapsed time,

  • why do you have code controlling the same LED in both progtime() and pisctemp()? why not have on sub-function that handles the buttons and another the LED

  • above looks like there are cases when timers may result in conflicting operations. in the case below won't the condition depending on tempA1 being >= 400 also be true when the 2nd condition, tempA2 >= 800 is true

why don't you do

instead of


instead of


why not

void setup () {
    pinMode (botao, INPUT_PULLUP);
    botaoState = digitalRead (botao);
}

void loop () {
    byte but = digitalRead(botao);
    if (botaoState != but)  {
        botaoState = but;
        delay (20);             // debounce
        if (LOW == but)  {
            if (3 <= ++ledconfig)
                ledconfig = 0;
        }
    }

    // ...
}

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

1 Like

Oh! Sorry about my lack of information. I like programing in Esp32, and them i realized i dont know how to save things in flash memory, them i started writing this code. What i want to do is: three modes, mode one: led blinks second to second, mode two: led blinks 0,5 sec faster and mode three: led goes anything differente than last two ones. So how do i save the modes? Putting a jumper between "progjump" and GND, them my while holds all the rest of my program and focus on showing what mode now is working. If mode one, led blinks one time second by second, if mode two, led blinks two times fast, them turn off for one second and starts again over. Well, i think you got what third mode should do, but i dont even get past mode two, probaly i am too dumb for this. Some extra: Why i am not using delay? Well would be more simple just use delay, but i cant, because esp32 cant handle "delays" if connected with internet, and i want mine little "librarie" working i any cases, with internet or no, them i need millis for this :/.

-Yes! Progtime need "2 buttons", one of them is a jumper between progjump and gnd.

  • While is for when i jump progtime, the program stops everything and focus on the modes, showing which mode is avaliabe now, blinking the "number"
  • They control the same led but not at the same time! And how could i do this sub-function? Can you give me a example what is it? Sorry about my sillyness, im trying to study everything i can do with this guys (prototype microcontrollers).

About i didnt used the INPUT_PULLUP, im training to start developing my "own" PCB's, them i use a resistor in protoboard doing the pull down

what do you mean by "focus on the modes"?

but both piscatemp() and progtime () do similar things depending on ledconfig which seems to be setting the mode?

i don't understand why there aren't just 5 modes

look this over

const byte PinLed = 25;
const byte PinBut = 26;
      byte butState;

const unsigned long  MsecShort = 200;
const unsigned long  MsecLong  = 1000;

unsigned long msec0 = MsecShort;
int           cnt;
int           mode;

// -----------------------------------------------------------------------------
void
ledMode2 ()
{
    switch (cnt++) {
    case 0:
    case 2:
        digitalWrite (PinLed, HIGH);
        msec0 += MsecShort;
        break;

    case 1:
        digitalWrite (PinLed, LOW);
        msec0 += MsecShort;
        break;

    case 3:
        digitalWrite (PinLed, LOW);
        msec0 += MsecLong - 4*MsecShort;
        break;

    case 4:
        msec0 += MsecShort;
        cnt = 0;
        break;
    }
}

// -----------------------------------------------------------------------------
void
ledMode0 (
    unsigned long msecPeriod)
{
    msec0 += msecPeriod;
    digitalWrite (PinLed, ! digitalRead (PinLed));
}

// -----------------------------------------------------------------------------
void loop ()
{
    // handle Led
    unsigned long msec = millis ();
    if (msec >= msec0)  {
        switch (mode) {
        case 0:
            digitalWrite (PinLed, LOW);     // off
            msec0 += MsecShort;
            break;

        case 1:
            ledMode0 (1000);
            break;

        case 2:
            ledMode2 ();
            break;

        case 3:
            ledMode0 (400);
            break;
        }
    }

    // handle switch
    byte but = digitalRead (PinBut);
    if (butState != but) {
        butState = but;
        delay (20);         // debounce
        if (HIGH == but) {
            if (3 < ++mode)
                mode = 0;
        }
    }
}


void setup() {
    pinMode (PinLed, OUTPUT);
    Serial.begin (9600);
}

Capitalize Constants

Well, "focus in the modes" i think like, everything that doenst matter stop and we can change the mode presset, for me this make sense XD but i dont think arduino needs time to "focus", its just me saying syllines things =p

About the 5 modes, why them? I just need Three (at moment), if i know how to do three, i can do more after!

And sorry about the loooong time until my answer, i was in work, sooorryyy~

hard to understand what you're trying to do.

you say you have just three modes and they are in piscatemp() indexed by the value of ledconfig. but in progTime() you have 2 "cases" also indexed by ledconfig. seems like ledconfig is the mode variable

it would save time now, for both you and us and in future projects if you could both more clearly state what you are doing or are trying to do, as well as want to do

msec0 += MsecShort;

What does the "+=" means, like, what he do?
(Actually, nvm i google it, just didnt now this shorthand. Pretty useful, huh)

Sorry but i think i dont get everything in the new code =/ dumb me, srry

Sorry about the confusion i created, im not really good making my codes pretty or even understable.

You right, just three modes in piscatemp(). The first case in piscatemp() reads ledconfig to know what mode is now, and progtime reads ledconfig so he can "display" which mode is working now.
Yeah, big part from confusion was because i didnt know how to say what i was trying to do, next time i will add some "//" with clear comments! Thx for the tip.

same as msec0 = msec0 + MsecShort;

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