Pages: [1]   Go Down
Author Topic: Blink without delay for a set period  (Read 697 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// constants won't change. Used here to
// set pin numbers:
const int OUTER =  7;// the number of the LED pin
const int CROSS =  8;// the number of the LED pin
const int CIRC =  9;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long 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();
    }
}
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This a long thread: http://arduino.cc/forum/index.php/topic,115352.0.html
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.

To answer your question about blink without delay: anything is possible.
« Last Edit: July 31, 2012, 03:16:58 am by Krodal » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's more a question for the programming forum...


Try this:
Code:
#define MODE_MODULO 2
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();
       }
   } else { // mode == 1
      // do what you wanna do her
   }
}

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

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats excellent! Thank you smiley

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:
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();
       }
}
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    } if (mode == 1)
There are no styles that allow for executable code after a }. Don't do it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-sad
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    } if (mode == 1)


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

Code:
    } if (mode == 1) {

Same for the mode==2 line.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could do the same with one mode variable but you probably have to define more modes and you have to change what your code is doing. It depends on what you wanna achieve which of the two ways is more appropriate.
Logged

Pages: [1]   Go Up
Jump to: