How to stop the code loop and start it again?

Thank you. I've leared something :smiley:

I' am confused with a heartbeat TIMER. I now understand that it is used for cheching if the code works correctly. But is it necessary? It does toggle all the time, just not for 60s that whole loop is turned OFF.

Uff, not sure if I am able to work out how to make code work for two minutes and not work for a minute. So I would have to change this part of the code ``unsigned long disableInterval = 10ul * 1000; //10 seconds (change as needed) `

No arguments there.

I would normally gravitate to a State Machine.

The offered sketch was divided into 3 different TIMERs to show some structure, however when it comes to helping a new person in the field it's hard to decide how far and to what complexity the new person is at in their understanding of things :thinking: .

I wish people would read the comments that we place in your attempts to help :woozy_face:

I am very sorry for all the confusion. You helped me so much.

So, I've rewrote your code, but without the heartbeat TIMER, since I have nothing connected to PIN2. Did I do it correctly? It works when I've tried it. It stays 60s ON and 60s OFF. Still not sure how to manipulate that, but will try agian.

#define ENABLED            true
#define DISABLED           false

#define LEDon              HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff             LOW

const byte leds[7]       = {7, 8, 9, 10, 11, 12, 13};

const byte maxLEDs       = sizeof(leds) / sizeof(byte);

bool timingFlag          = ENABLED;

//timing stuff
unsigned long toggleLedTime;
unsigned long disableTime;
unsigned long disableInterval = 60ul * 1000;  //10 seconds (change as needed)
unsigned long LEDinterval;


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  for (byte x = 0; x < maxLEDs; x++)
  {
    pinMode(leds[x], OUTPUT);
  }


  randomSeed(analogRead(0));

} //END of   setup()


//                                        l o o p ( )
//********************************************^************************************************
void loop()
{
  
  //************************************************              T I M E R  toggle LED
  //if enabled, is it time to toggle the a LED ?
  if (timingFlag == ENABLED && millis() - toggleLedTime >= LEDinterval)
  {
    //restart this TIMER
    toggleLedTime = millis();

    digitalWrite(leds[random(0, maxLEDs)], LEDon);
    digitalWrite(leds[random(0, maxLEDs)], LEDoff);
    LEDinterval = random(30, 400);

  }

  //************************************************              T I M E R  disable
  //is it time to toggle the timingFlag ?
  if (millis() - disableTime >= disableInterval)
  {
    //restart this TIMER
    disableTime = millis();

    //LEDs OFF
    for (byte x = 0; x < maxLEDs; x++)
    {
      digitalWrite(leds[x], LEDoff);
    }

    //toggle the timing Flag
    if (timingFlag == ENABLED)timingFlag = DISABLED;
    else timingFlag = ENABLED;
  }


  //************************************************
  //other non blocking code goes here
  //************************************************

} //END of   loop()

Should I change //timing stuff, to this:
unsigned long toggleLedTime = 120ul * 1000;
and then would my LEDs blink for 120s?

  • It toggles all the time, it is there only to give us a visual feedback on what is happening, it can be remove but suggest you use it while you learn to write code.
    How often have you looked at a POWER LED to see is a device is getting power ?
    Looking at the heartbeatLED is similar to a POWER LED.

Try this version of the sketch it still uses the heartbeatLED, but you do not need to connect pin 2:

#define ENABLED            true
#define DISABLED           false

#define LEDon              HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff             LOW

const byte leds[7]       = {7, 8, 9, 10, 11, 12, 13};
const byte heartbeatLED  = 2;

const byte maxLEDs       = sizeof(leds) / sizeof(byte);

bool timingFlag          = ENABLED;

//timing stuff
const unsigned long disableInterval_A = 10ul * 1000;  //10 seconds (change as needed)
const unsigned long disableInterval_B = 15ul * 1000;  //15 seconds (change as needed)
unsigned long disableInterval         = disableInterval_A;

unsigned long heartbeatTime;
unsigned long toggleLedTime;
unsigned long disableTime;
unsigned long LEDinterval;


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  for (byte x = 0; x < maxLEDs; x++)
  {
    pinMode(leds[x], OUTPUT);
  }

  pinMode(heartbeatLED, OUTPUT);

  randomSeed(analogRead(0));

} //END of   setup()


//                                        l o o p ( )
//********************************************^************************************************
void loop()
{
  //************************************************              T I M E R  heartbeatLED
  //is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= 500ul)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle the heartbeat LED
    if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
    else digitalWrite(heartbeatLED, HIGH);
  }

  //************************************************              T I M E R  toggle LED
  //if enabled, is it time to toggle the a LED ?
  if (timingFlag == ENABLED && millis() - toggleLedTime >= LEDinterval)
  {
    //restart this TIMER
    toggleLedTime = millis();

    digitalWrite(leds[random(0, maxLEDs)], LEDon);
    digitalWrite(leds[random(0, maxLEDs)], LEDoff);
    LEDinterval = random(30, 400);

  }

  //************************************************              T I M E R  disable
  //is it time to toggle the timingFlag ?
  if (millis() - disableTime >= disableInterval)
  {
    //restart this TIMER
    disableTime = millis();

    //select a new disableInterval
    if (disableInterval == disableInterval_A) disableInterval = disableInterval_B;
    else disableInterval = disableInterval_A;

    //LEDs OFF
    for (byte x = 0; x < maxLEDs; x++)
    {
      digitalWrite(leds[x], LEDoff);
    }

    //toggle the timing Flag
    if (timingFlag == ENABLED)timingFlag = DISABLED;
    else timingFlag = ENABLED;
  }


  //************************************************
  //other non blocking code goes here
  //************************************************

} //END of   loop()

Do you understand the code below which was added ?

    //select a new disableInterval
    if (disableInterval == disableInterval_A) disableInterval = disableInterval_B;
    else disableInterval = disableInterval_A;

Just saw the code above. I constantly check my LilyPad board, I have it sewn to my project and checking all the LED light and the board.

Honestly I do not understand it. I do not what == this means. But I think it means to interchange between Interval A and B. Why is that needed?

Ok, I've tried this code and now I think I know, disableInterval_A is for how long do LED blink, disableInterval_B is for how long do LED stay OFF.

Yes the two times A and B are swapped each time this TIMER expires.

The code below says, when the current value of disableInterval is equal (==) to variable disableInterval_A make disableInterval equal to (=) disableInterval_B

if (disableInterval == disableInterval_A) disableInterval = disableInterval_B;

else
Make disableInterval equal to (=) disableInterval_A

1 Like

Read this:

1 Like

Thank you so much. I think I will use the first code you posted in Post #13. I like how it works for a minute, and then is off one minute. But can I modify it without the Heartbeat TIMER, like I've posted in the Post #45. Is that ok?

After this I won't bother you anymore. I think there is a lot I have to learn before attempting another complex project. Thank you again for all your help, You saved me today, and I've leared some new things.

Of course.

The only thing we ask of you is that you should have fun !


No, come back when you have questions.

We only learn by doing and by asking questions :sunglasses:

2 Likes

Thank you. You are so kind:) For now I will leave it as in post #45, because I am truly satisfied with what it does. But after this exhibition I am doing I will be back and try and play with this code and try to understand it more, so I will be able to adjust it.

Thank you again for your patience, this forum is amazing.

EDIT: I think it would be easier for me if I would try and play with this code via Breadboard. Now I've already have a finished textile product and it is hard to see what exactly is going on. On Breadboard I could use PIN2 and do what you suggested.

1 Like

BTW

You were asked a while back about using a Flag variable to control code execution.


  //************************************************              T I M E R  toggle LED
  //if enabled, is it time to toggle the a LED ?
  if (timingFlag == ENABLED && millis() - toggleLedTime >= LEDinterval)
  {

  • In the above snippet, we use the timingFlag variable to control when we can check our TIMER

  • If timingFlag is equal to (==) ENABLED (true) we are allowed to check this TIMER.

  • When timerFlag is not equal to ENABLED (i.e. DISABLED) we cannot check this TIMER.

  • Later down the sketch we toggle the timerFlag variable from ENABLED to DISABLED.


Hopefully you can see our Flag variable is being used to control code execution, in this case, our toggle LED TIMER.

1 Like

Also try to avoid using HIGH/LOW or true/false in your code.

Instead, define good names that explain what is happening.

Examples:

#define ENABLED            true
#define DISABLED           false

#define LEDon              HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff             LOW

#define PRESSED            HIGH
#define RELEASED           LOW

etc.

1 Like

Adding a third timer added complexity that IMHO is not nescessary
And if a person does understand a pretty much complex code this person will have a breeze with a lesser complex code.
==> make the code as easy to understand as possible
==> explain in more detail than in less

Than you for your input.

1 Like

Ok, yes, I see that. What exactly do you mean with "allowed to check"? Like if it's working correctly or to check meaning it is right/enabled. Not sure if I formed that right :sweat_smile:

Also, why are the brackets here empty: millis() Should they be empty or should I add milliseconds? Thanks.

Also, would you recommend some online Arduino course for a total beginner like me. To get to know Arduino coding, understand the code etc. So I can learn some basics and not only copy-paste codes and try to understand them. I've been to few workshops for e-textile and I was so intrigued. Until now I've worked with simple e-textile sewn sensors and 3V battery. But now I would like to explore LilyPad options and coding is necessary. I have found this book called Wearable Electronics; Design, prototype, and wear your own interactive garments by Kate Hartman, maybe it'll be good place to start since is covers basic coding :slight_smile:

When the timingFlag variable is equal to ENABLED you can execute the code found next.

When the timingFlag variable is not equal to ENABLED you cannot execute the code found next.


millis( ) is a function that returns the number of milliseconds since the Arduino was powered on.

This function does not take any arguments, i.e. there is no need to place anything between the ( and ).

1 Like