How to blink a few times with the LED and exit the mode?

Hi,

Task: the LED should flash a couple of times when the sensor is triggered.

int mode;
int bled;
unsigned long timer;
int tb;
bool block;

#define BTN_LED 9


void led_mode (int mode) {
 
   switch(mode) {

    case 1:
      digitalWrite(BTN_LED, HIGH);
      mode = 1;
    break;

    case 2:
      block = false;
      tb = 0;
      timer = millis();
      mode = 5;
    break;
   }
}

void setup() {
  Serial.begin(115200);
  pinMode(BTN_LED, OUTPUT);
}

void loop() {

if(mode == 5) {
    if(tb < 6 && (millis() - timer) >= 500) {
      if(digitalRead(BTN_LED) == HIGH) {
        bled = LOW;
      } else {
        bled = HIGH;
      }
      digitalWrite(BTN_LED, bled);
      tb++;
      timer = millis();
    } else if(tb >= 6 && block == false) {
      Serial.println("<320>");
      if(digitalRead(BTN_LED) == LOW) {
        digitalWrite(BTN_LED, HIGH);
      }
      block = true;
    }
  }
}

It works, but exactly 1 time. If the sensor triggers again, nothing happens. How can I reset after the LED is triggered?

What sensor? What input?

Why do you have function (led_mode) that is never called?

Since mode only ever equals 0, the code does nothing.

Temperature sensor. He stands on another board and communicates with this controller via UART.
The function led_mode () is called upon the acceptance of the desired command via the UART

Please post the program that correlates to your description. There is no call of led_mode in the program posted. I change the led pin to 13 and nothing happens. It can't possibly happen, mode is only ever equal to 0.

P.S. 99 times out of 100, curly brackets belong on their own line. This makes you nested and compound if statements very hard to read.

@OP

1. Make a short description of the problem along with input and output variables.

2. Make a list of the hardware pieces (Arduino, sensor, switch, display device, and etc.) involved with the problem of Step-1.

3. Draw a connection diagram among the hardware devices of Step-2.

4. Write Pseudo Codes using simple if-else structure to describe the solution of the problem of Step-1.

5. Convert the Pseudo Codes of Step-4 into C/C++ and Arduino Codes.

6. Test the codes of Step-5 and keep debugging until they work. If problem, post the codes in the Forum seeking help.

7. Optimize the codes of Step-4 and repeat Step-6.

#define BTN_LED 6
int req;
int bblink;
int bled;
unsigned long timer;
int tb;
bool block;
bool re;


void ledbtn_mode(int sd) {
 switch (sd) {
   case 1:
    if(bblink != 1) {
      digitalWrite(BTN_LED, HIGH);
      bblink = 1;
    }
   break;
   case 5:
    if(bblink != 5) {
      block = false;
      tb = 0;
      timer = millis();
      bblink = 5;
    }
   break;

 }
}

void setup() {
  Serial.begin(115200);
  pinMode(BTN_LED, OUTPUT);
}

void loop() {
  if(Serial.available() > 0) {
    req = Serial.parseInt();
  }
  if(req == 10){
    ledbtn_mode(1);
  } else if(req == 15) {
    ledbtn_mode(5);
  }

  if(bblink == 5) {
    if(tb < 6 && (millis() - timer) >= 500) {
      if(digitalRead(BTN_LED) == HIGH) {
        bled = LOW;
      } else {
        bled = HIGH;
      }
      digitalWrite(BTN_LED, bled);
      tb++;
      timer = millis();
    } else if(tb >= 6 && block == false) {
      Serial.println("<320>");
      if(digitalRead(BTN_LED) == LOW) {
        digitalWrite(BTN_LED, HIGH);
      }
      block = true;
    }
  } 
}

Here is the complete code. Try sending the number 15 to the serial port several times. This mode will work no more than once. Speed 115200

You are sending the characters 1 and 5 via the serial monitor and expecting to see the integer 15.

Search up Serial Input Basics on the forum here. You need to brush up on the serial programming.

P.S. Here is your properly formatted code.

#define BTN_LED 6
int req;
int bblink;
int bled;
unsigned long timer;
int tb;
bool block;
bool re;


void ledbtn_mode(int sd)
 {
  switch (sd)
   {
    case 1:
      if(bblink != 1)
       {
        digitalWrite(BTN_LED, HIGH);
        bblink = 1;
       }
    break;
   case 5:
     if(bblink != 5)
      {
       block = false;
       tb = 0;
       timer = millis();
       bblink = 5;
      }
    break;

  }
 }

void setup()
 {
  Serial.begin(115200);
  pinMode(BTN_LED, OUTPUT);
 }

void loop()
 {
  if(Serial.available() > 0)
   {  req = Serial.parseInt();  }
  if (req == 10)
   { ledbtn_mode(1); }
   else if(req == 15)
    { ledbtn_mode(5); }

  if (bblink == 5)
   {
    if(tb < 6 && (millis() - timer) >= 500)
     {
      if(digitalRead(BTN_LED) == HIGH)
       { bled = LOW; }
       else
        { bled = HIGH; }
      digitalWrite(BTN_LED, bled);
      tb++;
      timer = millis();
     }
     else if(tb >= 6 && block == false)
      {
       Serial.println("<320>");
       if(digitalRead(BTN_LED) == LOW)
        { digitalWrite(BTN_LED, HIGH); }
       block = true;
     }
   } 
 }

See how much easier it is to read and find your problems.

P.P.S. It still doesn't do anything.

Suyano:
#define BTN_LED 6int req;int bblink;int bled;unsigned long timer;int t - Pastebin.com

#define BTN_LED 6

int req;
int bblink;
int bled;
unsigned long timer;
int tb;
bool block;
bool re;

void ledbtn_mode(int sd) {
switch (sd) {
  case 1:
   if(bblink != 1) {
     digitalWrite(BTN_LED, HIGH);
     bblink = 1;
   }
  break;
  case 5:
   if(bblink != 5) {
     block = false;
     tb = 0;
     timer = millis();
     bblink = 5;
   }
  break;

}
}

void setup() {
 Serial.begin(115200);
 pinMode(BTN_LED, OUTPUT);
}

void loop() {
 if(Serial.available() > 0) {
   req = Serial.parseInt();
 }
 if(req == 10){
   ledbtn_mode(1);
 } else if(req == 15) {
   ledbtn_mode(5);
 }

if(bblink == 5) {
   if(tb < 6 && (millis() - timer) >= 500) {
     if(digitalRead(BTN_LED) == HIGH) {
       bled = LOW;
     } else {
       bled = HIGH;
     }
     digitalWrite(BTN_LED, bled);
     tb++;
     timer = millis();
   } else if(tb >= 6 && block == false) {
     Serial.println("<320>");
     if(digitalRead(BTN_LED) == LOW) {
       digitalWrite(BTN_LED, HIGH);
     }
     block = true;
   }
 }
}




Here is the complete code. Try sending the number 15 to the serial port several times. This mode will work no more than once. Speed 115200

in loop()
where you have " if(bblink == 5)" set the last line if the if block to be "bblink = 1" (or something other than 5)

  if(bblink == 5) {
    if(tb < 6 && (millis() - timer) >= 500) {
      if(digitalRead(BTN_LED) == HIGH) {
        bled = LOW;
      } else {
        bled = HIGH;
      }
      digitalWrite(BTN_LED, bled);
      tb++;
      timer = millis();
    } else if(tb >= 6 && block == false) {
      Serial.println("<320>");
      if(digitalRead(BTN_LED) == LOW) {
        digitalWrite(BTN_LED, HIGH);
      }
      block = true;
    }
    bblink = 0;  // (or something else)
  }

adwsystems,

I watched the manual on Serial from Robin.
https://forum.arduino.cc/index.php?topic=288234.0

But unfortunately, this is not a problem. I replaced number 15 with 5, and the problem did not disappear. The counter probably overflows here and is not reset upon a second request.

P.s Your formatted code works for me. LED 6 pin. Sending the number 15.

darrob,

If I set bblink = 1 (it should turn on the LED), then nothing will happen at all.
I tried to do this:

  if(bblink == 5) {
    if(tb < 6 && (millis() - timer) >= 500) {
      if(digitalRead(BTN_LED) == HIGH) {
        bled = LOW;
      } else {
        bled = HIGH;
      }
      digitalWrite(BTN_LED, bled);
      tb++;
      timer = millis();
    } else if(tb >= 6 && block == false) {
      Serial.println("<320>");
      if(digitalRead(BTN_LED) == LOW) {
        digitalWrite(BTN_LED, HIGH);
      }
      block = true;
    }
    if(block == true) {
      bblink = 1;    
    }
  }
if(block == true) {
      bblink = 1;    
    }

The LED starts flashing constantly.
With bblink = 5, I want the LED to blink a couple of times before the counter overflows. This happened, but when the flashing mode is repeated (bblink = 5), nothing happens. Probably something is not reset.

Sorry My brain isn't working tonight I meant directly after "block = true"

#define BTN_LED 6
int req;
int bblink;
int bled;
unsigned long timer;
int tb;
bool block;
bool re;


void ledbtn_mode(int sd) {
 switch (sd) {
   case 1:
    if(bblink != 1) {
      digitalWrite(BTN_LED, HIGH);
      bblink = 1;
    }
   break;
   case 5:
    if(bblink != 5) {
      block = false;
      tb = 0;
      timer = millis();
      bblink = 5;
    }
   break;

 }
}

void setup() {
  Serial.begin(115200);
  pinMode(BTN_LED, OUTPUT);
}

void loop() {
  if(Serial.available() > 0) {
    req = Serial.parseInt();
  }
  if(req == 10){
    ledbtn_mode(1);
  } else if(req == 15) {
    ledbtn_mode(5);
  }

  if(bblink == 5) {
    if(tb < 6 && (millis() - timer) >= 500) {
      if(digitalRead(BTN_LED) == HIGH) {
        bled = LOW;
      } else {
        bled = HIGH;
      }
      digitalWrite(BTN_LED, bled);
      tb++;
      timer = millis();
    } else if(tb >= 6 && block == false) {
      Serial.println("<320>");
      if(digitalRead(BTN_LED) == LOW) {
        digitalWrite(BTN_LED, HIGH);
      }
      block = true;
      bblink = 0; // or something other than 5
    }
  }
}
[code]

It did not help. LED flashes endlessly :frowning:

The code that I posted works.

(I just uploaded it to my arduino)

enter 15 and the LED blinks 3 times then stays on.
enter 10 and the LED stays on
enter 15 again, blink 3 times
enter 15 again, blink 3 times

Try copying the entire code that I posted and upload it to your Arduino

Cheers

darrob,

It works for me too. If we change the mode, the variable is reset. But if I go running one mode several times in a row - nothing works.

Try entering a number, the LED will blink several times. Then try to enter the same number again and nothing will happen.

On you Serial Monitor. at the bottom, make sure that Newline is set.

I have the code running right now.

I can enter 15 many times, and each time the LED will blink 3 times
If I enter 10,the LED stays on

Those are the only two numbers that do anything.

Hmm, it looks like the problem really disappeared.

This is mystic : D

darrob, Thank you very much for your help!