why doesn't led go OFF?

sorry for another goofy question. if you can show me, fix this, comment it out so I can learn, greatly appreciated. all I want to see is how to turn on the pin 13 led at start up and have it shut off a minute later. I can make led's blink all freaking day long, but haven't figured this out. yes it is supposed to be in the set up, it will only run one time. i'll even buy you a beer.

int ledPin = 13;
int interval = 60000;
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();


void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  if (currentMillis - previousMillis >= interval)
  {
    currentMillis = previousMillis;
    digitalWrite(ledPin, LOW);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

setup runs once, and it is doubtful if millis even returns more than 1 whilst it runs.

Put the code in loop

Essentially your code wakes up from it's eternal nap, in its wonder of existence and purpose checks to see if 0 is greater than 1 minute. It is not, and then does nothing for remainder of its sad life.

delta_g, hows it going, didn't think of that, it might work, being new at this it will be another struggle for sure but I will give it a try. thanks

Put the code in loop

I can't, that I know of. the LED says, "your time is up" or, "your 1 minute is up". it's frustrating...........

what would you do? this blue light is on for 1 minute, during that minute, 1 or 2 things are going to happen.

  1. you don't do "anything" and after 45 seconds you send another pin HIGH for 1 second (edge detection).

or 2. you push and hold a button for 5 seconds and it starts a RGB LED count down. blue goes on for 1 second every second 5x, then the green, 5x, then the red, it's different........it only goes on once, and goes on for 10 seconds.

when that's done you send that edge detection high again for 1 second and you're done......
then you go to the void loop and run the program.

all ideas are good ideas, thanks

Well, you can create a while loop in setup. It's insane, but if you really want to do it that way, you can.

Okay, you're right. It's not that insane. There is no point to continually checking something in loop() that only gets done once at the program start.

Perhaps some people wouldn't know that loop() is called like this:

while(1)
{
  loop();
};

Thus, it is obvious that while() can accomplish loop()-like tasks.

To avoid type conversion warnings(?)

Seems stylistic.

Delta_G:
Actually if you look at main it's not in a while loop but a for loop.

for(;;;){

}

?!

Have you tried making a while loop in setup? Its crey crey but if you want to....you can.

Sounds like spam.

Sounds like spam.

hello Nick, no it is far from SPAM. it may be unconventional as is most of my stuff. but definitely not spam. not being a programmer and a beginner, I am unable to think as a programmer, though I know what I want to accomplish.

what I am working on at this point is.....sorta, like a user interface that goes in the "setup" it will run 1 time when the project is turned on. the "user" has 2 options, 1. do nothing and it times out, confirms previous user input and moves to the loop. 2. press and hold button for 5 seconds. the 5s is a safety feature. so if the button is inadvertently pushed for a "moment", nothing will happen. when the 5s is met it starts a sequence in which the user can make a change. once made it confirms the change and moves to the loop.

I use LED's in my description simply because it makes it easier than explaining all the components and component characteristics.

I can do it in bits and pieces, but, being a beginner, I suppose you might think it sounds like spam.

I have read many of your articles, and know you are very well versed in programming. perhaps you would like to make a recommendation or 2 on what I am trying to accomplish?

the "user" or operator has (1) minute to do, or not do, before the program leaves set up and moves to the loop once the arduino is turned on. every time the arduino is turned on.

only (1) of (2) things happen.

  1. a blue LED turns on for one minute
    1a. a yellow LED turns on at the 45 second mark for (1) second.
    //this confirms if, and, what action was taken by previous user//
    done and move to loop.

the second part is a bit more difficult because it "happens" while 1 is running. or in between the start of 1 and the 45 second mark.

  1. press and hold a button for 5 seconds, when or if condition is met.
    2a. a RGB LED, blinks 5x blue (1s on1s off)
    it transition to green, blinks 5x (1s on 1s off)
    it transitions to red, blinks 1 time for 10 seconds.
    //this allows new user to change action//

then 1a will confirm changes at the 45 second mark.
new set up is done.

and then it moves to the loop.

I can do the RGB LED sequence with delays or with using millis
I can also press and hold a button 5 seconds and turn on a LED
I can also complete the one minute with edge detection at the 45 second mark.
however, when I start putting it all together, the results are uhmmmmm not as expected.

do you think that this can be accomplished in setup? or should I pursue a different avenue?

thanks!

DougBenson:
Have you tried making a while loop in setup? Its crey crey but if you want to....you can.

LOL, see reply #5.

... and it's not crazy, what is crazy is loop() instead of a while inside main(). :wink:

hey aarg, how's it going my friend. everybody gets a laff out of my stuff. it's ok, I am almost done. perhaps you could lend a hand. I hear you are really good at programming. it may not, pretty sure it's not the way most people would do it. but this is all new to me. so here is a question for you. if you got a minute. i'll post the code. in set up I write the pwrSbBlueLed, HIGH. when you press the button down for 5 seconds, it blinks "another blinky light" sequence. at the end it turns off the pwrSbBlueLed. can you think of a way to leave the pwrSbBlueLed on for 1 minute and light the yellowLed for 1 second at the 45 second mark

"IF" the button ISN'T pressed down? meaning is there anyway to go around the button portion if I choose not to use it?

also, why is it crazy to do it this way? it only needs to run once every time it is turned on.

int pwrSbBlueLed = 9;
int rgbBlue = 4;
int rgbGreen = 5;
int rgbRed = 6;
int yellowLed = 10; 
int fiveToBtn = 7;
int btnPin = 8;

unsigned long previousMillis = 0;
unsigned long interval = 5000;
unsigned long lastBtnPressMillis;
unsigned long pwrSbInterval = 6000;
unsigned long currentMillis = millis();

int numrgbBlueBlinks = 5;
int numrgbGreenBlinks = 5;
int ledTimeOn1 = 1000;
int ledTimeOff1 = 1000;





void setup() {
  pinMode(pwrSbBlueLed, OUTPUT);
  pinMode(rgbGreen, OUTPUT);
  pinMode(rgbBlue, OUTPUT);
  pinMode(rgbRed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(fiveToBtn, OUTPUT);
  digitalWrite(fiveToBtn, HIGH);
  pinMode(btnPin, INPUT);
  digitalWrite(pwrSbBlueLed, HIGH);   //needs to stay on 1 minute even if the button is not pressed, 45 seconds later
                                      // blink yellowLed for 1 second[[[[[or just follow through with the push button routine]]]]]


  while (true) {                         ///////////////////////////////////////////////////////////////////////////////////////////
    int btnVal = digitalRead(btnPin);
    if (btnVal == LOW) {    // assumes btn is LOW when pressed
      lastBtnPressMillis = millis();   // btn is pressed so reset clock ///////////// 5 second button push code
    }
    if (millis() - lastBtnPressMillis >= interval) {
      break;
    }                                    ///////////////////////////////////////////////////////////////////////////////////////////
  }
  for (int j = 1; j <= numrgbBlueBlinks; j = j + 1)
  {
    digitalWrite(rgbBlue, HIGH);
    delay (ledTimeOn1);
    digitalWrite(rgbBlue, LOW);
    delay (ledTimeOff1);
  }
  for (int j = 1; j <= numrgbGreenBlinks; j = j + 1)
  {
    digitalWrite(rgbGreen, HIGH);
    delay(ledTimeOn1);
    digitalWrite(rgbGreen, LOW);
    delay(ledTimeOff1);
  }
  {
    digitalWrite(rgbRed, HIGH);
    delay (10000);
    digitalWrite(rgbRed, LOW);
    delay(10000);
  }
  {
    digitalWrite(yellowLed, HIGH);
    delay(1000);
    digitalWrite(yellowLed, LOW);
    delay(12000);
    digitalWrite(pwrSbBlueLed, LOW);
  }









  }

    void loop() {
    // put your main code here, to run repeatedly:

  }

Fredric58:
hello Nick, no it is far from SPAM. it may be unconventional as is most of my stuff. but definitely not spam.

I was referring to the immediately preceding post by someone with one post to their name:

DougBenson:
Have you tried making a while loop in setup? Its crey crey but if you want to....you can.

That just quotes an earlier post, but turning some of it into gibberish.

Why can you not put it in the loop? That's where this kind of thing goes.

what would you do? this blue light is on for 1 minute, during that minute, 1 or 2 things are going to happen.

  1. you don't do "anything" and after 45 seconds you send another pin HIGH for 1 second (edge detection).
    or 2. you push and hold a button for 5 seconds and it starts a RGB LED count down. blue goes on for 1 second every second 5x, then the green, 5x, then the red, it's different........it only goes on once, and goes on for 10 seconds.

The loop gets executed over and over, tens of thousands of times a second. So each pass through the loop, you look at the current millis(), look at your input pins, look at the state you have stored in your variables, and ask the question "ok: what do I do right now?"

Almost all the time, the answer is "nothing".

But sometimes, the answer is "Well lookee lookee! It's less than a minute since we started, and the button has been pressed! That means we turn off the blue LED, turn on the greed LED, make a note that we are in "countdown mode" and make a note of the current millis() so I can change the colour again in one second from now. Ok, I'm done".

I suggest you look at my response (response #10) on this thread. It implements a pit-stop light tree which sounds very similar to what you are trying to do.

Why can you not put it in the loop? That's where this kind of thing goes.

I'm with you on this. Put the code in loop() and let loop() do what it does best. If you want to stop the startup code running after a period of time and/or because of user input then set a flag and don't run the code when the flag is set.

Fredric58:
hey aarg [...] so here is a question for you. if you got a minute. i'll post the code. in set up I write the pwrSbBlueLed, HIGH. when you press the button down for 5 seconds, it blinks "another blinky light" sequence. at the end it turns off the pwrSbBlueLed. can you think of a way to leave the pwrSbBlueLed on for 1 minute and light the yellowLed for 1 second at the 45 second mark[...]

also, why is it crazy to do it this way? it only needs to run once every time it is turned on.

Okay, here's the skinny. You can create any number of repeat loops (such as loop()) in your program. They can be in setup(). But loop() is unique in that it never exits. So the loops that you place before it in setup(), must have an exit condition. Yours didn't so you have to change that.

In your case, the exit condition should be the completion of the longest delay that you want the first loop to implement. Since it is run at the beginning of the program, you can even just check the value of millis(). However, during this time you cannot run anything in loop().

BUT it sounds like you have to run the logic while the code in the actual loop() is running (since you mentioned 1 minute... I assume that the main program is doing its thing at that point and you are not just blinking LEDs). In that case, you have to set up a timer for it inside loop(), in the same standard way that all timed events are handled in loop().

If so, then this idea about doing things in setup() will only get you lost.

hey guys, you're probably right. I was just going by a tutorial by whorton on youtube. which said the things you want to do once, go here, in set up. Robins while(true) posted earlier works fantastic though I have changed it up a bit.

after multiple attempts to go around that snippet of code presented by Robin. a question came to mind.

would it be possible to have 2 led light sequences (in set up) that would operate in this manner.

------------------turn on arduino---------------------
| |
| |

if nothing happens in 30 seconds if button is pushed for 5 seconds

{do this - blink LEDs} before 30 seconds is up

then go to main program {do this - blink LEDs}

then go to main program

Paul, has your email addressed change, please contact me. thanks, Fred

hey aarg, I am going to combine the main program with the one Robin posted earlier, sometime to day. Robins while (true) seems to work very well, though I don't know how the 2 will work together. if it will exit I mean. as said I am only going by a youtuber tutorial in regards to what you want to do once goes in set up. Robin introduced break to me yesterday, I have been trying it in different scenerios. still just trying to learn.

if I put a simple blinking sequence in set up, it exits when it is done and goes straight to the main program. I just want to be able to choose between 2 sequences. thanks for you input. I am learning, there is just a lot to learn.

and I'm never........ going back........ to my old........... school

On the right hand side there, how long do you want to blink the LEDs before you go to the main program? Assuming that the blinking is supposed to begin after the button is released.... or is that not correct?

hi aarg, when the button is held down for 5 seconds, it doesn't start when you let go, it just starts after 5 seconds, I mean you can hold it for 10, but I just stop pushing when blue 2 led starts blinking.

it goes

blue 1 led goes on, stays on

blue 2 led starts at same time as 1 and blinks 5x, for one second with one second interval, I use delay,

then

green led blinks 5x " " " " "

then

red blinks 1x for 10 seconds

pause 10 seconds

yellow led blinks 1 second

pause 10 seconds

blue 1 led off

go to main program