millis()

Hi, i'm new to this arduino world.

i'm trying to make leds blink in an surtain order. Until now i've used the delay(); function. Now i'm trying millis().

im trying to do this:

green and red led HIGH greenled LOW redled low and yellowled HIGH redled HIGH And then i should repeat itself.

To me this is just impossible, i only manage to make the leds blink in order and i just can't figure out how to make two led HIGH on the same time when using millis();

I'm sure this is easy programming, and i hope some of you can help me. :D

I'm sure this is easy programming

It is. What you need to ask yourself, on each pass through loop() is "Is it time to do xxx?"

If it is, do xxx. Now, clearly you'll need to know some stuff in order to decide if it is time to "do xxx", like maybe the last time xxx was done, how much time should elapse before xxx is done again, etc.

Without seeing your code, all I can suggest is that you keep trying.

I would add that this is most easily accomplished by using arrays. Using an array (or arrays) to store which LEDs should be on at any given time, and then stepping through that array depending on millis() would make for pretty simple code.

http://arduino.cc/en/Reference/Array

Nathanck: i just can't figure out how to make two led HIGH on the same time when using millis();

Changing from delay() to non-blocking code using millis() needs a different mindset and I'm not surprised it's puzzling you, but I am surprised that making two LEDs come on is causing you trouble.

Using a non-blocking approach is more flexible and enables you to control multiple things concurrently, and is better all round. But if you aren't used to it, the code is less obvious than straight forward blocking code. If all you're doing is turning LEDs on and off in a sequence and you don't need to do anything else independently, you could use either approach.

(If you changed the requirements and said you wanted the LEDs to blink at different frequencies, or to handle serial input at the same time, then immediately the non-blocking approach using millis() becomes the better approach.)

 if(millis() >waitRedlight){
    state=!(state);
    waitRedlight+=3000;
    digitalWrite(redLight,state);
    digitalWrite(greenLight,state);
    
    }
       
     
   attachInterrupt(0, buttonPressed, CHANGE);

so this code makes the green and red led blink simultaneously. What if now want to turn off only the green led and turn on yellow led?

Nathanck: so this code makes the green and red led blink simultaneously. What if now want to turn off only the green led and turn on yellow led?

You mean you want them to alternate? Write one of them to !state.

so this code makes the green and red led blink simultaneously.

No that code does not compile because it is not complete. It has an gratuitous attach interrupt that should only be in the setup function.

What if now want to turn off only the green led and turn on yellow led?

Then have the digital writes that will do that.

Post ALL your code and say what you want it to do.

Grumpy_Mike:

Post ALL your code and say what you want it to do.

const int redLed=13;
const int greenLed=12
const int yellowLed=11;
const int button=2;

boolean state = false;   
long wait = 0;

void setup(){ erial.begin(9600);                             
  pinMode(redLed, OUTPUT);                        pinMode(yellowLed, OUTPUT);   pinMode(greenLed, OUTPUT);   pinMode(button,INPUT);

  //changetime=millis(); }

void buttonPressed(){    //just for fun delay(500); digitalWrite(yellowLed,HIGH); delay(500); digitalWrite(yelloLed,HIGH) }

void loop(){ if(millis() >wait){     state=!(state);     wait+=3000;     digitalWrite(redLight,state);     digitalWrite(greenLight,state);         }      
   
  attachInterrupt(0, buttonPressed, CHANGE); }




Now in my loop i want the green and red led to be on, and afterwards I want just the green led to turn off and then wait for å few seconds before the yellow led goes on, and then wait again before the red led goes off. *repeat itself*.

Was this understandable? ( english is not my native language)

thanks

Was this understandable?

No:

void setup(){
erial.begin(9600);

Serial data transmission may seem eerie, but that is not how to initiate it.

Using both millis() and delay() in the same code is counterproductive.

PaulS:

No:

it suposed to stand Serial.begin(9600);

if you look away from the buttonPressed() method.

I want the leds to do this: Green and red HIGH wait Green led LOW wait* Yellow HIGH wait* green HIGH

I want the leds to do this: Green and red HIGH wait Green led LOW wait* Yellow HIGH wait* green HIGH

Then, just use delay().

But, I don't think that IS what you want. What I think you want is: Green and red are turned on. Some time passes. It's time to turn green off. Some more time passes. It's time to turn yellow on. Some more time passes. It's time to turn green on.

Have you looked into arrays like I suggested?

The simplest (but most memory hungry) way would be to have 3 arrays - one for red, one for yellow and one for green. In that array is whether you want the LED on, off, or flashing.

For example, UK crossing traffic lights would be:

#define LED_OFF 0
#define LED_ON 1
#define LED_FLASH 2

unsigned char red[] = {LED_ON, LED_OFF, LED_OFF, LED_OFF};
unsigned char yellow[] = {LED_OFF, LED_FLASH, LED_OFF, LED_ON};
unsigned char green[] = {LED_OFF, LED_OFF, LED_ON, LED_OFF};

Then you have a counter that goes from 0 to 3 (4 entries in each array you see), and inspect millis() to know when to advance that counter.

When the counter advances (or resets to 0) you do the digitalWrite calls using the contents of the array (red[counter]) to decide if the LED is on or off. If the LED is set to flash, you can use another counter to count a flashing period, and turn the LED on or off depending on that counter instead.

void buttonPressed(){    //just for fun
delay(500);
digitalWrite(yellowLed,HIGH);
delay(500);
digitalWrite(yelloLed,HIGH)
}

and then you have

attachInterrupt(0, buttonPressed, CHANGE);

You can not use delays in an interrupt service routine. You should not constantly keep on calling the attach interrupt. Remove all the code I have just posted and try again.