I'm new but trying to learn.
Recently, I finished an WS2821B LED string sketch and am finding that C++ doesn't return more than 1 value from a function.
I'm looking for ideas on how to get the 'RunColors' function to return 2 values. The value Dir is deeded to determine direction of the LEDs
The sketch runs great, but when you un comment line 75 and comment out the lines below it, the sketch doesn't work. No compiler errors, just doesn't work.
I did place some Serial.print statements and it looks like nether the Pos or the Dir are advanced as anticipated in the function.
Again, I want to use line 75 RunColors(redPos, redDir); to execute the function and not use the rest of the lines of code below. Yes, I'll be adding very many other colors as needed and this will save a bunch of coding.
Please help with suggestions.
#include <FastLED.h>
#define LED_TYPE WS2812B // Type of RGB LEDs
#define DATA_PIN 8 // LED data pin on the Arduino UNO
#define COLOR_ORDER RGB // RGB -GRB // sequence of colors in data streem vs RGB GRB
#define NUM_LEDS 144 // 144 LEDs numbered [0 - 143]
#define BRIGHTNESS 160 // brightness range is 0=off to 255= bright
#define dt 100 // Delay Time
byte redDir = 0;
// Define the array of RGB control data for each LED
CRGB leds[NUM_LEDS];
int LED_length = 3;
int redPos = LED_length;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
randomSeed(analogRead(0));
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
// Function to set runner length
void SetColors(int Pos, int a, int b, int c){
leds[max(Pos - 1, 0)] = CRGB(a, b, c);
leds[Pos] = CRGB(a, b, c);
leds[min(Pos + 1, NUM_LEDS - 1)] = CRGB(a, b, c);
return Pos;
}
// Function to clear colors
void ClearColors(int Pos){
leds[max(Pos - 1, 0)] = CRGB(0,0,0);
leds[Pos] = CRGB(0,0,0);
leds[min(Pos + 1, NUM_LEDS - 1)] = CRGB(0, 0, 0);
return Pos;
}
// Function to set colors (advance down LEDs)
void RunColors(int Pos, int Dir){
if (Dir == 0){ // if going forward
Pos += random(1, 6);
if (Pos >= NUM_LEDS){
Pos = NUM_LEDS - 1;
Dir = 1; // go in reverse
}
}
if (Dir == 1){ // if going reverse
Pos -= random(1, 6);
if (Pos <= 0){
Pos = 0;
Dir = 0; // go forward
}
}
return (Pos, Dir);
}
void loop() {
// Sets Runners Colors
SetColors(redPos, 0, 255, 0);
FastLED.show();
delay(dt);
// Clears Colors
ClearColors(redPos);
FastLED.show();
//RunColors(redPos, redDir);
if (redDir == 0){ // if going forward
redPos += random(1, 6);
if (redPos >= NUM_LEDS){
redPos = NUM_LEDS - 1;
redDir = 1; // go in reverse
}
}
if (redDir == 1){ // if going reverse
redPos -= random(1, 6);
if (redPos <= 0){
redPos = 0;
redDir = 0; // go forward
}
}
}