Hi, I am currently completing a major work for in school and as a component I need to use an arduino, I am trying to fade 6 LED lights in a sequence. I would like this sequence to have the 6 LEDs light up all at once and then in 7 minutes fade out slowly. I would also like a push switch to be integrated into the circuit also. It would be great if the LEDs fade out one by one smoothly until the seven minutes are up.
I am using a UNO, and have searched and tried to complete my own codes although I cannot get it to do exactly what I want.
It would be great if someone could help, thankyou
PLEASE HELP
int ledPin = 9; // LED connected to digital pin 9
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
I tried manipulating this code for 6 LEDs although failed
You can use Mr. Nick Gammon code on class to fade up and down LEDs connected to the PWM pins
http://gammon.com.au/Arduino/LED_fader.zip
// Example of fading multiple LEDs at different rates without delays
// Author: Nick Gammon
// Date: 27 December 2012
#include <LedFader.h>
// set up some LEDs
// pin min max millis
LedFader strobe (3, 10, 200, 1000);
LedFader navigation (5, 10, 200, 500);
LedFader torpedoes (6, 10, 200, 250);
void setup()
{
strobe.begin ();
navigation.begin ();
torpedoes.begin ();
} // end of setup
void loop()
{
// update lights
strobe.update ();
navigation.update ();
torpedoes.update ();
// do other useful stuff here ...
} // end of loop
Hi thanks for the helpful links, I tried following this but it kept telling me there were multiple problems it would be a big help if you could take a look and inform me of what looks wrong and ill change it i tried modifying it twice differently.
sketch_jul30a.ino (3.1 KB)
sketch_jul30other.ino (807 Bytes)
Just wanted to paste the errors I'm getting from the sketch to confirm these are the same errors you're getting when you click verify.
sketch_jul30a:26: error: invalid suffix "LED" on integer constant
sketch_jul30a:27: error: invalid suffix "LED" on integer constant
sketch_jul30a:28: error: invalid suffix "LED" on integer constant
sketch_jul30a:29: error: invalid suffix "LED" on integer constant
sketch_jul30a:30: error: invalid suffix "LED" on integer constant
sketch_jul30a:31: error: invalid suffix "LED" on integer constant
sketch_jul30a.ino: In function 'void loop()':
sketch_jul30a:110: error: 'ledPin' was not declared in this scope
sketch_jul30a:118: error: 'ledPin' was not declared in this scope
invalid suffix "LED" on integer constant
pinMode (1LED, OUTPUT);
Variable names can't start with a digit.
error: 'ledPin' was not declared in this scope
Or indeed, in any scope
Seems like you got the same errors I did, so indeed the issues here are just syntax errors.
Steverobey:
Seems like you got the same errors I did, so indeed the issues here are just syntax errors.
I didn't get any errors, I just interpreted the ones you got.
When you define the LED pin with oneLED and can not use pinMode (1LED, OUTPUT); to change pinmode, Variable names oneLED is not the same as 1LED
// Which pins are connected to which LED
const byte oneLED = 3;
const byte twoLED = 5;
const byte threeLED = 6;
const byte fourLED = 9;
const byte fiveLED = 10;
const byte sixLED = 11;
byte ledPin[6] = {
oneLED,twoLED,threeLED,fourLED,fiveLED,sixLED };
// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long oneLEDinterval = 500;
const unsigned long twoLEDinterval = 1000;
const unsigned long threeLEDinterval = 1000;
const unsigned long fourLEDinterval = 1000;
const unsigned long fiveLEDinterval = 1000;
const unsigned long sixLEDinterval = 1000;
// Variable holding the timer value so far. One for each "Timer"
unsigned long oneLEDtimer;
unsigned long twoLEDtimer;
unsigned long threeLEDtimer;
unsigned long fourLEDtimer;
unsigned long fiveLEDtimer;
unsigned long sixLEDtimer;
void setup() {
pinMode (oneLED, OUTPUT);
pinMode (twoLED, OUTPUT);
pinMode (threeLED, OUTPUT);
pinMode (fourLED, OUTPUT);
pinMode (fiveLED, OUTPUT);
pinMode (sixLED, OUTPUT);
oneLEDtimer = millis ();
twoLEDtimer = millis ();
threeLEDtimer = millis ();
fourLEDtimer = millis ();
fiveLEDtimer = millis ();
sixLEDtimer = millis ();
}
void toggleoneLED ()
{
if (digitalRead (oneLED) == LOW)
digitalWrite (oneLED, HIGH);
else
digitalWrite (oneLED, LOW);
// remember when we toggled it
oneLEDtimer = millis ();
} // end of toggleGreenLED
void toggletwoLED ()
{
if (digitalRead (twoLED) == LOW)
digitalWrite (twoLED, HIGH);
else
digitalWrite (twoLED, LOW);
// remember when we toggled it
twoLEDtimer = millis ();
} // end of toggleGreenLED
void togglethreeLED ()
{
if (digitalRead (threeLED) == LOW)
digitalWrite (threeLED, HIGH);
else
digitalWrite (threeLED, LOW);
// remember when we toggled it
threeLEDtimer = millis ();
} // end of toggleGreenLED
void togglefourLED ()
{
if (digitalRead (fourLED) == LOW)
digitalWrite (fourLED, HIGH);
else
digitalWrite (fourLED, LOW);
// remember when we toggled it
fourLEDtimer = millis ();
} // end of toggleGreenLED
void togglefiveLED ()
{
if (digitalRead (fiveLED) == LOW)
digitalWrite (fiveLED, HIGH);
else
digitalWrite (fiveLED, LOW);
// remember when we toggled it
fiveLEDtimer = millis ();
} // end of toggleGreenLED
void togglesixLED ()
{
if (digitalRead (sixLED) == LOW)
digitalWrite (sixLED, HIGH);
else
digitalWrite (sixLED, LOW);
// remember when we toggled it
sixLEDtimer = millis ();
} // end of toggleGreenLED
void loop() {
for (int i=0; i < 6 ; i++)
{
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin[i], fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
for (int i=0; i < 6 ; i++)
{
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin[i], fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
delay(1000);
}
}
. . . and if you find yourself prefixing or suffixing variables names with numbers, it's probably time to start learning about arrays.
AWOL:
. . . and if you find yourself prefixing or suffixing variables names with numbers, it's probably time to start learning about arrays.
You know that's funny because now that you mention it I've never thought to use arrays in my Arduino sketches but I over used them in Python when I was working with the GPIO.. Thanks for reminding me how useful they are.
BillHo:
When you define the LED pin with oneLED and can not use pinMode (1LED, OUTPUT); to change pinmode, Variable names oneLED is not the same as 1LED
Thankyou so much,
When i stop the loop, i have tried multiple things such as for(; {} and while(1){} the loop stops after the first LED fades.
I want the loop to run and finish when all LEDs are out, any ideas?
The stickies at the top of the forum are there to be read!
alice0106 - one of them will show you how to do what you want - which one is it? Now write out (in your best hand writing) 10,000 times,
I will read the stickies at the top of any forum I wish to post on, before posting, so that I don't make a complete idiot of my self!
Mark
Here is the code to Fading 6 LEDs using PMW in a particular sequence
// Which pins are connected to which LED
const byte LEDpin[] = {
3,5,6,9,10,11};
// Time periods of fadings in milliseconds (1000 to a second).
const unsigned long LEDinterval = 60000L; // 1 min
// Variable holding the timer value so far. One for each "Timer"
unsigned long LEDtimer;
byte numberLED=0; // current LED to fade
bool startonce =true; //Start all turn on once
bool active_=true; // at the end make it inactive
// set pin to output, get current time
void setup ()
{
for(int i=0;i<6;i++){ // Set 6 pin mode to output
pinMode (LEDpin[i], OUTPUT);
}
LEDtimer = millis (); // get current time
} // end of setup
unsigned long newValue=255;
void updateLED ()
{
// do nothing if not active
if (!active_)
return;
unsigned long now = millis ();
unsigned long howFarDone = now - LEDtimer;
if (howFarDone >= LEDinterval)
{
LEDtimer = now;
numberLED ++;
if ( numberLED > 6){ // if all 6 LED off
numberLED = 0;
active_ = false;
}
}
else
{
byte valDifference = 255;
newValue = ((LEDinterval - howFarDone) * valDifference) / LEDinterval;
analogWrite (LEDpin[numberLED], newValue );
if (newValue <= 0 ){
digitalWrite (LEDpin[numberLED], LOW);
}
} // end of still in same cycle
} // end of fadingLED
void loop ()
{
if (startonce){
for(int i=0;i<6;i++){
analogWrite (LEDpin[i], 255);
}
startonce = false;
}
// Handling the fading of LED one by one.
updateLED ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED fading interval to finish. */
} // end of loop
I played around with some LEDs a little this weekend after reading this thread and was able to find some examples for this kind of stuff. The LEDs I was using are yellow and seem to draw a little more power than I expected but I had no problem lighting, dimming, strobing and chasing all six after a while. This has given me an idea for a little project that I'll share later if anybody is interested. I'm going to connect a rangefinder on some sort of a fixed mount and use a string of LED's next to a numbered scale to indicate distance. I want to work it so that they don't just light or shut off as an object is moved closer or further away but rather fade in / out. This should be fairly easy to do since I can use the comparative value of the rangefinder to determine which LEDs should be dimming and which should be on or off, I just have to work out the math to do that.
..Along with other projects that I want to have done by the beginning of October this year. Our maker group is planning to go to a local maker-fair the beginning of October so I want to have lots of demos ready to go.
If I wanted to control 6 LEDs with different fade sequences I would create a 6 byte array (fadeValue) to hold the different PWM values and have a function like this to apply the values
void lightLeds() {
for (byte n = 0; n < 6; n++) {
analogWrite(ledPin[n], fadeValue[n];
}
}
This function would be called from loop().
Then all you need is some other code to change the values in the array to cause whatever fade patterns you want. Use millis() for timing as illustrated in several things at a time
...R
I also found the below code just now that Might help the OP out a bit..
int Pin5 = 5; // Led 1
int Pin3 = 3; // Led 5
int Pin9 = 9; // Led 2
int Pin10 = 10; // Led 3
int Pin11 = 11; // Led 4 (the 'wave' goes Led: 1-2-3-4-5)
int Value5 = 0; //Value Led1
int Value3 = 0; //Value Led5
int Value9 = 0; //Value Led2
int Value10 = 0; //Value Led3
int Value11 = 0; //Value Led4
int analog0Pin = 0; // button to start wave
int val0 = 0; // variable to store the value read
int threshold = 512; // threshold for button
int timer = 100;
void setup()
{
pinMode(analog0Pin, INPUT);
pinMode(Pin5, OUTPUT); // sets the digital pin as output
pinMode(Pin3, OUTPUT); // sets the digital pin as output
pinMode(Pin9, OUTPUT); // sets the digital pin as output
pinMode(Pin10, OUTPUT); // sets the digital pin as output
pinMode(Pin11, OUTPUT); // sets the digital pin as output
Serial.begin(9600); // setup serial
}
void loop()
{
val0 = analogRead(analog0Pin); // read the input pin for button
Serial.println(val0); // debug value
if (val0 >= threshold) //
{
for(Value5 = 0 ; Value5 <= 255; Value5+=5) // fade in (from min to max) Led 1
{
analogWrite(Pin5, Value5); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(Value5 = 255; Value5 >=0; Value5-=5) // fade out (from max to min)
{
analogWrite(Pin5, Value5);
delay(30);
}
if (Value5 = 255) //
{
for(Value9 = 0 ; Value9 <= 255; Value9+=5) // fade in (from min to max) Led 2
{
analogWrite(Pin9, Value9); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(Value9 = 255; Value9 >=0; Value9-=5) // fade out (from max to min)
{
analogWrite(Pin9, Value9);
delay(30);
}
if (Value9 = 255) //
{
for(Value10 = 0 ; Value10 <= 255; Value10+=5) // fade in (from min to max) Led 3
{
analogWrite(Pin10, Value10); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(Value10 = 255; Value10 >=0; Value10-=5) // fade out (from max to min)
{
analogWrite(Pin10, Value10);
delay(30);
}
}
if (Value10 = 255) //
{
for(Value11 = 0 ; Value11 <= 255; Value11+=5) // fade in (from min to max) Led 4
{
analogWrite(Pin11, Value11); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(Value11 = 255; Value11 >=0; Value11-=5) // fade out (from max to min)
{
analogWrite(Pin11, Value11);
delay(30);
}
}
if (Value11 = 255) //
{
for(Value3 = 0 ; Value3 <= 255; Value3+=5) // fade in (from min to max) Led 5
{
analogWrite(Pin3, Value3); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(Value3 = 255; Value3 >=0; Value3-=5) // fade out (from max to min)
{
analogWrite(Pin3, Value3);
delay(30);
}
}
}
}
}
This can be modified to better suit your needs too, the guy who posted it wanted some help modifying the code This thread.
alice0106:
Hi BillHo, you have been so helpful with my project. Thankyou for your input and help, appreciate it alot. I have one final query that I was wondering if you could help explain.I cannot work out how to add a push-button into the code, I do not understand where to add the commands. I tried just adding the button code in from the freetronics project guide, but i had no luck.
// Which pins are connected to which LED
const byte LEDpin[] = {
3,5,6,9,10,11};
int inPin = 2; // choose the input pin (for a pushbutton)
int val = 0;
// Time periods of fadings in milliseconds (1000 to a second).
const unsigned long LEDinterval = 70000L; // 1 min
// Variable holding the timer value so far. One for each "Timer"
unsigned long LEDtimer;
byte numberLED=0; // current LED to fade
bool startonce =true; //Start all turn on once
bool active_=true; // at the end make it inactive
// set pin to output, get current time
void setup ()
{
for(int i=0;i<6;i++){ // Set 6 pin mode to output
pinMode (LEDpin[i], OUTPUT);
pinMode(inPin, INPUT);
}
LEDtimer = millis (); // get current time
} // end of setup
unsigned long newValue=255;
void updateLED ()
{
// do nothing if not active
if (!active_)
return;
unsigned long now = millis ();
unsigned long howFarDone = now - LEDtimer;
if (howFarDone >= LEDinterval)
{
LEDtimer = now;
numberLED ++;
if ( numberLED > 6){ // if all 6 LED off
numberLED = 0;
active_ = false;
}
}
else
{
byte valDifference = 255;
newValue = ((LEDinterval - howFarDone) * valDifference) / LEDinterval;
analogWrite (LEDpin[numberLED], newValue );
if (newValue <= 0 ){
digitalWrite (LEDpin[numberLED], LOW);
}
} // end of still in same cycle
} // end of fadingLED
void loop (){
val = digitalRead(inPin); // read input value
if (val == HIGH) {
digitalWrite(LEDPin[i], LOW); // turn LED OFF
} else {
digitalWrite(LEDPin[i], HIGH); // turn LED ON
}
} // check if the input is HIGH (button released)
if (startonce){
for(int i=0;i<6;i++){
analogWrite (LEDpin[i], 255);
}
startonce = false;
}
// Handling the fading of LED one by one.
updateLED ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED fading interval to finish. */
} // end of loop
--- It would be great to hear back from you, thankyou :)
What you want the button to do? after you press then what will happen?
I would like the button to initiate the start of the sequence, so once the button is pushed in the lights would come on and then fade