Go Down

Topic: Blink without delay for a set period (Read 774 times) previous topic - next topic

kev1311

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

Krodal

#1
Jul 31, 2012, 10:15 am Last Edit: Jul 31, 2012, 10:16 am by Krodal Reason: 1
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.

pylon

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


Try this:
Code: [Select]

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

kev1311

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

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

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

kev1311

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

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

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

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

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

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.

pylon

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.

Go Up