Go Down

### Topic: Blink without delay for a set period (Read 1 time)previous topic - next topic

#### kev1311

##### Jul 31, 2012, 09:44 am
Can someone please tell me how I could make the code below only run for say 5 seconds and then start another sequence of blinks for just the Cross/pin 8 for another 5 seconds before looping back to he beginning?

Or is this impossible when using blink without delay!?

Code: [Select]
`// constants won't change. Used here to // set pin numbers:const int OUTER =  7;// the number of the LED pinconst int CROSS =  8;// the number of the LED pinconst int CIRC =  9;// the number of the LED pin// Variables will change:int ledState = LOW;             // ledState used to set the LEDlong previousMillis = 0;        // will store last time LED was updated// the follow variables is a long because the time, measured in miliseconds,// will quickly become a bigger number than can be stored in an int.long interval = 1000;           // interval at which to blink (milliseconds)void setup() {  // set the digital pin as output:    pinMode(OUTER, OUTPUT);      pinMode(CIRC, OUTPUT);      pinMode(CROSS, OUTPUT);  }void loop () {    static unsigned long starttime;    static int ledstate = LOW;    if (ledstate == HIGH && millis() - starttime > 200) {   ledstate = LOW;   digitalWrite(OUTER, LOW);               digitalWrite(CIRC, LOW);               digitalWrite(CROSS, LOW);   starttime = millis();    }    else if (ledstate == LOW && millis() - starttime > 200) {   ledstate = HIGH;   digitalWrite(OUTER, HIGH);               digitalWrite(CIRC, HIGH);               digitalWrite(CROSS, HIGH);   starttime = millis();    }} `

#### Krodal

#1
##### Jul 31, 2012, 10:15 amLast Edit: Jul 31, 2012, 10:16 am by Krodal Reason: 1
But you should look at some examples. In the second post of that thread I posted some links. Look at them to see if they can be of use for you.

I could look at your code, but I'm not sure what you want.

#### pylon

#2
##### Jul 31, 2012, 10:21 am
It's more a question for the programming forum...

Try this:
Code: [Select]
`#define MODE_MODULO 2void loop () {    static unsigned long starttime;    static int ledstate = LOW;    static uint8_t mode = 0;    static unsigned long modechangetime = millis() + 5000;     if (millis() > modechangetime) {       mode = (mode + 1) % MODE_MODULO;       modechangetime = millis() + 5000;   }    if (mode == 0) {       if (ledstate == HIGH && millis() - starttime > 200) {        ledstate = LOW;  digitalWrite(OUTER, LOW);               digitalWrite(CIRC, LOW);               digitalWrite(CROSS, LOW);  starttime = millis();       }       else if (ledstate == LOW && millis() - starttime > 200) {  ledstate = HIGH;  digitalWrite(OUTER, HIGH);               digitalWrite(CIRC, HIGH);               digitalWrite(CROSS, HIGH);  starttime = millis();       }   } else { // mode == 1      // do what you wanna do her   }} `

By increasing MODE_MODULO you can add as many "modes" as you like.

#### kev1311

#3
##### Jul 31, 2012, 12:49 pm
Thats excellent! Thank you

I added another mode and that worked great, however, I tried adding an extra mode but the code does not seem to work now. I have changed the mode module to 3 (#define MODE_MODULO 3) .... is there something different I have to do for more than 2 modes to work?

Code: [Select]
`void loop () {    static unsigned long starttime;    static int ledstate = LOW;    static uint8_t mode = 0;    static unsigned long modechangetime = millis() + 5000;      if (millis() > modechangetime) {       mode = (mode + 1) % MODE_MODULO;       modechangetime = millis() + 5000;   }    if (mode == 0) {       if (ledstate == HIGH && millis() - starttime > 200) {        ledstate = LOW; digitalWrite(OUTER, LOW);         digitalWrite(CIRC, LOW);         digitalWrite(CROSS, LOW); starttime = millis();       }       else if (ledstate == LOW && millis() - starttime > 200) { ledstate = HIGH; digitalWrite(OUTER, HIGH);         digitalWrite(CIRC, HIGH);         digitalWrite(CROSS, HIGH); starttime = millis();        }    } if (mode == 1)       if (ledstate == HIGH && millis() - starttime > 200) {        ledstate = LOW;         digitalWrite(CROSS, LOW); starttime = millis();       }       else if (ledstate == LOW && millis() - starttime > 200) { ledstate = HIGH;         digitalWrite(CROSS, HIGH); starttime = millis();        }    } if (mode == 2)       if (ledstate == HIGH && millis() - starttime > 200) {        ledstate = LOW;         digitalWrite(CROSS, LOW); starttime = millis();       }       else if (ledstate == LOW && millis() - starttime > 200) { ledstate = HIGH;         digitalWrite(CROSS, HIGH); starttime = millis();       }}`

#### pylon

#4
##### Jul 31, 2012, 12:53 pm
The problem is that your code for mode==1 and mode==2 is identical. I guess the code works but you don't see an effect, do you?

#### PaulS

#5
##### Jul 31, 2012, 01:07 pm
Code: [Select]
`    } if (mode == 1)`
There are no styles that allow for executable code after a }. Don't do it.

#### kev1311

#6
##### Jul 31, 2012, 01:51 pm
Quote
The problem is that your code for mode==1 and mode==2 is identical. I guess the code works but you don't see an effect, do you?

If I was to take the mode==2 out and leave the mode==1 in the code works and the effect works too.

#### kev1311

#7
##### Jul 31, 2012, 03:17 pm
Apologies Pylon, I have just realised what you meant. I changed the mode 2 to say "CIRC" instead of "Cross" but the code still does not work.

Paul - What would the solution be instead of using { after an executable on this code? It seemed to work before the second mode was added.

#### PaulS

#8
##### Jul 31, 2012, 03:28 pm
Quote
Paul - What would the solution be instead of using { after an executable on this code?

The issue isn't using a { after an executable statement. It is having another statement on the same line as the {.

There is nothing syntactically wrong with doing that. There is nothing logically wrong with doing that. It just makes your code damned hard to read.

Quote
but the code still does not work.

The code does something. You want it to do something. Clearly those two somethings are not the same thing. What either of them is is a mystery, though.

#### pylon

#9
##### Jul 31, 2012, 04:43 pm
Code: [Select]
`    } if (mode == 1)`

Does that code compile? The opening and closing curly brackets are not balanced. The line should be

Code: [Select]
`    } if (mode == 1) {`

Same for the mode==2 line.

#### kev1311

#10
##### Aug 01, 2012, 10:12 am
Had a better look at it all this morning and I have it working just nicely now. Thank you again!

Thinking ahead on my project there is something that I am pondering over .... Using the Mode_Module and having the modechangetime set to 5000 works great. But is it possible to have an additional modechangetime at 1000 too so I can make some other LED's blink at a faster rate?

#### PaulS

#11
##### Aug 01, 2012, 12:13 pm
Quote
But is it possible to have an additional modechangetime at 1000 too so I can make some other LED's blink at a faster rate?

With a different name, yes.