Can Anyone Help Me Understand What I am Doing Wrong?

I am trying to program a timed loop. I can't figure out why this isn't working. The only thing that happens is pin 13 flashes briefly for no reason, then nothing. Why doesn't the loop execute between 4 and 11 seconds after starting the program?

My code:

//Blink in staggered loop between 4 and 11 second into execution of program

unsigned long ttt = millis();

void setup() {
  pinMode(11, OUTPUT);    // sets the digital pin 11 as output
  pinMode(12, OUTPUT);    // sets the digital pin 12 as output
  pinMode(13, OUTPUT);    // sets the digital pin 13 as output
 
}

void loop() {
  while (4000 < ttt < 11000);

  {
  digitalWrite(11, HIGH); // sets the digital pin 11 on
  delay(100);            // waits for a second
  digitalWrite(11, LOW);  // sets the digital pin 11 off
  delay(100);            // waits for a second

  digitalWrite(12, HIGH); // sets the digital pin 12 on
  delay(100);            // waits for a second
  digitalWrite(12, LOW);  // sets the digital pin 12 off
  delay(100);            // waits for a second

  digitalWrite(13, HIGH); // sets the digital pin 13 on
  delay(100);            // waits for a second
  digitalWrite(13, LOW);  // sets the digital pin 13 off
  delay(100);            // waits for a second
}
}

What do you think this does ?

And once you have answered that, ask yourself what is going to update ttt‘s value

1 Like

Hi, @usernameee333
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Where are you updating the value of ttt ?
Also the ; is not part of a condition statement, easily done.

while (4000 < ttt < 11000);

A delay of 100 == 100ms= 0.1s

delay(100); // waits for a second

Tom... :smiley: :+1: :coffee: :australia:

This waits for 1/10 of a second

There are two things wrong here.

  1. This is not the way to check if a variable has a value between two values. You will have to do two compares; e.g.
while (4000 < ttt && ttt < 11000);
// && is a logical AND; you can also use the keyword 'AND'
  1. The semicolon ends the while-loop so whatever is after that will be executed unconditionally (the same affect as if you did not have the while condition).
  1. Often beginners assume that this means that ttt will follow millis(); not sure if that is the case for you but you will have to refresh ttt ever so often so it reflects the actual millis().
  2. You should not call functions that rely on hardware in the global declaration section of the program; millis() might not work as expected and if ttt gets a value between 4000 and 11000, you will enter the while-loop but you will never get out of it because you don't update ttt.
    If you don't assign a value, you're program will kind-of do what you expect.
//unsigned long ttt = millis();
unsigned long ttt;

That might very well be the bootloader kicking in after a reset of the board; though I would expect two or three flashes.

To fix your problem, assign millis() to ttt in the beginning of loop() and replace the while loop by an if statement The below code will flash your leds in the period from 4 seconds after reset till 11 seconds after reset.

void loop()
{
  // get the current time
  ttt = millis();
  
  if (4000 < ttt  && ttt < 11000)
  {
    digitalWrite(11, HIGH); // sets the digital pin 11 on
    delay(100); // waits for a second
    digitalWrite(11, LOW); // sets the digital pin 11 off
    delay(100); // waits for a second

    digitalWrite(12, HIGH); // sets the digital pin 12 on
    delay(100); // waits for a second
    digitalWrite(12, LOW); // sets the digital pin 12 off
    delay(100); // waits for a second

    digitalWrite(13, HIGH); // sets the digital pin 13 on
    delay(100); // waits for a second
    digitalWrite(13, LOW); // sets the digital pin 13 off
    delay(100); // waits for a second
  }
}

You can ignore point (4) when using above code.

One last comment: mixing millis() and delay() is often not going to give the desired results.

only executes the loop when time since start is between 4 and 11 seconds.

Although it's valid syntax, it does not do what you think it does :wink: And yes, we wrote it like that at school when doing math :slight_smile:

I tried to run this code, but it just kept giving my the " ^~~
exit status 1
'ttt' was not declared in this scope
"
error like before.

Hi, @usernameee333
Put

unsigned long ttt;

Before the void setup() statement.

You haven't declared what type of variable ttt is.

Tom.... :smiley: :+1: :coffee: :australia:

If you have problems with modified code, post the full code. I only gave you loop(), I left the rest to you; so you need setup() and the variable declarations.

Hello
I think your sketch needs a time hander, based on the BlinkWithOutWait IDE exampel, to manage two so called 'virtual' switches.
One switch to handle the time frame from 4000 to 11000 and the second switch to blink the led.

may be a picture is better than words :slight_smile:

this is what needs fixing
image

Hi,
While this bit of code appears to say;
image
While ttt is between 4000 and 11000,
In C++ you have to write it as.

while( ttt > 4000 && ttt < 11000)

Which says while ttt is greater then 4000 AND ttt is less than 11000.

https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicaland/
They are called boolean operators.

|| is OR

&& is AND

Tom... :smiley: :+1: :coffee: :australia:

yep, that's fixing

image

2 more arrows to address (as discussed above by others)

Hi,
I'll call them the AND ARROWS... :laughing: :smiley: :laughing:

Tom... :smiley: :+1: :coffee: :australia:

Thank you everyone. It works as intended now.

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