Halloween Pumpkin LED Help needed!?

Hello guys,

Trying to build a little LED flickering light for a Pumpkin, and have the following code working with a red, orange and yellow LED for a really nice candle flicker effect.

But....I would like to add one more LED (high brightness green) working as per the Blink code, so evey say 60s the pumking take on a spooky green hue.

Trouble is I can't seem for the life of me to get it to work..........any chance of a little help?

// LED Pumpkin Effect

int ledPin1 = 09; // Red LED on Digi Pin 09
int ledPin2 = 10; // Yellow LED on Digi Pin 10
int ledPin3 = 11; // Orange LED on Digi Pin 11

void setup()
{
pinMode(ledPin1, OUTPUT);  
pinMode(ledPin2, OUTPUT); 
pinMode(ledPin3, OUTPUT);  
}

void loop() {
analogWrite(ledPin1, random(120)+135); 
analogWrite(ledPin2, random(120)+135); 
analogWrite(ledPin3, random(120)+135);
delay(random(100));
}[td][/td]

Calling the delay() function will have to be eliminated. Are you familiar with the BlinkWithoutDelay example?

[quote author=Jack Christensen link=topic=193402.msg1428955#msg1428955 date=1381797814]
Calling the delay() function will have to be eliminated. [/quote]
I had a inkling this was were my issues was. but not the skill to deal with it!

I am no......but I will google this now!

What have you tried so far?

Remember, loop() keeps running, so anything you code after the delay() and before the end of loop() will also get exeuted.

You could have a look in the Examples directory (Examples->Digital->BlinkWithout Delay), and use those techniques to turn a LED on every 60 seconds, and tuen it off a few seconds later, all while your red, yellow, and orange LEDs are still flickering.

XD XD XD XD

Works!!!

// LED Pumpkin Effect

int ledPin1 = 9;             // Red LED on Digi Pin 09
int ledPin2 = 10;            // Yellow LED on Digi Pin 10
int ledPin3 = 11;            // Orange LED on Digi Pin 11
const int ledPin4 =  13;     // Green LED
int ledState = LOW;          // ledState used to set the LED
long previousMillis = 0;     // will store last time LED was updated
long interval = 60000;       // interval at which to blink (60 seconds)
void setup()
{
pinMode(ledPin1, OUTPUT);  
pinMode(ledPin2, OUTPUT); 
pinMode(ledPin3, OUTPUT);  
pinMode(ledPin4, OUTPUT);
}

void loop() {
analogWrite(ledPin1, random(120)+135); 
analogWrite(ledPin2, random(120)+135); 
analogWrite(ledPin3, random(120)+135);
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledState);
  }
}

Is there a way I can reduces the amount of time the Green LED is in the HIGH state? so it could come on every 60000ms But only for 10000ms?

Sure! In the sketch as it now stands, interval is constant, it never changes. To make the off time and on time different, set the value of interval at the same places the LED state is set. When setting the state LOW, set interval to 50000, when setting state HIGH, set interval to 10000.

Good job!

Change interval when you change ledstate. When you turn the led on, set interval to 10000L, when you turn it off, turn it back to 60000L.

Edit: or just read what Jack C wrote.

:blush: This is where I start getting lost!

Would I do that like this?

 if (ledState == LOW)
      ledState = HIGH; interval (10000);
    else
      ledState = LOW; interval (60000);

And would I have to delete the interval in setup? long interval = 60000;      // interval at which to blink (60 seconds)

Note that interval(10000) is incorrect syntax, it won't compile. More like:

        if (ledState == LOW) {
            ledState = HIGH;
            interval = 10000;
        }
        else {
            ledState = LOW;
            interval = 50000;
        }

interval still needs to be declared, so I'd write

unsigned long interval = 50000;       // interval at which to blink

Thank you so much for your help with this, will amend the code in the morning and report back! XD