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;
}
}
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:
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.
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
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?
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
}
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.
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.