Hi there!
I am playing a bit around with the Strandtest example from the Adaruit Neopixels library. I have isolated 6 different behaviours (chase in 6 different colours), and I would like to display them randomly.
I have tried a few things but couldnt make a sense of it. My attempt was to make a function for each color and try to call each function randomly, but there is something I am not doing correctly.
Anyone could please check out my code?
thanks!
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 17
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);
long randNumber;
void setup() {
strip.begin();
strip.show();
}
void loop() {
randNumber = random(1, 7);
randNumber;
strip.show();
delay (3000);
}
void chase01 {
theaterChase(strip.Color(127, 0, 0), 50); // Red
}
void chase02 {
theaterChase(strip.Color(0, 0, 127), 50); // Blue
}
void chase03 {
theaterChase(strip.Color(0, 127, 0), 50); // Green
}
void chase04 {
theaterChase(strip.Color(127, 0, 127), 50); // Magenta
}
void chase05 {
theaterChase(strip.Color(0, 127, 127), 50); // Cyan
}
void chase06 {
theaterChase(strip.Color(127, 127, 0), 50); // Yellow
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
randNumber = random(1, 7);
randNumber; // ############# What is this supposed to do?
strip.show();
Is that supposed to be a function call? Where is the function? The parenthesis are missing if it is a function call, parenthesis are required even if there are no parameters to pass.
void chase01 {
This (and others) are missing the parenthesis. Should be
void chase01() {
Where do you use the random number to choose which function to run?
I have just added the parenthesis where they are missing. thanks
and I have "corrected" where RandNumber is supposed to be, but I know this is a wrong attempt anyway. how do you write this to work?
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 17
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);
long RandNumber;
void setup() {
strip.begin();
strip.show();
}
void loop() {
RandNumber = random(1, 7);
chaseRandNumber();
strip.show();
delay (3000);
}
void chase01() {
theaterChase(strip.Color(127, 0, 0), 50); // Red
}
void chase02() {
theaterChase(strip.Color(0, 0, 127), 50); // Blue
}
void chase03() {
theaterChase(strip.Color(0, 127, 0), 50); // Green
}
void chase04() {
theaterChase(strip.Color(127, 0, 127), 50); // Magenta
}
void chase05() {
theaterChase(strip.Color(0, 127, 127), 50); // Cyan
}
void chase06() {
theaterChase(strip.Color(127, 127, 0), 50); // Yellow
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
The chaseRandNumber() function could be like this:
void chaseRandNumber()
{
switch(RandNumber)
{
case 1:
chase01();
break;
case 2:
chase02();
break;
case 3:
chase03();
break;
case 4:
chase04();
break;
case 5:
chase05();
break;
case 6:
chase06();
break;
}
}
The switch-case reference.
That works lovely!
I didnt know how to use switch. Thank you very much for your help!!!
This will be very useful for me in the future!