Wie kan mij helpen met mijn project met WS2812 en FastLED.h

Arduino specifieke Software
Berichten: 1
Geregistreerd: 24 Dec 2022, 14:54

Wie kan mij helpen met mijn project met WS2812 en FastLED.h

Berichtdoor Olivier_degelder » 24 Dec 2022, 15:43

Beste allemaal, Ik ben nieuw op dit form en ben al een hele tijd bezig met een project waar ik niet uit kom. Ik ben nog niet zo goed met Arduino, maar wil het graag leren. Ik heb het internet al aardig afgestruind maar helaas tevergeefs.

Ik heb 4 WS2812 8 leds RGB bordjes, één arduino Uno en een aantal drukknoppen. In de bijlage de opstelling.

Het idee is dat er onder iedere knop één licht preset zit. Bijvoorbeeld: knop 1 (oranje knipperen) knop 2 (wit/rood) ect.

Het is me al gelukt om de lampjes oranje te laten knipperen;

Code: Alles selecteren
#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    32

CRGB leds[NUM_LEDS];

void setup() {

  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
 
}

void loop() {
 
leds[1] = CRGB(255,69,0);
FastLED.show();
leds[3] = CRGB(255,69,0);
FastLED.show();
leds[5] = CRGB(255,69,0);
FastLED.show();
leds[7] = CRGB(255,69,0);
FastLED.show();
leds[9] = CRGB(255,69,0);
FastLED.show();
leds[11] = CRGB(255,69,0);
FastLED.show();
leds[13] = CRGB(255,69,0);
FastLED.show();
leds[15] = CRGB(255,69,0);
FastLED.show();
leds[17] = CRGB(255,69,0);
FastLED.show();
leds[19] = CRGB(255,69,0);
FastLED.show();
leds[21] = CRGB(255,69,0);
FastLED.show();
leds[23] = CRGB(255,69,0);
FastLED.show();
leds[25] = CRGB(255,69,0);
FastLED.show();
leds[27] = CRGB(255,69,0);
FastLED.show();
leds[29] = CRGB(255,69,0);
FastLED.show();
leds[31] = CRGB(255,69,0);
FastLED.show();

delay(50);

leds[1] = CRGB(0, 0, 0);
FastLED.show();
leds[3] = CRGB(0, 0, 0);
FastLED.show();
leds[5] = CRGB(0, 0, 0);
FastLED.show();
leds[7] = CRGB(0, 0, 0);
FastLED.show();
leds[9] = CRGB(0, 0, 0);
FastLED.show();
leds[11] = CRGB(0, 0, 0);
FastLED.show();
leds[13] = CRGB(0, 0, 0);
FastLED.show();
leds[15] = CRGB(0, 0, 0);
FastLED.show();
leds[17] = CRGB(0, 0, 0);
FastLED.show();
leds[19] = CRGB(0, 0, 0);
FastLED.show();
leds[21] = CRGB(0, 0, 0);
FastLED.show();
leds[23] = CRGB(0, 0, 0);
FastLED.show();
leds[25] = CRGB(0, 0, 0);
FastLED.show();
leds[27] = CRGB(0, 0, 0);
FastLED.show();
leds[29] = CRGB(0, 0, 0);
FastLED.show();
leds[31] = CRGB(0, 0, 0);
FastLED.show();

delay(50);

leds[1] = CRGB(255,69,0);
FastLED.show();
leds[3] = CRGB(255,69,0);
FastLED.show();
leds[5] = CRGB(255,69,0);
FastLED.show();
leds[7] = CRGB(255,69,0);
FastLED.show();
leds[9] = CRGB(255,69,0);
FastLED.show();
leds[11] = CRGB(255,69,0);
FastLED.show();
leds[13] = CRGB(255,69,0);
FastLED.show();
leds[15] = CRGB(255,69,0);
FastLED.show();
leds[17] = CRGB(255,69,0);
FastLED.show();
leds[19] = CRGB(255,69,0);
FastLED.show();
leds[21] = CRGB(255,69,0);
FastLED.show();
leds[23] = CRGB(255,69,0);
FastLED.show();
leds[25] = CRGB(255,69,0);
FastLED.show();
leds[27] = CRGB(255,69,0);
FastLED.show();
leds[29] = CRGB(255,69,0);
FastLED.show();
leds[31] = CRGB(255,69,0);
FastLED.show();

delay(50);

leds[1] = CRGB(0, 0, 0);
FastLED.show();
leds[3] = CRGB(0, 0, 0);
FastLED.show();
leds[5] = CRGB(0, 0, 0);
FastLED.show();
leds[7] = CRGB(0, 0, 0);
FastLED.show();
leds[9] = CRGB(0, 0, 0);
FastLED.show();
leds[11] = CRGB(0, 0, 0);
FastLED.show();
leds[13] = CRGB(0, 0, 0);
FastLED.show();
leds[15] = CRGB(0, 0, 0);
FastLED.show();
leds[17] = CRGB(0, 0, 0);
FastLED.show();
leds[19] = CRGB(0, 0, 0);
FastLED.show();
leds[21] = CRGB(0, 0, 0);
FastLED.show();
leds[23] = CRGB(0, 0, 0);
FastLED.show();
leds[25] = CRGB(0, 0, 0);
FastLED.show();
leds[27] = CRGB(0, 0, 0);
FastLED.show();
leds[29] = CRGB(0, 0, 0);
FastLED.show();
leds[31] = CRGB(0, 0, 0);
FastLED.show();

delay(100);

leds[0] = CRGB(255,69,0);
FastLED.show();
leds[2] = CRGB(255,69,0);
FastLED.show();
leds[4] = CRGB(255,69,0);
FastLED.show();
leds[6] = CRGB(255,69,0);
FastLED.show();
leds[8] = CRGB(255,69,0);
FastLED.show();
leds[10] = CRGB(255,69,0);
FastLED.show();
leds[12] = CRGB(255,69,0);
FastLED.show();
leds[14] = CRGB(255,69,0);
FastLED.show();
leds[16] = CRGB(255,69,0);
FastLED.show();
leds[18] = CRGB(255,69,0);
FastLED.show();
leds[20] = CRGB(255,69,0);
FastLED.show();
leds[22] = CRGB(255,69,0);
FastLED.show();
leds[24] = CRGB(255,69,0);
FastLED.show();
leds[26] = CRGB(255,69,0);
FastLED.show();
leds[28] = CRGB(255,69,0);
FastLED.show();
leds[30] = CRGB(255,69,0);
FastLED.show();
leds[32] = CRGB(255,69,0);
FastLED.show();

delay(50);

leds[0] = CRGB(0, 0, 0);
FastLED.show();
leds[2] = CRGB(0, 0, 0);
FastLED.show();
leds[4] = CRGB(0, 0, 0);
FastLED.show();
leds[6] = CRGB(0, 0, 0);
FastLED.show();
leds[8] = CRGB(0, 0, 0);
FastLED.show();
leds[10] = CRGB(0, 0, 0);
FastLED.show();
leds[12] = CRGB(0, 0, 0);
FastLED.show();
leds[14] = CRGB(0, 0, 0);
FastLED.show();
leds[16] = CRGB(0, 0, 0);
FastLED.show();
leds[18] = CRGB(0, 0, 0);
FastLED.show();
leds[20] = CRGB(0, 0, 0);
FastLED.show();
leds[22] = CRGB(0, 0, 0);
FastLED.show();
leds[24] = CRGB(0, 0, 0);
FastLED.show();
leds[26] = CRGB(0, 0, 0);
FastLED.show();
leds[28] = CRGB(0, 0, 0);
FastLED.show();
leds[30] = CRGB(0, 0, 0);
FastLED.show();
leds[32] = CRGB(0, 0, 0);
FastLED.show();

delay(50);

leds[0] = CRGB(255,69,0);
FastLED.show();
leds[2] = CRGB(255,69,0);
FastLED.show();
leds[4] = CRGB(255,69,0);
FastLED.show();
leds[6] = CRGB(255,69,0);
FastLED.show();
leds[8] = CRGB(255,69,0);
FastLED.show();
leds[10] = CRGB(255,69,0);
FastLED.show();
leds[12] = CRGB(255,69,0);
FastLED.show();
leds[14] = CRGB(255,69,0);
FastLED.show();
leds[16] = CRGB(255,69,0);
FastLED.show();
leds[18] = CRGB(255,69,0);
FastLED.show();
leds[20] = CRGB(255,69,0);
FastLED.show();
leds[22] = CRGB(255,69,0);
FastLED.show();
leds[24] = CRGB(255,69,0);
FastLED.show();
leds[26] = CRGB(255,69,0);
FastLED.show();
leds[28] = CRGB(255,69,0);
FastLED.show();
leds[30] = CRGB(255,69,0);
FastLED.show();
leds[32] = CRGB(255,69,0);
FastLED.show();

delay(50);

leds[0] = CRGB(0, 0, 0);
FastLED.show();
leds[2] = CRGB(0, 0, 0);
FastLED.show();
leds[4] = CRGB(0, 0, 0);
FastLED.show();
leds[6] = CRGB(0, 0, 0);
FastLED.show();
leds[8] = CRGB(0, 0, 0);
FastLED.show();
leds[10] = CRGB(0, 0, 0);
FastLED.show();
leds[12] = CRGB(0, 0, 0);
FastLED.show();
leds[14] = CRGB(0, 0, 0);
FastLED.show();
leds[16] = CRGB(0, 0, 0);
FastLED.show();
leds[18] = CRGB(0, 0, 0);
FastLED.show();
leds[20] = CRGB(0, 0, 0);
FastLED.show();
leds[22] = CRGB(0, 0, 0);
FastLED.show();
leds[24] = CRGB(0, 0, 0);
FastLED.show();
leds[26] = CRGB(0, 0, 0);
FastLED.show();
leds[28] = CRGB(0, 0, 0);
FastLED.show();
leds[30] = CRGB(0, 0, 0);
FastLED.show();
leds[32] = CRGB(0, 0, 0);
FastLED.show();

delay(100);

}


En rood/wit;
Code: Alles selecteren
#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    32

CRGB leds[NUM_LEDS];

void setup() {

  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
 
}

void loop() {
leds[0] = CRGB(255,255,255);
FastLED.show();
leds[1] = CRGB(255,255,255);
FastLED.show();
leds[2] = CRGB(255,255,255);
FastLED.show();
leds[3] = CRGB(255,255,255);
FastLED.show();
leds[4] = CRGB(255,255,255);
FastLED.show();
leds[5] = CRGB(255,255,255);
FastLED.show();
leds[6] = CRGB(255,255,255);
FastLED.show();
leds[7] = CRGB(255,255,255);
FastLED.show();
leds[8] = CRGB(255,255,255);
FastLED.show();
leds[9] = CRGB(255,255,255);
FastLED.show();
leds[10] = CRGB(255,255,255);
FastLED.show();
leds[11] = CRGB(255,255,255);
FastLED.show();
leds[12] = CRGB(255,255,255);
FastLED.show();
leds[13] = CRGB(255,255,255);
FastLED.show();
leds[14] = CRGB(255,255,255);
FastLED.show();
leds[15] = CRGB(255,255,255);
FastLED.show();

leds[16] = CRGB(255,0,0);
FastLED.show();
leds[17] = CRGB(255,0,0);
FastLED.show();
leds[18] = CRGB(255,0,0);
FastLED.show();
leds[19] = CRGB(255,0,0);
FastLED.show();
leds[20] = CRGB(255,0,0);
FastLED.show();
leds[21] = CRGB(255,0,0);
FastLED.show();
leds[22] = CRGB(255,0,0);
FastLED.show();
leds[23] = CRGB(255,0,0);
FastLED.show();
leds[24] = CRGB(255,0,0);
FastLED.show();
leds[25] = CRGB(255,0,0);
FastLED.show();
leds[26] = CRGB(255,0,0);
FastLED.show();
leds[27] = CRGB(255,0,0);
FastLED.show();
leds[28] = CRGB(255,0,0);
FastLED.show();
leds[29] = CRGB(255,0,0);
FastLED.show();
leds[30] = CRGB(255,0,0);
FastLED.show();
leds[31] = CRGB(255,0,0);
FastLED.show();
leds[32] = CRGB(255,0,0);
FastLED.show();

}


Nu is mijn vraag hoe ik deze codes kan combineren zodat als ik één knop indruk één licht preset aan gaat. Ik heb het volgende al geschreven;
Code: Alles selecteren
#include <FastLED.h>

#define LED_PIN     7
#define NUM_LEDS    32
#define LED_TYPE    WS2812
#define OVERALL_BRIGHTNESS 255
CRGB leds[NUM_LEDS];

#define buttonPin1 2
#define buttonPin2 3


void setup() {

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness (OVERALL_BRIGHTNESS);
}


void loop() {

  int btnState = digitalRead(buttonPin1);


Maar verder kom ik er niet uit.

Alvast heel erg bedankt voor het helpen en fijne feestdagen!

Olivier

Advertisement

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Wie kan mij helpen met mijn project met WS2812 en FastLE

Berichtdoor shooter » 25 Dec 2022, 20:54

makkellijker is om alle leds een kleur te geven en dan pas een show te doen, en je kunt natuurlijk als een ingang laag wordt dus knop ingedrukt dat je dan een volgende scen neerzet, de leds die uit moeten zijn geef je dus gewoon 0 en de leds aan een andere kleur (dat mag ook 255,255,etc zijn je kunt het ook in een for loop doen en dan telkens een andere kleur etc.
eigenlijk alle leds (ook die uit zijn allemaal een kleur gven (zwart is ook een kleur) en dan telkens en maar een keer per situatie een show doen van alle leds(maar dat regelt fastled al voor je)
paul deelen
shooter@home.nl

Berichten: 80
Geregistreerd: 02 Nov 2022, 13:03

Re: Wie kan mij helpen met mijn project met WS2812 en FastLE

Berichtdoor ThHe » 08 Jan 2023, 13:42

Als eerste stap het programma wat simpeler maken door er een procedure met naam "show" op te nemen.
Ik heb recht toe recht aan je eerste programma omgezet zonder wijziging van functionaliteit.
------------------------------
#include <FastLED.h>

#define LED_PIN 7
#define NUM_LEDS 32

CRGB leds[NUM_LEDS];

// startIndex is de eerste index in leds.
// stap is de sprong naar de volgende index.
// RGB code in de 3 basis kleuren
void show(int startIndex, int stap, int red, int green, int blue)
{
for (int i = startIndex; i < NUM_LEDS; i = i + stap)
{
leds[i] = CRGB(red, green, blue);
FastLED.show();
}
// FastLED.show(); // <<<<<<
delay(50);
}

void setup()
{
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

void loop()
{
show(1, 2, 255, 69, 0);
show(1, 2, 0, 0, 0);
show(1, 2, 255, 69, 0);
show(1, 2, 0, 0, 0);
delay(50); // extra 50
show(0, 2, 255, 69, 0);
show(0, 2, 0, 0, 0);
show(0, 2, 255, 69, 0);
show(0, 2, 0, 0, 0);
delay(50);
}
------------
Het resultaat kan ik alleen fout vrij maken maar niet testen.
FastLED.show() kan buiten de loop worden geplaatst.
delay kan eventueel in de procedure vervallen.

Nu de digitalRead(buttonPin1)
Inhoud van de loop invoeren in een procedure bv:
void showOranje()
{
show(1, 2, 255, 69, 0);
show(1, 2, 0, 0, 0);
show(1, 2, 255, 69, 0);
show(1, 2, 0, 0, 0);
}
De andere in een procedure showRoodWit() bv:
void showRoodWit()
{
show(0, 1, 255, 255, 255);
show(16, 1, 255, 0 , 0);
}
De loop kan dan als volgt wordt ingevuld:
void loop()
{
if (digitalRead(buttonPin1) == HIGH)
{
showOranje();
}
if digitalRead(buttonPin1) == LOW)
{
showRoodWit();
}
}
Leuke basis om verder uit te proberen succes ermee.
---------------
Tip: Als het werkt dan is een kleine uitbreiding van show een stapje voorwaarts:
met deze procedure "Show()" wordt niet het hele leds array gewijzigd maar slechts een aantal en
door de modulo functie (%) kan je ook over de eindgrens hen stappen.
Stel: led 24, 27, 30, 1, 4, 7 een kleur geven door: show(24, 3, 6, ....)

void show(int startIndex, int stap, int aantal, int red, int green, int blue)
{
for (int i = 0; i < aantal; i++)
{
int index = (startIndex + i * stap) % NUM_LEDS;
leds[index] = CRGB(red, green, blue);
}
FastLED.show();
}

Berichten: 80
Geregistreerd: 02 Nov 2022, 13:03

Re: Wie kan mij helpen met mijn project met WS2812 en FastLE

Berichtdoor ThHe » 08 Jan 2023, 17:32

Sorry de loop uit mijn vorige opmerking moet zijn:
void loop()
{
if (digitalRead(buttonPin1) == 1)
{
showOranje();
}
if digitalRead(buttonPin1) == 0)
{
showRoodWit();
}
}
digitalRead geeft als result 0 of 1

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 11 gasten