Page 1 of 1

Arduino LED effects example

Arduino LED effects example

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Hardware Arduino Arduino LED effects example

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • 13103

    Hans
    Keymaster

    This is an example on how to create a sketch as used in the Arduino – LEDStrip effects for NeoPixel and FastLED article.

    In this example I assume we will be using the FastLED library, so make sure it is installed in your Arduino IDE’s library.

    Step 1. Grab the proper starting code, so the “framework” for FastLED, which would be this:

    #include "FastLED.h"
    #define NUM_LEDS 60
    CRGB leds[NUM_LEDS];
    #define PIN 6
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    }
    // *** REPLACE FROM HERE ***
    void loop() {
      // ---> here we call the effect function <---
    }
    // ---> here we define the effect function <---
    // *** REPLACE TO HERE ***

    void showStrip() {
     #ifdef ADAFRUIT_NEOPIXEL_H
       // NeoPixel
       strip.show();
     #endif
     #ifndef ADAFRUIT_NEOPIXEL_H
       // FastLED
       FastLED.show();
     #endif
    }
    void setPixel(int Pixel, byte red, byte green, byte blue) {
     #ifdef ADAFRUIT_NEOPIXEL_H
       // NeoPixel
       strip.setPixelColor(Pixel, strip.Color(red, green, blue));
     #endif
     #ifndef ADAFRUIT_NEOPIXEL_H
       // FastLED
       leds[Pixel].r = red;
       leds[Pixel].g = green;
       leds[Pixel].b = blue;
     #endif
    }
    void setAll(byte red, byte green, byte blue) {
      for(int i = 0; i < NUM_LEDS; i++ ) {
        setPixel(i, red, green, blue);
      }
      showStrip();
    }

    Note the bolded section in the code that starts with “// *** REPLACE FROM HERE ***” and ends at “// *** REPLACE TO HERE ***“?

    Replace the lines between these 2 with one of the effects you’d like to use.

    So let’s say we want to use the “LEDStrip Effect – Fade In and Fade Out: Red, Green and Blue” effect. As you can see the suggested code would be:

    void loop() {
      RGBLoop();
    }
    void RGBLoop(){
      for(int j = 0; j < 3; j++ ) {
        // Fade IN
        for(int k = 0; k < 256; k++) {
          switch(j) {
            case 0: setAll(k,0,0); break;
            case 1: setAll(0,k,0); break;
            case 2: setAll(0,0,k); break;
          }
          showStrip();
          delay(3);
        }
        // Fade OUT
        for(int k = 255; k >= 0; k--) {
          switch(j) {
            case 0: setAll(k,0,0); break;
            case 1: setAll(0,k,0); break;
            case 2: setAll(0,0,k); break;
          }
          showStrip();
          delay(3);
        }
      }
    }

    Now replace the text between the lines  “// *** REPLACE FROM HERE ***” and “// *** REPLACE TO HERE ***” with this code, and you’d get this (the bold section is what I’ve pasted into the “framework” code):

    #include "FastLED.h"
    #define NUM_LEDS 60
    CRGB leds[NUM_LEDS];
    #define PIN 6
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    }
    // *** REPLACE FROM HERE ***
    void loop() {
      RGBLoop();
    }
    void RGBLoop(){
      for(int j = 0; j < 3; j++ ) {
        // Fade IN
        for(int k = 0; k < 256; k++) {
          switch(j) {
            case 0: setAll(k,0,0); break;
            case 1: setAll(0,k,0); break;
            case 2: setAll(0,0,k); break;
          }
          showStrip();
          delay(3);
        }
        // Fade OUT
        for(int k = 255; k >= 0; k--) {
          switch(j) {
            case 0: setAll(k,0,0); break;
            case 1: setAll(0,k,0); break;
            case 2: setAll(0,0,k); break;
          }
          showStrip();
          delay(3);
        }
      }
    }

    // *** REPLACE TO HERE ***
    void showStrip() {
     #ifdef ADAFRUIT_NEOPIXEL_H
       // NeoPixel
       strip.show();
     #endif
     #ifndef ADAFRUIT_NEOPIXEL_H
       // FastLED
       FastLED.show();
     #endif
    }
    void setPixel(int Pixel, byte red, byte green, byte blue) {
     #ifdef ADAFRUIT_NEOPIXEL_H
       // NeoPixel
       strip.setPixelColor(Pixel, strip.Color(red, green, blue));
     #endif
     #ifndef ADAFRUIT_NEOPIXEL_H
       // FastLED
       leds[Pixel].r = red;
       leds[Pixel].g = green;
       leds[Pixel].b = blue;
     #endif
    }
    void setAll(byte red, byte green, byte blue) {
      for(int i = 0; i < NUM_LEDS; i++ ) {
        setPixel(i, red, green, blue);
      }
      showStrip();
    }

    Now you can compile and run the code on your Arduino.
    Don’t forget to set the correct number of LEDs (NUM_LEDS) and Pin (PIN) you’ve used.

    13205

    geowai
    Participant

    Atoms electrons animation

    Am trying to make an animation of electron of an atom as a teaching
    aid. As a Chemistry teacher, it has been a challenge to make students
    imagine of the small electrons moving around the nucleus. I have though
    out a simple way using led to represent the electrons while the nucleus
    where protons and neutrons which do not move are displayed with an LCD
    as well as the Mass Unit(MU).
    I have made the code for Hydrogen, this is simple because its led
    lighting around a RGB ring.
    Here is the code .

    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    #include &lt;Adafruit_NeoPixel.h&gt;
    #include &lt;Wire.h&gt;
    #include &lt;LCD.h&gt;
    #include &lt;LiquidCrystal_I2C.h&gt;
    #define PIN 3
    #define BRIGHTNESS 30
    // How many NeoPixels are attached to the Arduino?
    #define NUMPIXELS      8

    //////////////////////////////////////////////Define variables for LCD1602//////////////////////////////////////////

    #define I2C_ADDR          0x27       //Define I2C Address where the PCF8574A is
    #define BACKLIGHT_PIN      3
    #define En_pin             2
    #define Rw_pin             1
    #define Rs_pin             0
    #define D4_pin             4
    #define D5_pin             5
    #define D6_pin             6
    #define D7_pin             7

    ////////////////////////////////////////////////////Initialize the LCD1602///////////////////////////////////////////
    LiquidCrystal_I2C      lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
    //                BS  E  D4 D5  D6 D7
    //LiquidCrystal_I2C lcd(7, 8, 9, 10, 11, 12);
    void printDetail(uint8_t type, int value);

    Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

    //int delayval = 250; // delay for half a second
    void setup()
    {
      Serial.begin(115200);

      Serial.println();
      pixels.setBrightness(BRIGHTNESS);
      pixels.begin(); // This initializes the NeoPixel library.
    lcd.begin (16,2); //Define the LCD as 16 column by 2 rows
     lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); //Switch on the backlight
     lcd.setBacklight(HIGH);

    }

    void loop()
    {
     lcd.setCursor(2,0);
      lcd.print("Hydrogen Atom");
      lcd.setCursor(1,1);
      lcd.print("P=1,N=0, MU =1");

      for(int i=0; i&lt;=8; i++)
      {
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(0,255,0)); // Moderately bright green color.
        pixels.show(); // This sends the updated pixel color to the hardware.
        delay(100);
        pixels.clear();
       //delay(delayval); // Delay for a period of time (in milliseconds).
      }

     lcd.clear();
    }

    I have also made for Helium as shared below

    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    #include &lt;Adafruit_NeoPixel.h&gt;
    #include &lt;Wire.h&gt;
    #include &lt;LCD.h&gt;
    #include &lt;LiquidCrystal_I2C.h&gt;
    //#define PIN_A
    //#define PIN_B
    #define PIN 3
    #define SDA A4
    #define SDL A5
    #define PIN 3
    //#define PIN_A 8
    #define BRIGHTNESS 30
    // How many NeoPixels are attached to the Arduino?
    #define NUMPIXELS      12

    //////////////////////////////////////////////Define variables for LCD1602//////////////////////////////////////////

    #define I2C_ADDR          0x27       //Define I2C Address where the PCF8574A is
    #define BACKLIGHT_PIN      3
    #define En_pin             2
    #define Rw_pin             1
    #define Rs_pin             0
    #define D4_pin             4
    #define D5_pin             5
    #define D6_pin             6
    #define D7_pin             7

    ////////////////////////////////////////////////////Initialize the LCD1602///////////////////////////////////////////
    LiquidCrystal_I2C      lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
    //                BS  E  D4 D5  D6 D7
    //LiquidCrystal_I2C lcd(7, 8, 9, 10, 11, 12);
    void printDetail(uint8_t type, int value);

    Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

    //int delayval = 250; // delay for half a second
    void setup()
    {
      Serial.begin(115200);

      Serial.println();
      pixels.setBrightness(BRIGHTNESS);
      pixels.begin(); // This initializes the NeoPixel library.
    lcd.begin (16,2); //Define the LCD as 16 column by 2 rows
     lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); //Switch on the backlight
     lcd.setBacklight(HIGH);

    }

    void loop()
    {
    Serial.println("Lithium Atom");
     lcd.setCursor(2,0);
      lcd.print("Helium Atom");
      lcd.setCursor(1,1);
      lcd.print("P=2,N=2 MU=4");

      for(int i=0; i&lt;5; i++)
      {
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.
        pixels.setPixelColor(5+i, pixels.Color(0,255,0));
        pixels.show(); // This sends the updated pixel color to the hardware.
       delay(200);
       pixels.clear();
       //delay(delayval); // Delay for a period of time (in milliseconds).
      }

     // lcd.clear();
    }

    my problem which i need assistance is for Lithium and from is i can iterate other atoms up-to Posassium.

    I want assisted using the ,millis approach because the delay is not running the leds in a smooth pattern.
    lithium code with delay

    
    
    1
    #include &lt;Adafruit_NeoPixel.h&gt;<br />#define PIN_A<br />#define PIN_B<br />#define PIN_A 7<br />#define PIN_A 8<br />#define BRIGHTNESS 10<br />// How many NeoPixels are attached to the Arduino?<br />#define NUMPIXELS_A      24<br /> <br />Adafruit_NeoPixel pixelsA = Adafruit_NeoPixel(NUMPIXELS_A, PIN_A, NEO_GRBW + NEO_KHZ800);<br /> <br />//int delayval = 250; // delay for half a second<br />NEO_GRBW pixelsA;<br />void setup()<br />{<br /> <br />  pixelsA.setBrightness(BRIGHTNESS);<br />  pixelsA.begin(); // This initializes the NeoPixel library.<br />}<br /> <br />void loop()<br />{<br /><br />  for(int i=0; i&lt;=23; i++)<br />  {<br />    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255<br />    pixelsA.setPixelColor(i, pixelsA.Color(0,255,0)); // Moderately bright green color.<br />    pixelsA.show(); // This sends the updated pixel color to the hardware.<br />    delay(10);<br />    pixelsA.setPixelColor(4+i, pixelsA.Color(0,255,0));<br />    pixelsA.show(); // This sends the updated pixel color to the hardware.<br />    delay(15);<br />    pixelsA.setPixelColor(8+i, pixelsA.Color(0,255,0));<br />    pixelsA.show(); // This sends the updated pixel color to the hardware.<br />    delay(20);<br />    pixelsA.clear();<br />   //delay(delayval); // Delay for a period of time (in milliseconds).<br />  }<br />}<br />

    The atoms have energy levels, in this use case am using rings to represent the energy levels.
    For example the first ring with eight leds , 2ndwith 12 leds and so on.

    Mega 2650 board, LCD and ws2812 led rings

    I would also want to use a push button so that it can change the
    elements no. from Hydrogen to Potassium every time you push it.
    So the second ring is put on a different pin , so is the third one , on
    and on

Viewing 2 posts - 1 through 2 (of 2 total)



You must be logged in to reply to this topic.