I want to do fade in and fade out for 2 leds (then more leds). I do that but with it's very long codes. I'm sure there must be a short codes but i can't find it. Could you help me about it?
I attached my long codes. So you can understand what i want with this code.
Why on earth would you do it like that? It's a massive amount of code that's just copy past. A rule of thumb, if your code is a bunch of copy past than there is a better way! And I hope you don't need the Arduino for something else?
Here an edit that frees up the Arduino (it doesn't use delay) and is scaleble.
const byte LedPins[] = {3, 5, 6, 9, 10, 11};
const unsigned int FadeInterval = 20;
const unsigned int FadeStepsize = 20;
#define NLEDS (sizeof(LedPins)/sizeof(LedPins[0]))
void setup() {
for(byte i = 0; i < (sizeof(LedPins) / sizeof(LedPins[0])); i++){
pinMode(LedPins[i], OUTPUT);
}
}
void loop() {
fadeLoop();
}
void fadeLoop(){
static unsigned long lastMillis;
//only alter the fading once every FadeInterval
if(millis() - lastMillis > FadeInterval){
lastMillis = millis();
fadeDo();
}
}
void fadeDo(){
static byte i;
static bool dir;
static byte brightness;
//last brightness was max, time to go to the next
if(brightness == 255){
brightness = FadeStepsize;
i++;
}
//if FadeStepsize would overshoot
else if(255 - brightness < FadeStepsize){
brightness = 255;
}
//just increase the brightness
else{
brightness += FadeStepsize;
}
if(i == ( sizeof(LedPins) / sizeof(LedPins[0]) ){
i = 0;
dir != dir;
}
//do the fading
if(dir){
analogWrite(LedPins[i], 255 - brightness);
analogWrite(LedPins[i + 1], brightness);
}
else{
analogWrite(LedPins[NLEDS - i, 255 - brightness);
analogWrite(LedPins[NLEDS - i - 1, brightness);
}
}