This is my current project. Very rough, I'm still learning how to use Classes.
I want my project to use a tact button to run through 4 functions.
1:Green Fire Flicker
2:Blue Fire Flicker
3:Red Fire Flicker
4:Rainbow Fade
I can get the Rainbow fade to work by itself and I can get the 3 different fire flickers to work by themselves with a debounce function to switch between them.
But the second I add the Rainbow Fade to the Case structure it doesn't run right. I was told to check out Classes so the fade can essentially run over and over and still being able to use the button to debounce back to a default before restarting the processes all over again.
Any help would be appreciated, Thank you
#define redone 11
#define greenone 10
#define blueone 9
#define redtwo 6
#define greentwo 5
#define bluetwo 3
#define button 4
class greenFire
{
public:
greenFire()
{
pinMode (redone, OUTPUT);
pinMode (blueone, OUTPUT);
pinMode (greentwo, OUTPUT);
pinMode (redtwo, OUTPUT);
pinMode (bluetwo, OUTPUT);
pinMode (greentwo, OUTPUT);
}
void update()
{
analogWrite (redone, 0);
analogWrite (blueone, 30);
analogWrite (greenone, random(120)+ 135);
analogWrite (redtwo, 0);
analogWrite (bluetwo, 15);
analogWrite (greentwo, random(120)+ 135);
}
};
class blueFire
{
public:
blueFire()
{
pinMode (redone, OUTPUT);
pinMode (blueone, OUTPUT);
pinMode (greentwo, OUTPUT);
pinMode (redtwo, OUTPUT);
pinMode (bluetwo, OUTPUT);
pinMode (greentwo, OUTPUT);
}
void update()
{
analogWrite (redone, 0);
analogWrite (blueone, random(120)+ 135);
analogWrite (greenone, 100);
analogWrite (redtwo, 0);
analogWrite (bluetwo, random(120)+ 135);
analogWrite (greentwo, 50);
}
};
class redFire
{
public:
redFire()
{
pinMode (redone, OUTPUT);
pinMode (blueone, OUTPUT);
pinMode (greentwo, OUTPUT);
pinMode (redtwo, OUTPUT);
pinMode (bluetwo, OUTPUT);
pinMode (greentwo, OUTPUT);
}
void update()
{
analogWrite (redone, random(120)+ 135);
analogWrite (blueone, 0);
analogWrite (greenone, 15);
analogWrite (redtwo, random(120)+ 135);
analogWrite (bluetwo, 0);
analogWrite (greentwo,5);
}
};
class rgb
{
int redone = 11;
int greenone = 10;
int blueone = 9;
int redtwo = 6;
int greentwo = 5;
int bluetwo = 3;
int counter = 0;
int numColors = 255;
int animationDelay = 15;
public:
rgb()
{
pinMode (redone, OUTPUT);
pinMode (blueone, OUTPUT);
pinMode (greentwo, OUTPUT);
pinMode (redtwo, OUTPUT);
pinMode (bluetwo, OUTPUT);
pinMode (greentwo, OUTPUT);
}
void update()
{
// This part takes care of displaying the
// color changing in reverse by counting backwards if counter
// is above the number of available colors
float colorNumber = counter > numColors ? counter - numColors: counter;
// Play with the saturation and brightness values
// to see what they do
float saturation = 1; // Between 0 and 1 (0 = gray, 1 = full color)
float brightness = 1; // Between 0 and 1 (0 = dark, 1 is full brightness)
float hue = (colorNumber / float(numColors)) * 360; // Number between 0 and 360
long color = HSBtoRGB(hue, saturation, brightness);
// Get the red, blue and green parts from generated color
int red = color >> 16 & 255;
int green = color >> 8 & 255;
int blue = color & 255;
setColor(red, green, blue);
// Counter can never be greater then 2 times the number of available colors
// the colorNumber = line above takes care of counting backwards (nicely looping animation)
// when counter is larger then the number of available colors
counter = (counter + 1) % (numColors * 2);
// If you uncomment this line the color changing starts from the
// beginning when it reaches the end (animation only plays forward)
// counter = (counter + 1) % (numColors);
delay(animationDelay);
}
void setColor (unsigned char red, unsigned char green, unsigned char blue)
{
analogWrite(redone, red);
analogWrite(greenone, green);
analogWrite(blueone, blue);
analogWrite(redtwo, red);
analogWrite(greentwo, green);
analogWrite(bluetwo, blue);
}
long HSBtoRGB(float _hue, float _sat, float _brightness) {
float red = 0.0;
float green = 0.0;
float blue = 0.0;
if (_sat == 0.0) {
red = _brightness;
green = _brightness;
blue = _brightness;
} else {
if (_hue == 360.0) {
_hue = 0;
}
int slice = _hue / 60.0;
float hue_frac = (_hue / 60.0) - slice;
float aa = _brightness * (1.0 - _sat);
float bb = _brightness * (1.0 - _sat * hue_frac);
float cc = _brightness * (1.0 - _sat * (1.0 - hue_frac));
switch(slice) {
case 0:
red = _brightness;
green = cc;
blue = aa;
break;
case 1:
red = bb;
green = _brightness;
blue = aa;
break;
case 2:
red = aa;
green = _brightness;
blue = cc;
break;
case 3:
red = aa;
green = bb;
blue = _brightness;
break;
case 4:
red = cc;
green = aa;
blue = _brightness;
break;
case 5:
red = _brightness;
green = aa;
blue = bb;
break;
default:
red = 0.0;
green = 0.0;
blue = 0.0;
break;
}
}
long ired = red * 255.0;
long igreen = green * 255.0;
long iblue = blue * 255.0;
return long((ired << 16) | (igreen << 8) | (iblue));
}
};
void setup()
{
pinMode (button, INPUT);
pinMode (redone, OUTPUT);
pinMode (blueone, OUTPUT);
pinMode (greentwo, OUTPUT);
pinMode (redtwo, OUTPUT);
pinMode (bluetwo, OUTPUT);
pinMode (greentwo, OUTPUT);
analogWrite (redone, 0);
analogWrite (blueone, 0);
analogWrite (greenone, 0);
analogWrite (redtwo, 0);
analogWrite (bluetwo, 0);
analogWrite (greentwo, 0);
}
void loop() {
buttonPoll = digitalRead(button);
if(buttonPoll == 1) {
delay(50);
buttonPoll = digitalRead(button);
if(buttonPoll == false) {
state = old + 1;
}}
else{
delay(100);
}
switch (state) {
case 1: //Green Flame
greenFire.update();
old = state;
break;
case 2: //Blue Flame
blueFire.update();
old = state;
break;
case 3: //Red Flame
redFire.update
old = state;
break;
case 4: //RGB
rgb.update
old = state;
break;
default: //if state is not 1,2
analogWrite (redone, 0); //turn red led to off
analogWrite (blueone, 0); //turn blue led to off
analogWrite (greenone, 0); //turn green led to off
analogWrite (redtwo, 0); //turn red led to off
analogWrite (bluetwo, 0); //turn blue led to off
analogWrite (greentwo, 0); //turn green led to off
old = 0; //reset to off/state 0
break;
}
}
Flickering_Bone_Club_Classes.ino (6.6 KB)