Code to increse frequency of blinking works, but not to decrease, please review

Hello all, I made such code for increasing and decreasing frequency of LED blinking on Uno R3, and real LED stars from blink 1s off for 1 sec, frequency increases to quite fast, and suddenly it become constantly ON (of super high freq), it never goes in the other direction.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}
  int our_delay = 1000;
  bool direction = true; //if true delay decreases and LED blinks faster
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // change state of the LED by setting the pin to the HIGH voltage level
  delay(our_delay);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // change state of the LED by setting the pin to the LOW voltage level
  delay(our_delay);                      // wait for a second
  if (direction = true)
  {
  our_delay=our_delay-50;
  }
  else {
  our_delay=our_delay+50;
  }
 if (our_delay<151){//reverse direction when LED blinks fast
  direction=false;
 }
 if (our_delay>1000){//reverse direction when LED blinks slow
  direction = true;
 }
}

Slow down takes longer delay but you have

  if (direction = true)
  {
  our_delay=our_delay-50;
  }
  else {
  our_delay=our_delay+50;
  }

You have a lot of debug skills to develop, this should have been easy.

What you have doesn't work so what works must be different, right? And with so few lines, what could change? Maybe the variable that holds the delay is a good candidate for fix? Why?

A little side tip, nothing more right than what you do but less typing:

x += 50; // adds 50 to x

x -= 50; // subtracts 50 from x

if (direction = true)
should be
if (direction == true)

I dont see issue in this code:

  • our_delay decreases by 50 with each turn of loop if direction is descending (true);
  • our_delay increases by 50 when direction is false;

Why do you think it is wrong?

Think about this "if" syntax. Is correct?

See post #3

That was it, thank you. Interesting is that process of assigning true value to direction is logically high. Strange, but OK. Thanks

I agree but it's how C works.

That is not what the code does. I quoted it is post #2 and Jim caught another bug right there.

if (direction = true) <<=== this sets direction = true and tests that

where

if (direction == true) <<=== this tests if direction = true

And in your code, you subtract 50 when you think direction is true even though your code makes it true then shortens the delay.

When you debug, do it expecting to find out what you thought wrong because until you do you haven't finished debugging. I write this as someone who spent years of hours debugging over decades of writing code, i started getting paid to do it in Jan 1980. Bug hunting is a required skill for programming, not something to avoid or feel hurt over.

Could you post the code that works?

Value of "asigning value to variable" is the new value of assigned variable.
In C/C++ is zero considered false and anything nonzero is considered true.
so

is (roughly) like

direction=true;
if (direction)

which is (roughly) like

direction=true;
if(true)

and so it goes ony one way.

Working code:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}
  int our_delay = 1000;
  bool direction = true; //if true delay decreases and LED blinks faster
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // change state of the LED by setting the pin to the HIGH voltage level
  delay(our_delay);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // change state of the LED by setting the pin to the LOW voltage level
  delay(our_delay);                      // wait for a second
  if (direction == true)
  {
  our_delay=our_delay-50;
  }
  else {
  our_delay=our_delay+50;
  }
 if (our_delay<151){//reverse direction when LED blinks fast
  direction=false;
 }
 if (our_delay>1000){//reverse direction when LED blinks slow
  direction = true;
 }
}

Mr Smoke, no need to be frustrated i did not do enough of my own research, sorry to waste your precious usually paid time, are you aware you are frustrated with 11 yo?

Wow, I'm impressed.

You have several errors. First the = vs == and your logic is inverted.

You could try it like this : https://wokwi.com/projects/473807830176280577

Yeay!

I like "direction" to be type int rather than type bool because I see "direction" as a "up/down" or "left/right" or "positive/negative," rather a "yes/no".

When "direction" is type int it can be used in a multiplication to make a value go in the positive or negative direction. "anynumber times +1" is "positive anynumber" and "anynumber times -1" is "negative anynumber."

When delay_value becomes too small or too big, use the negative symbol "-" to change direction from positive to negative OR negative to positive.

Also... check a changing variable (with your sketch, "our_delay") to ensure it is "within bounds" BEFORE using it. Later in programming, writing "out of bounds" will not be good.

Here is an example of using int direction rather than bool direction in your sketch to change direction of the delay value.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

int our_delay = 1000;
int direction = 1;  // direction is 'int' to allow it to be negative or positive

void loop() {
  if (our_delay <= 150 || our_delay >= 1000)  // check lower and upper bounds
    direction = -direction;                   // negate (change) direction
  our_delay += 50 * direction;                // direction is +1 or -1, resulting in +50 or -50

  digitalWrite(LED_BUILTIN, HIGH);  // change state of the LED by setting the pin to the HIGH voltage level
  delay(our_delay);                 // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // change state of the LED by setting the pin to the LOW voltage level
  delay(our_delay);                 // wait for a second
}

Good luck.

LOL.

Writing out of bounds is never good.

a7

The variable direction is already a bool, so you can avoid the == vs = pitfall by just doing

  if (direction)

But that kinda reads weird, which actually exposes the main problem, as mentioned by others: "direction" is a bad name for a boolean true/false variable. Compare, for example: "Is it direction, yes or no?" versus "Is it decreasing". In fact, you even have a comment

Naming the variable "decreasingDelay" or "faster" would be mostly self-explanatory.

Did I charge for my time? I'm retired long ago.

Frustrated? LOL!

You still have direction == true result in a Shorter Delay, i.e. faster blinks but since delay < 151 makes direction false it will yoyo the blinks.

I'm more interested in members making progress than I am in mistakes but we need to learn from those perhaps the most.

Not if you see everything in parens as an expression and (expression) is false if zero, true if not zero. It's a little thing that can take us out of our plans, into reality.