Dear all,
I am wondering if anyone may be able to help me. I am fairly new to programming and am currently going through the motions trying to teach myself how to work with multiple LEDs, arrays and for loops.
I have four strips of LED tape and they are interfaced with the arduino via a mosfet and powered by an external supply (see image).
So far I have had all the LEDs blinking at once, all the LEDs fading at once and then each strip blinking one after the next. I now need to make the LEDs fade in and out one after the next and I am stuck as to how to integrate one code into the other - or do I need a different code all together?
The code I have used for each strip blinking one after the next is:
/*
Arrays
Demonstrates the use of an array to hold pin numbers
in order to iterate over the pins in a sequence.
Lights multiple LEDs in sequence, then in reverse.
Unlike the For Loop tutorial, where the pins have to be
contiguous, here the pins can be in any random order.
The circuit:
* LEDs from pins 2 through 7 to ground
created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Array
*/
int timer = 1000; // The higher the number, the slower the timing.
int ledPins[] = {
3, 5, 6, 9 }; // an array of pin numbers to which LEDs are attached
int pinCount = 4; // the number of pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
and the fade code for fading all pins at once is this:
/*
Fading
This example shows how to fade an LED using the analogWrite() function.
The circuit:
* LED attached from digital pin 9 to ground.
Created 1 Nov 2008
By David A. Mellis
modified 30 Aug 2011
By Tom Igoe
http://arduino.cc/en/Tutorial/Fading
This example code is in the public domain.
*/
int ledPin1 = 3; // LED connected to digital pin 9
int ledPin2 = 5;
int ledPin3 = 6;
int ledPin4 = 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(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
analogWrite(ledPin3, fadeValue);
analogWrite(ledPin4, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(50);
}
// 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(ledPin1, fadeValue);
analogWrite(ledPin2, fadeValue);
analogWrite(ledPin3, fadeValue);
analogWrite(ledPin4, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(50);
}
}
Would anyone be able to offer me guidance as to how to make each strip fade in and out and then the next strip fade in and out and then the next and loop forever please?
I look forward to your responses.
Thanks,
Aphra
