Go Down

Topic: question about running ws2812 leds (Read 301 times) previous topic - next topic

Nataschabakker

Hey,

For a while I work with neopixel ws2812 LEDs and works fine. Only I always use example skins because self programming is quite difficult. Now I have found a program that helps me to find colors for these LEDs but I want to add something.

Below you will find a sketch what this program generates, in it the colors red white blue and now I want to let it run at a certain speed, for example every second changed to red becomes white, white becomes blue and blue becomes red. Can someone change what I have to do for this. How do I tell arduino that he has to walk and the speed of that. If I know that, then I'll get out of it.

thanks in advance

Code: [Select]

#include <Adafruit_NeoPixel.h>

#define PIN 6

uint32_t ledarray[] = {
  0x000000FF, 0x00FFFFFF, 0x00FF0000,
  0x00FF0000, 0x000000FF, 0x00FFFFFF,
  0x00FFFFFF, 0x000000FF, 0x00FF0000,
};



Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  strip.begin();
  strip.show();   // Initialize all pixels to 'off'
  RenderFrame();
}

void loop()
{
  // nothing needed here :(
}

void RenderFrame()
{
  for (int t = 0; t < 40; t++)
  {
    strip.setPixelColor(t, ledarray[t]);
  }

  strip.show();
}


larryd

Hint:
Code: [Select]

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define ledCount 3
uint32_t ledarray[] =
{
  0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFF7F00, 0x00FFFF, 0xFF2000, 0xFFFFFF, 0x000000
};

Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledCount, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  strip.begin();
  strip.show();   // Initialize all pixels to 'off'
}

void loop()
{
  for (int t = 0; t < 9; t++)
  {
    for (int y = 0; y < ledCount; y++)
    {
      strip.setPixelColor(y, ledarray[t]);
      strip.show();
      delay(500);
    }
  }
}


No technical PMs.
The last thing you did is where you should start looking.

Nataschabakker

Thank you, it works so well indeed. Can I be so brazen to ask a question?

I have a print with 6 x ws2812 digital LEDs, but I do not have 1 print but 20 prints in total, so 120 x ws2812 leds. all prints are connected with one controller / arduino but I want all prints to do the same. so led1 and led7 led13 led19 do the same as led2, led8, led14 ect. I will add a picture, this is the photo of 1 print with 6 ws2812 LEDs so there are 20 prints of how to make the schedule so that they all do the same.



Thanks


DrAzzy

#3
Nov 22, 2017, 09:14 pm Last Edit: Nov 22, 2017, 09:16 pm by DrAzzy
Loop over all the 120 LEDs, and (assuming the index in the for loop is 'i') take i%6 - that will give you a number from 0~5 corresponding to the position of that led on the board, which you can use to look up what color to make it.
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Nataschabakker

oke thanks,

Is there a example from, so where i put it in?
One of the first line of by every kind of led program/skecht

Thanks a again.

Happy  :)

larryd

#5
Nov 22, 2017, 11:46 pm Last Edit: Nov 22, 2017, 11:49 pm by larryd
@Nataschabakker

Do you understand how this works
for(byte y = 0; y < ledCount; y++)
{
. . .
}

Do you know what this equals
7 % 6 = ?


No technical PMs.
The last thing you did is where you should start looking.

Nataschabakker

Larry,

thank you for your answer.

I do not understand it at all but mostly I load file sketch and then change a few things, I often learn well from this, I also understand that when I see it happening. little by little I understand.
When I read this code, I think it says when the first LED starts and how many LEDs are present. is that right? Y++ counts up. I often try it in a schematic and by trying and changing, I quickly find out what it does exactly.

It is quite difficult to create your own LED scheme, for example 2 white 2 red and 2 blue running and if he has done 4 times everything in orange changed as an example. Therefore, working with 00ff00 code seems easier than this but it is good to learn how things can be done differently.


Grumpy_Mike

What Larry was asking about was this https://en.m.wikipedia.org/wiki/Modular_arithmetic
It is a way of turning numbers that go on getting bigger into a sequence of numbers from zero to some value that keep cycling. It is a very useful concept if you want to write the sort of code you are trying to write.

Nataschabakker

okay no, that's pretty complicated for me.

larryd

#9
Nov 23, 2017, 04:49 pm Last Edit: Nov 23, 2017, 05:10 pm by larryd
See if you can follow the code in this example:

Code: [Select]

//WalkingMarkeeVer2
#include <Adafruit_NeoPixel.h>

#define wait 1000         //viewing time

bool allowWalking = true; //true  >>>---> allows the colors to walk
//                          false >>>---> stationary

#define ledCount 12       //total number of LEDs  <----<<< change to 120, when all LEDs are attached
#define PIN 6             //output pin to the LED strip
#define LEDsPerBoard 6    //number of LEDs on a PCB

int offset  = 0;          //offset or index into colourArray[]

unsigned long colourArray[] =  //colour code sequence
{
  //example 1:
  //red       green     blue      pink      yellow    white     cyan      orange     black
  //0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFF7F00, 0xFFFFFF, 0x00FFFF, 0xFF3000,  0x000000

  //example 2:
  //red       green     blue      white
  //0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF

  //example 3:
  //red     red       green     green     blue      blue      pink      pink      white     white
  //0xFF0000, 0xFF0000, 0x00FF00, 0x00FF00, 0x0000FF, 0x0000FF, 0xFF00FF, 0xFF00FF, 0xFFFFFF, 0xFFFFFF

  //example 4:
  //red       green     blue      pink      yellow    white
  0x3F0000, 0x003F00, 0x00003F, 0x3F003F, 0x3F3F00, 0x3F3F3F    //dimmer

  //example 5:
  //red       green     blue      pink      yellow    white
  //0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFF7F00, 0xFFFFFF  //bright
};

//how many colours do we have
const byte availableColors = sizeof(colourArray) / sizeof(unsigned long);

Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledCount, PIN, NEO_GRB + NEO_KHZ800);

//***********************************************************************************
void setup()
{
  strip.begin();
  strip.show();   // Initialize all pixels to 'off'

}// END of setup()

//***********************************************************************************
void loop()
{
  //cycle through all the colours
  for (byte colourSelect = 0; colourSelect < availableColors; colourSelect++)
  {
    //fill the pixel array memory
    for (byte ledNumber = 0; ledNumber < ledCount; ledNumber++)
    {
      strip.setPixelColor(ledNumber, colourArray[offset]);

      //the next index/offset into colourArray[]
      offset++;
      offset = offset % availableColors;
      
    }// END of for()

    //send the pixel array memory to the LEDs
    strip.show();

    //give some time to view the strip
    delay(wait);

    //*********************
    //move to the next item in colourArray[]
    if (allowWalking == true)
    {
      offset = colourSelect;
    }
    //*********************
    
  }// END of for()
  
}// END of loop()

//***********************************************************************************

No technical PMs.
The last thing you did is where you should start looking.

Go Up