Hi this is my first attempt at doing non-linear programming.
Ive done more complex stuff but it always gets bogged down due to bad code techniques so I decided to start fresh.
This code does a simple cylon pattern on a Neopixel programmable RGB strip.
Since we cant do instances of a class I wanted to find a way around that and this is how I solved the problem.
The problem is that for every “instance” I need to basically create a whole copy of the function, rename it to something else (cyon1,cylon2, etc), an then also duplicate all of my variables (time1, time 2, etc)
Right now I only am using two instances, but if I tried say 20, then I would have 20 times the functions and 20 times the variables
I also noticed that I cant define the variables inside the functions so in a large complex sketch, the variable list get to be very confusing.
Is there a better way to do this?
Is it possible to define the variables in the functions instead of at the top of the sketch. The reason for this is that in my arduino sketch I opened up many new tabs, each tab is one sequence such as a tab for cylon, another for cylon2, another for flicker3, etc.
It would be easy to understand it if the variables specific to each tab could be in that tab itself (int flag1 in the cylon1 tab, int flage2 in the cylon2 tab, etc)
Any feedback would be really helpful.
neopixel_generic_patterns (main tab)
#include <Adafruit_NeoPixel.h>
unsigned long time1; //timer variable
unsigned long time2; //timer variable
int flag1=1;
int flag2=1;
int r;
int g;
int b;
int currentLED1;
int currentLED2;
uint32_t stripValues[40];//create array to store all led position values
#define PIN 2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
for (int i=0; i<strip.numPixels(); i++){//set all leds to off and load array of off values
stripValues[i]=0;
strip.setPixelColor(i, stripValues[i]);
}
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
cylon1(strip.Color(0, 255,0),10,21,40,3);//color, speed(millis),firstLED,LastLED,number of LEDs in chase
cylon2(strip.Color(0, 0, 255),30,0,20,3);//color, speed(millis),firstLED,LastLED,number of LEDs in chase
showLEDS();//display the loaded array on LED strip
}
void showLEDS(){//display the loaded array on LED strip
for (int i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, stripValues[i]);
}
strip.show();
}
Cylon1 TAB
//Cylon lights (back and forth)
void cylon1(uint32_t c, int wait, int firstLED, int lastLED,int numLEDS) { //color, speed(millis),firstLED,LastLED,number of LEDs in chase
if (millis()-time1>wait){/do something only if delay parameter is met
if (flag1==1) {// if at start of sequence determine first led position
currentLED1=firstLED;
flag1=2;
}
if (flag1==2){//slide leds down strip
for (int i=firstLED; i <= currentLED1; i++) {
stripValues[i]=0; //turn start of chain off
}
for (int i=currentLED1; i < currentLED1+numLEDS; i++) {
stripValues[i]= c; //turn every third pixel on
}
for (int i=currentLED1+numLEDS; i < lastLED; i++) {
stripValues[i]=0; //turn every third pixel on
}
strip.show();
currentLED1+=1;
if (currentLED1+numLEDS>=lastLED || currentLED1>=strip.numPixels()){// if at end of strip or working space then move to next section
flag1=3;
}
}
if (flag1==3){/move LEDS backwards towards start
for (int i=firstLED; i <= currentLED1; i++) {
stripValues[i]=0; //turn start of chain off
}
for (int i=currentLED1; i < currentLED1+numLEDS; i++) {
stripValues[i]=c; //turn every third pixel on
}
for (int i=currentLED1+numLEDS; i < lastLED; i++) {
stripValues[i]=0; //turn every third pixel on
}
strip.show();
currentLED1-=1;
if (currentLED1<=firstLED || currentLED1<=0){//if at start of strip or strip work area then restart requence
flag1=1;
}
}
time1=millis();
}
}
Cylon2 TAB
//Cylon lights (back and forth)
void cylon2(uint32_t c, int wait, int firstLED, int lastLED,int numLEDS) { //color, speed(millis),firstLED,LastLED,number of LEDs in chase
if (millis()-time2>wait){ //do something only if delay parameter is met
if (flag2==1) {// if at start of sequence determine first led position
currentLED2=firstLED;
flag2=2;
}
if (flag2==2){//slide leds down strip
for (int i=firstLED; i <= currentLED2; i++) {
stripValues[i]=0; //turn start of chain off
}
for (int i=currentLED2; i < currentLED2+numLEDS; i++) {
stripValues[i]=c; //turn every third pixel on
}
for (int i=currentLED2+numLEDS; i < lastLED; i++) {
stripValues[i]=0; //turn every third pixel on
}
strip.show();
currentLED2+=1;
if (currentLED2+numLEDS>=lastLED || currentLED2>=strip.numPixels()){// if at end of strip or working space then move to next section
flag2=3;
}
}
if (flag2==3){//move LEDS backwards towards start
for (int i=firstLED; i <= currentLED2; i++) {
stripValues[i]=0; //turn start of chain off
}
for (int i=currentLED2; i < currentLED2+numLEDS; i++) {
stripValues[i]=c; //turn every third pixel on
}
for (int i=currentLED2+numLEDS; i < lastLED; i++) {
stripValues[i]=0; //turn every third pixel on
}
strip.show();
currentLED2-=1;
if (currentLED2<=firstLED || currentLED2<=0){//if at start of strip or strip work area then restart requence
flag2=1;
}
}
time2=millis();//reset timer
}
}