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!



Fastled ws2812b syn...
 
Share:
Notifications
Clear all

[Solved] Fastled ws2812b synch with audio DFplayer mini

10 Posts
2 Users
0 Reactions
3,502 Views
(@geowai)
Active Member
Joined: 5 years ago
Posts: 8
Topic starter  

Hi Hans

I have tested your idea and works perfectly on a section of the code
with actual customization of my requirement. This has given me a heads
up on other sections that operate in the same way.

Am confident with your assistance I will manage the project as per expected timelines.

My other section has the leds in a matrix of 4×4, which starts with
two colors ( yellow& blue) that change gradually to a different
color as they keep blinking to red( imagine molecules reacting to form a
new product)

An idea oh how this can be achieved will be most welcome.

Thank you so much.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Geowai!

I just noticed that you've already posted in the forum 

Anyhoo, could you elaborate on the 4x4 matrix?
I'm not sure I understand the effect you're looking for.


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

New project.

HI Hans and team,

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>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define PIN_A 3
#define PIN_B 4
//#define PIN 7
//#define PIN 8
#define BRIGHTNESS 30
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS_A      3
#define NUMPIXELS_B      4



//////////////////////////////////////////////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(uint32_t type, int value);

Adafruit_NeoPixel pixels_A = Adafruit_NeoPixel(NUMPIXELS_A, PIN_A, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel pixels_B = Adafruit_NeoPixel(NUMPIXELS_B, PIN_B, NEO_GRBW + NEO_KHZ800); 
//int delayval = 250; // delay for half a second
unsigned long interval = 100;
unsigned long previousMillis  = 0; 
//const unsigned long pixel_B = 0;

//unsigned long interval pixel_B = 100;
void setup()
{
  //Serial.begin(115200);

 // Serial.println();
 pixels_A.setBrightness(BRIGHTNESS);
  pixels_A.begin(); // This initializes the NeoPixel library.
 pixels_B.setBrightness(BRIGHTNESS);
  pixels_B.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()
{
if ((unsigned long)(millis() - previousMillis) >= interval) {
  previousMillis = millis();

 int i,j;
 lcd.setCursor(2,0);
  lcd.print("Lithium Atom");
  lcd.setCursor(1,1);
  lcd.print("P=3 N=4 MU=?");

    }

 for (unsigned int i=0; i<4; i++) for (unsigned int j=0; j<12; j++)
  {

  pixels_A.setPixelColor(i, pixels_A.Color(255,0,0)); // Moderately bright green color.
    pixels_A.setPixelColor(4+i,pixels_A.Color(0,255,0));

   // pixels_B.setPixelColor(j, pixels_B.Color(0,0,255(50))); // Moderately bright green color.
   //  pixels_A.show(); // This sends the updated pixel color to the hardware.
   }

  // delay(10);
 //   pixels_A.clear();
 // pixels_B.setPixelColor(j, pixels_B.Color(0,0,255)); // Moderately bright green color.
// delay(50);
// pixels_B.show(); // This sends the updated pixel color to the hardware.
// pixels_B.clear();



   //for (int j=0; j<12; j++)
   // { pixels_B.setPixelColor(j, pixels_B.Color(0,0,255)); // Moderately bright green color.
   // pixels_B.show(); // This sends the updated pixel color to the hardware.

   // delay(100);
    //pixels_B.clear();
   //delay(delayval); // Delay for a period of time (in milliseconds).
  }

 lcd.clear();

}

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
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Geoway!

This for sure is a very interesting project, do you have a little video showing the effect?
Unfortunately, I do not have a LED array (circular) like you have, so testing or experimentation will not be an option.

Two things that came to mind though is this (based on my limited knowledge on the topic);

1) Using a function that calculates the position of the electrons may never go smooth.

2) You could try (I'm not sure how "big" it would become) making an array for each "frame".

With 2) I mean: for each "move" or "frame" (like in a video for example), define all LEDs.
Then with each step display a "frame".

An oversimplified example of 4 LEDs, with 1 LED walking from left to right

Note: a color in the NeoPixel library can be represented as a 32bit integer (uint32_t).

uint32_t frame1 [] = { FFFFFF, 000000, 000000, 000000 };
uint32_t frame2 [] = { 000000, FFFFFF, 000000, 000000 };
uint32_t frame3 [] = { 000000, 000000, FFFFFF, 000000 };
uint32_t frame4 [] = { 000000, 000000, 000000, FFFFFF };
byte frame_count = 4;

Of course, you can use 2 dimensional arrays as well 

uint32_t frames [][] = { { FFFFFF, 000000, 000000, 000000 },
                         { 000000, FFFFFF, 000000, 000000 },
{ 000000, 000000, FFFFFF, 000000 },
{ 000000, 000000, 000000, FFFFFF };

Then create a for-loop that assigns the values to the LEDs, one frame at a time, show, and then the next frame (you can determine the size of an array also with the "sizeof(array)" function).

Maybe this helps ...


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

Thanks Hans for ideas.

I will send circuit connection, short video on what is working and where I am.

We can use Tinkard to simulate the code.


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

Atom animation.

Hi Hans.

Here is the video clip on Helium. Challenges start from Lithium onwards because i have to use the 2nd ring , and my code is not smooth because the leds in ring one have to move before the ones in ring two move, this is nor good animation because of the delay function, how can I implement the millis function so that they can run concurrently without interrupting each other?

Here is the video clip on Helium Atom


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

Hi Hans.

here is the link on Tinkard  with the Helium simulation.

Helium simulation on Tinkard


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

Hi Hans.

Help improve on this code so that the led animation runs smoothly, maybe with use of millis.

Below is the video of how its running. notice the red and green lighting have to move first before the other blue circles the ring. I want them to move seamlessly.

Thanks

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define PIN_A 3
#define PIN_B 4
//#define PIN 7
//#define PIN 8
#define BRIGHTNESS 30
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS_A 8
#define NUMPIXELS_B 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(uint32_t type, int value);

Adafruit_NeoPixel pixels_A = Adafruit_NeoPixel(NUMPIXELS_A, PIN_A, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel pixels_B = Adafruit_NeoPixel(NUMPIXELS_B, PIN_B, NEO_GRBW + NEO_KHZ800);

void setup()
{
//Serial.begin(115200);

// Serial.println();
pixels_A.setBrightness(BRIGHTNESS);
pixels_A.begin(); // This initializes the NeoPixel library.
pixels_B.setBrightness(BRIGHTNESS);
pixels_B.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()
{

unsigned int i,j;
lcd.setCursor(2,0);
lcd.print("Lithium Atom");
lcd.setCursor(1,1);
lcd.print("P=3 N=4 MU=?");


for (unsigned int i=0; i<4; i++) for (unsigned int j=0; j<12; j++)
{

pixels_A.setPixelColor(i, pixels_A.Color(255,0,0));
pixels_A.setPixelColor(4+i,pixels_A.Color(0,255,0));
pixels_A.show(); // This sends the updated pixel color to the hardware.
delay(50);
pixels_A.clear();
pixels_B.setPixelColor(j, pixels_B.Color(0,0,255));
pixels_B.show(); // This sends the updated pixel color to the hardware.
delay(100);
pixels_B.clear();
}

}


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Hi Geowai!

This sounds a little more complex than I may be able to help you with.
The Tinkard link (by the way: thanks for mention this site - I had not heard of them before!) gave me an "410 - The requested page has expired" error 

To have LEDs move smoother you'll either need a more potent controller, maybe something like a Raspberry Pi?
OR ... use the approach I suggested.

The Arduino isn't really designed for doing multiple things at the same time.
So having the nucleus and the atoms move at almost the same time you'd need to come up with a "task switching" mechanism, which makes the sketch much more complicated.

A good example (also showing the increased complexity) can be found here.


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

Hi Hans.

This is a continuation of the earlier code into another Pin 3( previous one was Pin 2)

The matrix has 16 ws2812b leds in a "SerpentineLayout' . I would want to animate a random  reaction( bombardment ) between  yellow and blue coloured leds that would gradually change as the bombardment continues to generate a few green led in a consequential manner until the whole matrix changes to green.

See the GIF below to demonstrate ref: https://www.google.com/url?sa=i&rct=j&q=&esrc ="s&source=imgres&cd=&cad=rja&uact=8&ved=2ahUKEwitnOXug-zlAhXZ6eAKHWIHAjYQjRx6BAgBEAQ&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FKinetic_theory_of_gases&psig=AOvVaw1Om3gIRwrasaE8YGKTPNf5&ust=1573900888987936"

I hope this visualizes my ideas.

Thanks.


   
ReplyQuote
Share: