Fade 3 LEDs arduino

preview_player
Показать описание
//source code. enjoy!
//better code in the comments (this description does not allow brackets, so i can't put it here)

int red = 5; //the first three lines are location of leds
int green = 6;
int blue = 3;

int turn = 0; //turn is whose turn is it to turn on, 0 is red, 1 is blue and 2 is green; this is just to make switching easier
int fade = 5; // amount of fade
int startblue = 0; //this is the birghness of the leds i should have named it brightblue, brightgreen etc.
int startgreen = 0;
int startred = 0;

void setup(){
pinMode(red,OUTPUT); //sets led outputs
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);

}

void loop(){

startred = startred + fade; //these three lines is the fading, it does not show up until its turn is up.
startblue = startblue + fade;
startgreen = startgreen +fade;

//first is the red leds turn which turn starts w/ 0 it faded up then down

if (turn == 0 ){

analogWrite(red,startred);

if (startred == 255){
fade = -fade; //negate the fade to go down

}
}

//once it reaches all the way done it negates the fade again to prepare the blue to fade and it switches the turn to 1 so blue can start
if (fade == -5 && startred == 0 && turn == 0){
turn = 1;
fade = -fade;

}

//blue's turn, same thing as red, fades up, then down
if (turn== 1){
analogWrite(blue,startblue);

if (startblue ==255){
fade = -fade;
}
}
//switches to green and negatives fade to prepare for green
if (turn == 1 && fade ==-5 && startblue == 0){
turn = 2;
fade =-fade;
}

if (turn == 2){
analogWrite(green,startgreen);
if (startgreen == 255){
fade = -fade;
}
}

// finally it resets to 0 to start w. red again
if (turn == 2 && fade ==-5 && startgreen == 0){
turn = 0;
fade = -fade;
}

delay (50); //delay to make it smooth
}
Рекомендации по теме
Комментарии
Автор

//better code, with more clarity
//haven't tested myself so comment any bugs (looks ok)
int ledArray[] = {3, 5, 6}; //array with the led pins
int sizeArray = 3; //a constant to say the size of the array (change if the amount depending on LEDs you have in the array
int index = 0; //array starts at index zero
int brightness = 0; //brightness of led (starts at zero)
int fade = 5; //fade amount (change for different effect)
int de = 50; //amount of delay (change for different effect)

void setup(){
for(int i = 0; i < sizeArray; i++){
pinMode(ledArray[i], OUTPUT); //sets the led output
}
}

void loop(){

analogWrite(ledArray[index], brightness);
brightness = brightness + fadeAmount;

if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;

if(brightness == 0){
analogWrite(ledArray[index], brightness); //before you change the index you have to dim the current one all the way
delay(de);
index = (index+1) % sizeArray; //loop through the array like a circle
}

}
delay (de); //delay to make it smooth
}

withcheesepls
Автор

I used your code, Arduino Nano & mosfets and I am controlling 3 spools of 5 meter long LEDs with wonderful results, thanks!!

dukesnyder
Автор

hi i am trying to light up all the rgb but want it to fade away on different times on LED strip how can I adjust your code

sacmalyorum
Автор

The code that you posted in your description works fine.
But, the code that you posted in your comments isn't working so please check that out

siddharthchad
Автор

Hi! This is exactly what I want to do with my arduino! Can you tell me what registers are you using?
Thanks!

yuanning
Автор

can you upload the code in pastebin so i can copy it and way better. just in case the code goes mess when i copy it from comments

Linkachus
Автор

would this work with a series of leds wired into each pin?

GummyBearsandChemicals
Автор

What omh did you use for each resistor?

perfect
Автор

What omh did you use for each resistor?

perfect