Page 1 of 1
Forum

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.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Arduino LED effects...
 
Share:
Notifications
Clear all

[Solved] Arduino LED effects example

2 Posts
2 Users
0 Reactions
4,172 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2796
Topic starter  

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.


   
ReplyQuote
(@geowai)
Active Member
Joined: 5 years ago
Posts: 8
 

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 .

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#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<=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

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//#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<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

#include <Adafruit_NeoPixel.h>
#define PIN_A
#define PIN_B
#define PIN_A 7
#define PIN_A 8
#define BRIGHTNESS 10
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS_A 24

Adafruit_NeoPixel pixelsA = Adafruit_NeoPixel(NUMPIXELS_A, PIN_A, NEO_GRBW + NEO_KHZ800);

//int delayval = 250; // delay for half a second
NEO_GRBW pixelsA;
void setup()
{

pixelsA.setBrightness(BRIGHTNESS);
pixelsA.begin(); // This initializes the NeoPixel library.
}

void loop()
{

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

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


   
ReplyQuote
Share: