LED blink for 500ms outside of loop function

So I need to get an LED to blink for 500ms if I try to divide the input by zero. I've tried this in the loop fuction, for loops, while loops, but nothing seems to work. I was wondering if anyone might have a solution?

In the function "divide(String inputInt)" I'm changing the value of LED_flash from false to true for 500ms, and then back to false. The while loop I have is in the loop function at the end of the code.

void getPotentiometerValue() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  potentiometerToNewRange(sensorValue);
}

//-------------------------------------------------------------------------------

String stageOne(String inputInt) {
  if (inputInt.length() == 5) {
    previousAnswer(inputInt);
  }
  else if (inputInt.length() == 8) {
    whatOperator(inputInt);
  }
  else if (inputInt.length() == 2 && inputInt.charAt(0) == 'P') {
    getPotentiometerValue();
  }
  else {
    errorMessage();
  }
}

//-------------------------------------------------------------------------------


void plus(String inputInt)
{
  getIntOne(inputInt);
  getIntTwo(inputInt);
  answer = intOne + intTwo;
  isPositiveOrNegative();
  isOddOrEven();
  LED_addition();
  Serial.print("Answer: ");
  Serial.println(answer);
  return answer;
}
//-------------------------------------------------------------------------------


void minus(String inputInt)
{
  getIntOne(inputInt);
  getIntTwo(inputInt);
  answer = intOne - intTwo;
  isPositiveOrNegative();
  isOddOrEven();
  LED_minus();
  Serial.print("Answer: ");
  Serial.println(answer);
  return answer;
}
//-------------------------------------------------------------------------------


void multiply(String inputInt)
{
  getIntOne(inputInt);
  getIntTwo(inputInt);
  answer = intOne * intTwo;
  isPositiveOrNegative();
  isOddOrEven();
  LED_multiply();
  Serial.print("Answer: ");
  Serial.println(answer);
  return answer;
}
//-------------------------------------------------------------------------------


void divide(String inputInt)
{
  getIntOne(inputInt);
  getIntTwo(inputInt);
  if (intTwo == 0)
  {
    Serial.println("ERROR");
    Serial.println("");
  }
  else
  {
    answer = intOne / intTwo;
    isPositiveOrNegative();
    isOddOrEven();
    LED_divide();
    LED_flash = true;
    delay(500);
    LED_flash = false;
    Serial.print("Answer: ");
    Serial.println(answer);
    return answer;
  }
}
//-------------------------------------------------------------------------------



void plus2(String inputInt)
{
  getIntOne(inputInt);
  answer = prevAns + intOne;
  isPositiveOrNegative();
  isOddOrEven();
  Serial.print("Answer: ");
  Serial.println(answer);
  return answer;
}
//-------------------------------------------------------------------------------


void minus2(String inputInt)
{
  getIntOne(inputInt);
  answer = prevAns - intOne;
  isPositiveOrNegative();
  isOddOrEven();
  Serial.print("Answer: ");
  Serial.println(answer);
  return answer;
}
//-------------------------------------------------------------------------------


void multiply2(String inputInt)
{
  getIntOne(inputInt);
  answer = prevAns * intOne;
  isPositiveOrNegative();
  isOddOrEven();
  Serial.print("Answer: ");
  Serial.println(answer);
  return answer;
}
//-------------------------------------------------------------------------------


void divide2(String inputInt)
{
  getIntOne(inputInt);
  if (intOne == 0)
  {
    errorMessage();
    Serial.println("");
  }
  else
  {
    answer = prevAns / intOne;
    isPositiveOrNegative();
    isOddOrEven();
    Serial.print("Answer: ");
    Serial.println(answer);
    return answer;
  }
}

//-------------------------------------------------------------------------------

void whatOperator(String inputInt)
{
  char Operator1 = inputInt.charAt(0);


  //Serial.print("\'");
  //Serial.print(Operator);
  //Serial.println("\'");


  if (Operator1 == '+' )
  {
    plus(inputInt);
    digitalWrite(LED_1, LOW);
  }
  else if (Operator1 == '-' )
  {
    minus(inputInt);
    digitalWrite(LED_1, LOW);
  }
  else if (Operator1 == '*' )
  {
    multiply(inputInt);
    digitalWrite(LED_1, LOW);
  }
  else if (Operator1 == '/' )
  {
    divide(inputInt);
    digitalWrite(LED_1, LOW);
  }
  else
  {
    errorMessage();
  }
}

//------------------------------------------------------------------------------

void whatOperator2(String inputInt)
{
  char Operator = inputInt.charAt(0);


  //Serial.print("\'");
  //Serial.print(Operator);
  //Serial.println("\'");


  if (Operator == '+' )
  {
    plus2(inputInt);
  }
  else if (Operator == '-' )
  {
    minus2(inputInt);
  }
  else if (Operator == '*' )
  {
    multiply2(inputInt);
  }
  else if (Operator == '/' )
  {
    divide2(inputInt);
  }
  else
  {
    errorMessage();
  }
}

//-------------------------------------------------------------------------------

String getOperand(String input) {
  char Operator = inputInt.charAt(0);
  return Operator;
}

//-------------------------------------------------------------------------------

int previousAnswer(String inputInt) {
  whatOperator2(inputInt);
}

//-------------------------------------------------------------------------------


void setup()
{
  Serial.begin(9600);
  pinMode (LED_1, OUTPUT);
  pinMode (LED_2, OUTPUT);
  pinMode (LED_4, OUTPUT);
  pinMode (LED_8, OUTPUT);
  pinMode (LED_16, OUTPUT);
  digitalWrite(LED_1, HIGH);
  LED_flash = false;
  prevAns = 0;
  printWelcomeMessage();
  Serial.println("");
}
//-------------------------------------------------------------------------------


void loop()
{
  while (LED_flash = true) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_1, HIGH);
  }
  inputInt = promptReadln();
  inputInt.toUpperCase();
  Serial.print("Input: ");
  Serial.print(inputInt);
  stageOne(inputInt);
  if (answer != "P" || answer != "p") {
    prevAns = answer;
  }
  Serial.println("");
}

Project.ino (15.9 KB)

Some of the code has been removed + the variables that were at the top due to character limit

    LED_flash = true;
    delay(500);
    LED_flash = false;

While the delay function is running your sketch is not doing anything else that isn't in an interrupt service routine. Since the delay prevents anything else from happening you might as well put your blink code here or in a separate funciton.

void blink(unsigned long milliseconds)
{
unsigned long startTime = millis();
  while (millis() - startTime <  milliseconds) 
  {
    // Blink at roughly ten blinks per seconds (100 milliseconds per blink)
    digitalWrite(LED_1, LOW);
    delay(50);
    digitalWrite(LED_1, HIGH);
    delay(50);
  }
}

Stop messing around using delay() and learn to use millis() for timing

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

A state machine (Google it) might come in handy too

I've spotted a mistake I've made as well. The delay code I attempted was in the wrong section on the if statement so It would blink if the second input WASN'T a zero, instead of when it was a zero. I also need to have the led to illuminate at half brightness after the 500ms has passed. I've tried placing it just after the while statement John posted but it just stays off. I've also tried a delay just after the while loop ends, and also placing it in the divide function but It wont come on.

void blink(unsigned long milliseconds) {
  unsigned long startTime = millis();
  while (millis() - startTime <  milliseconds)
  {
    // Blink at roughly ten blinks per seconds (100 milliseconds per blink)
    digitalWrite(LED_1, LOW);
    delay(50);
    digitalWrite(LED_1, HIGH);
    delay(50);
  }
  analogWrite(LED_1, 127);
 LED_flash = true;
    delay(500);
    LED_flash = false;

The thing you have to understand about code is that it is really just a simple set of instructions. People make it out to be all this complex stuff that only geeks and processors understand, but it's really just a set of step by step instructions. And the processor is dumb, it just runs them one by one. It's not going to surmise from the fact that you set that variable to be true that you want it to jump to that other section of code. It's just going to set the value true and run the next line, which is a delay, and then the next line which sets it false again. And by the time your code gets down to the while loop, the variable there is false.

The first thing you have to learn if you want to be able to code is to stop thinking like a story-book. New coders tend to want to write code that tells the story of what happens from beginning to end in a nice linear fashion. And when something happens they tend to start it and then stop and wait right there for it to end. But that's not how we live our lives and for good reason. And it's not how our code should work either.

Instead, think of your code as a high speed checklist. Never will it stop and dawdle on one line or another. It just blasts through a whole bunch of if statements at super high speed, and most of the time they're all false and nothing happens. But occasionally one of them says that it is time to take some small miniscule step in the process, and the code takes that step (setting a pin HIGH or sending some output) and then goes back to blasting through the checklist. It never stops and waits for the return communication. No way, it just has that as one of the many things it checks every few microseconds.

Does anyone know how I would go about getting the "analogWrite(LED_1, 127);" to work after the while loop has ended?

In the code, as written in #4, the analogWrite will be called immediately after the while loop. It will "work". Whether or not that meets your expectations I don't know because I'm not real clear on what exactly your expectations are. But it will do what it is supposed to do. If the pin is a PWM capable pin it will be set at 50% duty cycle. If it isn't a PWM pin then it will be turned off.

I need the LED to blink for 500ms at full luminosity, and after the 500ms I need it to stay lit at 50% luminosity but I'm not sure how to go about coding it. I've tried googling the issue but I can't find any similar solution. I'm probably missing something very obvious.

What you’ve shown us will do that, assuming you’re using a pin that is capable of PWM. Are you?

Yes, the pin in PWM capable. I'm using it in another part of the code and it works fine, but in this case after the while loop ends the pin stays off and doesn't light to 50%

Gwydion0412:
Yes, the pin in PWM capable. I'm using it in another part of the code and it works fine, but in this case after the while loop ends the pin stays off and doesn't light to 50%

That's just not true. If it is PWM capable (you know you could say what pin it is) then that line sets it to 50% duty cycle. Maybe some other line in your code turns it back off or whatever, maybe the end result isn't what you wanted because of something else. But for the little snippet you just showed it will do what you ask.

I have a turnOffLED function that appears twice but they are bypassed in an if statement before the while loop is executed. I've also commented the function call out to check and the LED still remaisn off afetr the while loop.

The LED is on a board that goes on top of the Uno, theres 5 LED's 2 potentiometers, a butoon etc...

Here is the full project for reference

Project.ino (16.1 KB)

Gwydion0412:
Here is the full project for reference

Put it in a code box where I can see it. If you don't know how, read the "How to use this forum" thread.

part 1

String inputInt;
char Operator = inputInt.charAt(0);
int intOne;
int intTwo;
int prevAns;
int answer;
int LED_1 = 5;
int LED_2 = 6;
int LED_4 = 9;
int LED_8 = 10;
int LED_16 = 11;
bool LED_flash;
//------------------------------------------------------------------------------


void printWelcomeMessage()
{
  Serial.println("Please enter the integer values you wish to calculate:");
}
//-------------------------------------------------------------------------------


String promptReadln()
{
  while (!Serial.available()) {};
  return Serial.readString();
}


//-------------------------------------------------------------------------------


void errorMessage()
{
  Serial.println("ERROR");
  Serial.println("");
  digitalWrite(LED_16, HIGH);
}


//-------------------------------------------------------------------------------

void isPositiveOrNegative() {
  if (answer > 0) {
    digitalWrite(LED_16, HIGH);
  }
  else if (answer < 0) {
    digitalWrite(LED_16, LOW);
  }
  else if (answer == 0) {
    analogWrite(LED_16, 127);
  }
}

//-------------------------------------------------------------------------------

void isOddOrEven() {
  if ((answer % 2) == 0) {
    digitalWrite(LED_2, HIGH);
  }
  else if ((answer % 2) != 0) {
    digitalWrite(LED_2, LOW);
  }
}

//-------------------------------------------------------------------------------

void LED_divide() {
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
}

void LED_multiply() {
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
}

void LED_minus() {
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
}

void LED_addition() {
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
}

//-------------------------------------------------------------------------------


void getIntOne(String inputInt)
{
  String stringIntOne = inputInt.substring(1, 4);
  intOne = stringIntOne.toInt();
}


//-------------------------------------------------------------------------------


void getIntTwo(String inputInt) {
  String stringIntTwo = inputInt.substring(4, 7);
  intTwo = stringIntTwo.toInt();
}

//-------------------------------------------------------------------------------

void turnOffLED() {
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}

//-------------------------------------------------------------------------------

void LED0() {
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);

}
void LED1() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED2() {
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED3() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED4() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED5() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED6() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED7() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED8() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED9() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED10() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED11() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED12() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED13() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED14() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED15() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED16() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED17() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, HIGH);
}
void LED18() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED19() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, HIGH);
}
void LED20() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED21() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, HIGH);
}
void LED22() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED23() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, HIGH);
}
void LED24() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED25() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, HIGH);
}
void LED26() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED27() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, HIGH);
}
void LED28() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, LOW);
}
void LED29() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, LOW);
  digitalWrite(LED_16, HIGH);
}
void LED30() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, LOW);
}
void LED31() {
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_4, HIGH);
  digitalWrite(LED_8, HIGH);
  digitalWrite(LED_16, HIGH);
}

If you put those pin numbers in an array then all those LED## functions could just be one function that took a number as an argument. What you have now is plain crazy. Anytime you realize your coding project is going to involve typing the same thing over and over a bunch of times you’re doing it wrong.

Finally got where I can download your code and see it.

This line is useless. It's ALWAYS true. The variable can't have two values, so it will always be not equal to at least one of them. Did you mean to have && there?

if (answer != "P" || answer != "p") {
    prevAns = answer;
  }
int previousAnswer(String inputInt) {
  whatOperator2(inputInt);
}

A function that does nothing but call another function from the same unit? Why not just call the function you want in the first place?

And look what that function does...

void whatOperator2(String inputInt)
{
  char Operator = inputInt.charAt(0);
  turnOffLED();

It turns off the LED!

whatOperator would have done the same thing.

So whenever you get an error, you set that pin to 50% and then loop continues. It makes the useless comparison and sets prevAns = answer. It prints a blank line. And then it prompts for input again. Do you have line endings set in the Serial monitor? If so then there's something to read and it buzzes on through to the whatOperator function and cuts off the light.

I don't know, that code is REALLY hard to follow. There are places for functions and then there is using functions when you could have just written a line of code. Too many one line functions can make code hard to read, especially when you have that ridiculousness with the LED## functions breaking it all up where you have to scroll forever.

Put some serial print statements in your turnOffLed function and see if it's being called. Check some of the others that might turn off that led.

Oh wait, I see it. Look at divide. That's the function that's calling blink(500) and ending up with LED_1 set to 50%. Now look at whatOperator. What's the very next line after divide? What does that do?

So what you have to learn to do is to step through your code. Get a piece of paper to serve as your "memory" and write down all the initial values before you start. Then just walk through your code one line at a time seeing what happens until you get to the point where things go wrong. Just pay attention to what the lines actually say, and not what you "meant".

I think this part could be the problem. As soon as you return from divide() you turn the LED off. That will happen within microseconds of setting the LED to half power.

  else if (Operator1 == '/' )
  {
    divide(inputInt);
    digitalWrite(LED_1, LOW);
  }