I'm having issues writing a code that would allow my LED strip to create a wave effect and then blink at random intervals with different colors. the wave effect is in blue but the blink effect would be in red and green. I was able to find the Wave effect and modify it but I don't really know how to do the random blink effect and add that in the same code.
Show what you have.
How many LEDs (WS2812) are in a strip?
How many LEDs do you want blinking at one time?
How often do you want the blink to occur?
How long will the blink stay on?
What color do you want on each LED?
24 on the strip
probably 4 at one time
I probably want it to blink pretty often then every ten minutes the wave effect comes on.
I'd like the blink to stay on for a few seconds
for the LEDs that are on blinking I would like green and red
this is what i have for the wave effect:
#include <FastLED.h>
CRGB led[30];
//=====================================================
void setup()
{
FastLED.addLeds<WS2812B, 4, GRB>(led, 30);
FastLED.setBrightness(100);
}
//=====================================================
void loop()
{
uint8_t sine = beatsin8(15, 0, 23, 0, 0);
led[sine] = CHSV(160, 255, 255);
blur1d(led, 24, 50);
fadeToBlackBy(led, 24, 1);
FastLED.show();
}
Sorry, the idea is to have different LEDs to blink at random red and green then after ten minutes a cool wave effect comes on
How long does the wave last?
24 or 30 LEDs?
During the blink... ON for (a few seconds) off for how long?
I only have 24 on the strip, I’ll fix the 30! And I would like it on for let’s say 5 seconds off 3 seconds and the wave effect maybe last for 3 minutes
You could possibly try something with rand().
Something like
#include <stdlib.h>
#include <time.h>
//#include <string.h>
char* pick_colour(int seed) {
srand(seed);
int col = (rand() % 3) + 1;
switch(col) {
case 1:
return "red";
break;
case 2:
return "green";
break;
case 3:
return "blue";
break;
default:
return "none";
break;
}
}
void setup() {
//do whatever
pinMode(#, OUTPUT);
}
void loop() {
char colour[8] = pick_colour(time(NULL));
if() {}
if() {}
if() {}
}
@tgc135
pinMode()
, stdlib
and time
would not be necessary in the Arduino IDE. pinMode()
is done by FastLED object, and the other two are built-in.
You will want to read about "arduino multiple events" and "finite state machine"...
Here is what I see...
- starting in "state 0,"
- blink the random four LEDs red or green.
- After the blink, determine when "10 minutes" occurs,
- move into "state 1" for the wave to begin (stop "blinking" and start "waving").
- determine when "3 minutes" has passed, which will return to "state 0" (stop "waving" and start "blinking").
- During "blinking" hold the random LEDs ON for 5000ms, then OFF for 3000ms.
Given your setup()
, wave()
and blink()
are complete functions... loop()
might look something like this...
void loop() {
if (millis() - waveTimer > waveStart) { // check wave start time
waveTimer = millis(); // reset waveTimer for next waveStart
showWave = 1; // state 1
}
if (showWave && millis() - stopTimer > waveStop) { // check state is 1 and wave stop time
stopTimer = millis();
showWave = 0; // back to state 0
}
if (showWave) // if state 1... wave
wave();// stop blinking and start waving
else // if not state 1 (if state 0)... blink
blink(); //stop waving and start blinking
}
sorry I am new to this, so in the set up I will have to define the wave and blink and then in loop it should look like the example you have given? Just trying to wrap my head around this.
Careful on the terms you use...
setup()
is one of the two function names required by the Arduino IDE. loop()
is the other function name. See IDE >> FILE >> EXAMPLES >> BUILT-IN EXAMPLES >> 01 BASIC >> minimal.ino. Here: https://docs.arduino.cc/built-in-examples/basics/BareMinimum/
As that page says, setup()
runs one time, usually to allow run-time configuration of the hardware, and loop()
runs continuously.
The sketch you have in Post #3 uses loop()
to run the wave
function. I used loop()
to call a function I called wave()
. Using functions allows for modular building of a program. Sketch #3 also identifies the hardware and libraries to be used.
#include <FastLED.h>
CRGB led[24]
//===========================================================================
void setup() {
FastLED.addLeds<WS2812B, 4, GRB>(led, 24);
FastLED.setBrightness(100);
uint8_t sine = beatsin8(15, 0, 23, 0, 0);
led[sine] = CHSV(160, 255, 255);
blur1d(led, 24, 50);
fadeToBlackBy(led, 24, 1);
FastLED.show()
char* pick_colour(int seed) {
srand(seed);
int col = (rand() % 3) + 1;
switch(col) {
case 1:
return "red";
break;
case 2:
return "green";
break;
case 3:
return "blue";
break;
default:
return "none";
break;
}
}
}
void loop() {
if (millis() - waveTimer > waveStart) { // check wave start time
waveTimer = millis(); // reset waveTimer for next waveStart
showWave = 1; // state 1
}
if (showWave && millis() - stopTimer > waveStop) { // check state is 1 and wave stop time
stopTimer = millis();
showWave = 0; // back to state 0
}
if (showWave) // if state 1... wave
wave();// stop blinking and start waving
else // if not state 1 (if state 0)... blink
blink(); //stop waving and start blinking
}
so something along the lines like this?
still trying to work out the errors though
Does it (sketch post 12) work as you intended?
Show the errors and indicate the sketch you are using.
these are the error messages I am currently getting:
error: expected initializer before 'void'
void loop() {
^~~~
In function 'void setup()':
: error: 'led' was not declared in this scope
FastLED.addLeds<WS2812B, 4, GRB>(led, 24);
^~~
error: expected ';' before 'char'
char* pick_colour(int seed) {
^~~~
In function 'void loop()':
error: 'waveTimer' was not declared in this scope
if (millis() - waveTimer > waveStart) { // check wave start time
^~~~~~~~~
'waveStart' was not declared in this scope
if (millis() - waveTimer > waveStart) { // check wave start time
^~~~~~~~~
: note: suggested alternative: 'va_start'
if (millis() - waveTimer > waveStart) { // check wave start time
^~~~~~~~~
va_start
error: 'showWave' was not declared in this scope
showWave = 1; // state 1
^~~~~~~~
error: 'showWave' was not declared in this scope
if (showWave && millis() - stopTimer > waveStop) { // check state is 1 and wave stop time
^~~~~~~~
error: 'stopTimer' was not declared in this scope
if (showWave && millis() - stopTimer > waveStop) { // check state is 1 and wave stop time
^~~~~~~~~
error: 'waveStop' was not declared in this scope
if (showWave && millis() - stopTimer > waveStop) { // check state is 1 and wave stop time
^~~~~~~~
error: 'showWave' was not declared in this scope
if (showWave) // if state 1... wave
^~~~~~~~
error: 'wave' was not declared in this scope
wave();// stop blinking and start waving
^~~~
error: 'blink' was not declared in this scope
blink(); //stop waving and start blinking
^~~~~
exit status 1
Compilation error: expected initializer before 'void'
Which code are you using? Let us find the errors.
This following code works for me (slightly adapted)...
#include <FastLED.h>
CRGB led[30];
#define PIN 6 // pin number
#define PIX 24 // number of pixels
#define MAXBRT 255 // max brightness
const int blinkers = 4;
int randomLED[blinkers], onTime = 5000, offTime = 3000;
bool ledState, showWave;
unsigned long waveTimer, stopTimer;
unsigned long waveStart = 10 * 60 * 1000; // min * sec/min * ms/sec
unsigned long waveStop = 3 * 60 * 1000;
void setup() {
randomSeed(analogRead(A0));
FastLED.addLeds<WS2812B, PIN, GRB>(led, PIX);
FastLED.setBrightness(MAXBRT);
}
void loop() {
if (millis() - waveTimer > waveStart) { // check wave start time
waveTimer = millis(); // reset waveTimer for next waveStart
showWave = 1;
}
if (showWave && millis() - stopTimer > waveStop) { // check wave stop time
stopTimer = millis();
showWave = 0;
}
if (showWave)
wave();// state to stop blinking and start waving
else
blink(); // time to stop waving and blink
}
void wave() {
uint8_t sine = beatsin8(15, 0, 23, 0, 0);
led[sine] = CHSV(160, 255, 255);
blur1d(led, 24, 50);
fadeToBlackBy(led, 24, 1);
FastLED.show();
}
void blink() {
for (int i = 0; i < blinkers; i++) { // number of blinking LEDs
randomLED[i] = random(PIX);
ledState = random (2); // random 0 or 1
led[randomLED[i]] = CRGB(255 * ledState, 255 * !ledState, 0); // color the buffer red, green, blue
}
FastLED.show(); // show the ON LEDs
delay(onTime); // keep them on
for (int i = 0; i < blinkers; i++) {
led[randomLED[i]] = CRGB(0, 0, 0); // clear the buffer
}
FastLED.show(); // show the buffer
delay(offTime); // keep them off
}