0

How do you make a fading led with 8 different colors I already made a blinking led every 1 second. Can you give me an idea how am I going to fade it every time it switch?

const int red = 5; 
const int green = 6; 
const int blue = 9; 

void setup() { 
    // put your setup code here, to run once: 
    pinMode(red, OUTPUT); 
    pinMode(green, OUTPUT); 
    pinMode(blue, OUTPUT);  
}  

void loop() { 
    lightMe(red); 
    lightMe2(red, green);  // yellow 
    lightMe(green); 
    lightMe2(green, blue);  // cyan 
    lightMe(blue); 
    lightMe2(blue, red);  // magenta 
    lightMe3(red, green, blue); // white 
} 

void lightMe(int pin){ 
    digitalWrite(pin, HIGH); 
    delay(1000); 
    digitalWrite(pin, LOW); 
    delay(500); 
} 

void lightMe2(int pin1, int pin2) { 
    digitalWrite(pin1, HIGH); 
    digitalWrite(pin2, HIGH); 
    delay(1000); 
    digitalWrite(pin1, LOW); 
    digitalWrite(pin2, LOW); 
    delay(500);  
} 

void lightMe3(int pin1, int pin2, int pin3) { 
    digitalWrite(pin1, HIGH); 
    digitalWrite(pin2, HIGH); 
    digitalWrite(pin3, HIGH); 
    delay(1000); 
    digitalWrite(pin1, LOW); 
    digitalWrite(pin2, LOW); 
    digitalWrite(pin3, LOW); 
    delay(500);  
} 
4

You could look at this example, which fades an LED using PWM.

https://www.arduino.cc/en/Tutorial/Fading

Basically you use analogWrite to control the output of the pins with finer resolution, 0-255 rather than 0-1. Only certain pins support PWM, they should be marked on your board. You just need to extend the example to support your LED. (Put the resistor on the common pin)

-1
int color_set = 0;    //set to red first
int wait_fade = 1000 // delay time in ms.  1000ms is equal to 1sec

// set the color for all the LEDs based on the color code
void setcolor(int colorcode) {

  for (int i = 0; i < LED_COUNT; i++) {
    // instead of setting the color all at once we're going to step through each LED to show how to set them individually
    switch(colorcode) { 
      // We're going to pass a CRGB here,   
  case 0: leds[i] = CRGB(255, 0 , 0); break; // red

  case 1: leds[i] = CRGB(255, 127, 0); break; // orange

  case 2: leds[i] = CRGB(255, 255, 0); break; // yellow

  case 3: leds[i] = CRGB(0, 255, 0); break; // green

  case 4: leds[i] = CRGB(0, 255, 255); break; // cyan

  case 5: leds[i] = CRGB(0, 0, 255); break; // blue

  case 6: leds[i] = CRGB(75, 0, 130); break; // indigo

  case 7: leds[i] = CRGB(148, 0, 211); break; // violet
}
  }
     LEDS.show();
    }
// we'll make the color fade in and out by setting the brightness
void pulse(int wait_fade) {
  // let's fade up by scaling the brightness - in general, brightness shouldn't go above 93, so the strip won't draw too much power.
  // Oh, and 93 is plenty bright!
  for(int scale = 0; scale < 93; scale++) { 
    LEDS.setBrightness(scale);
    LEDS.show();
    delay(wait_fade);
  }
  // now let's fade down by scaling the brightness
  for(int scale = 93; scale > 0; scale--) { 
    LEDS.setBrightness(scale);
    LEDS.show();
    delay(wait_fade);
  }
}

// this is the main loop where we call the other functions. 
void loop() {
  int waiting_time = 10; //time in ms for color scaling
  setcolor(color_set);  //we call our color-set routine
  pulse(waiting_time); //now we make it fade in and out
  color_set++;        //next color
  color_set = color_set % 8; // number of color modulus, remember it's 0 based.
}

because there are so many different microcontrollers, i left it basic, and you only need to define color_set as a integer and then the setcolor() loop, as you can see in the main loop we pass the parameter color_set from our integral 8 colors. here is my complete arduino setup:

#include <FastLED.h>

#define LED_COUNT 96 // number of LEDs!
struct CRGB leds[LED_COUNT]; // this struct contains 60 CRGB values.

#define PIN_BUTTON 10
#define PIN_IO_A   7
#define PIN_IO_B   11
#define PIN_SIGNAL 13
#define PIN_INPUT  10

int color_set = 0;   // set to red
int wait_fade = 1000 // delay time in ms.  1000ms is equal to 1sec
// first, let's get ready to blink using some FastLED routines
// take a look at the FastLED example called Fast2Dev for more usage info
void setup()
{  
  LEDS.addLeds<WS2812B, PIN_SIGNAL, GRB>(leds, LED_COUNT); // this configures the Board - leave as is.
  LEDS.showColor(CRGB(0, 0, 0)); // set the color for the strip all at once.
  LEDS.setBrightness(0); // start out with LEDs off
  LEDS.show(); // you'll always need to call this function to make your changes happen.
}

// set the color for all the LEDs based on the color code
void setcolor(int colorcode) {

  for (int i = 0; i < LED_COUNT; i++) {
    // instead of setting the color all at once we're going to step through each LED to show how to set them individually
    switch(colorcode) { 
      // We're going to pass a CRGB here,   
  case 0: leds[i] = CRGB(255, 0 , 0); break; // red

  case 1: leds[i] = CRGB(255, 127, 0); break; // orange

  case 2: leds[i] = CRGB(255, 255, 0); break; // yellow

  case 3: leds[i] = CRGB(0, 255, 0); break; // green

  case 4: leds[i] = CRGB(0, 255, 255); break; // cyan

  case 5: leds[i] = CRGB(0, 0, 255); break; // blue

  case 6: leds[i] = CRGB(75, 0, 130); break; // indigo

  case 7: leds[i] = CRGB(148, 0, 211); break; // violet
}
  }
     LEDS.show();
    }
// we'll make the color fade in and out by setting the brightness
void pulse(int wait_fade) {
  // let's fade up by scaling the brightness - in general, brightness shouldn't go above 93, so the strip won't draw too much power.
  // Oh, and 93 is plenty bright!
  for(int scale = 0; scale < 93; scale++) { 
    LEDS.setBrightness(scale);
    LEDS.show();
    delay(wait_fade);
  }
  // now let's fade down by scaling the brightness
  for(int scale = 93; scale > 0; scale--) { 
    LEDS.setBrightness(scale);
    LEDS.show();
    delay(wait_fade);
  }
}

// this is the main loop where we call the other functions. 
void loop() {
  int waiting_time = 10; //time in ms for color scaling
  setcolor(color_set);  //we call our color-set routine
  pulse(waiting_time); //now we make it fade in and out
  color_set++;        //next color
  color_set = color_set % 8; // number of color modulus, remember it's 0 based.
}

A lot of the code is commented. I use FastLED too. If you have any questions please let me know.

  • You have to explain what it do and how you use it. Code is incomplete ... what is CRGB? What is colorcode (RGB, HSV, ...)? What is wait_fade (ms, sec, other)? – user31481 Aug 10 '17 at 17:17
  • i defined 'wait_fade', which is the delay time. everything runs just fine in any arduino IDE – Oddacon Feb 26 '18 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.