Hello.I have an arduino uno , a keypad 4x3 and a led strip that consists of 30 leds (5V).
I have done many animations with this led strip and now I'm using this keypad so that I control the colors , the animations & the brightness.
I will show you 2 animations
1st animation: Just colors.When I push number '1' on keypad,I turn on red (for instance) ,when I push again number '1' I change color and so on.When I push '*' ,I decrease the brightness of each color and with '#' I increase the brightness.No problem with 1st animation...
2nd animation: When I push number '2' , I turn on an animation that fade in and fade out all leds with green color (for instance). When I push again number '2', I change color directly and led strip continues with this rhythm with no problems.
Mix of 1st & 2nd animation: Let's say I push number '1' so I have RED color ,then I push number '2' led strip does it's job (fade in and fade out). But ,when I push again number '1' ,led strip continues fade in and fade out and after a lot of presses of number '1' (4-7 presses) , finally I go to 1st animation.
I told you a lot so that you can understand my problem and what I have done so far.
Code (a part of it):
#include <FastLED.h>
#include <Keypad.h>
#define LED_STRIP_PIN 13
#define NUM_OF_LEDS 30
CRGB leds[NUM_OF_LEDS];
int brightness = 255;
int pick_color_variable = 0;
int fade_variable = 0;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5};
byte colPins[COLS] = {4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Number of colors: 18
CRGB array_colors[] = {CRGB::Red , CRGB::Green , CRGB::Blue , CRGB::Yellow , CRGB::GreenYellow , CRGB::DeepSkyBlue , CRGB::Olive , CRGB::Navy , CRGB::Magenta , CRGB::Orange ,
CRGB::DeepPink , CRGB::Chocolate , CRGB::Coral , CRGB::Indigo , CRGB::Cyan , CRGB::Purple , CRGB::White , CRGB::Black
};
void setup()
{
FastLED.addLeds<WS2812B, LED_STRIP_PIN, GRB>(leds, NUM_OF_LEDS);
FastLED.setBrightness(brightness);
pick_color(array_colors[17] , 0);
Serial.begin(9600);
}
void pick_color(CRGB color , int brightness)
{
for(int i = 0; i < NUM_OF_LEDS; i++)
{
leds[i] = color;
FastLED.setBrightness(brightness);
FastLED.show();
}
}
void fade_in_fade_out(CRGB color , char key , int b , bool UP , bool enable__ , bool keep)
{
int var;
bool _1 = false;
while(keep)
{
if(UP) // fade in
{
if(enable__) // if true ,then (in case I have changed color) the brightness continues from the last value brightness
{
var = b;
}
else // otherwise , brightness starts from the beginning
{
var = 0;
}
for(int k = var; k < 255; k += 3)
{
for(int i = 0; i < NUM_OF_LEDS; i++)
{
key = keypad.getKey();
if(key == '1')
{
keep = false; // stop while loop
_1 = true; // number '1' just pressed
break; // exit from inner for loop
}
else if(key == '2')
{
if(fade_variable < 16)
{
fade_variable++;
}
else
{
fade_variable = 0;
}
fade_in_fade_out(array_colors[ fade_variable ] , key , k , true , true , true);
}
else // if no key has pressed , continue your job
{
enable__ = false;
UP = false; // This gets false , so that leds fade out
}
FastLED.setBrightness(k);
leds[i] = color;
FastLED.show();
}
if( _1 ) // if number '1' has pressed
{
break; // exit from outer for loop
}
}
}
else
{
if(enable__) // if true ,then (in case I have changed color) the brightness continues from the last value brightness
{
var = b;
}
else
{
var = 255; // otherwise , brightness starts from the beginning (which is 255, cause we are in 'fade out')
}
for(int k = var; k >= 0; k -= 3)
{
for(int i = 0; i < NUM_OF_LEDS; i++)
{
key = keypad.getKey();
if(key == '1')
{
keep = false; // stop while loop
_1 = true; // number '1' just pressed
break; // exit from 2nd for loop
}
else if(key == '2')
{
if(fade_variable < 16)
{
fade_variable++;
}
else
{
fade_variable = 0;
}
fade_in_fade_out(array_colors[ fade_variable ] , key , k , false , true , true);
}
else // if no key has pressed , continue your job
{
enable__ = false;
UP = true; // This gets false , so that leds fade in
}
FastLED.setBrightness(k);
leds[i] = color;
FastLED.show();
}
if( _1 ) // if number '1' has pressed
{
break; // exit from 1st for loop
}
}
}
}
if( _1 ) // if number '1' has pressed
{
pick_color(array_colors[pick_color_variable - 1] , brightness);
}
}
void control_brightness(char key)
{
//char key = keypad.getKey();
if(key == '*' && brightness > 0)
{
brightness -= 15;
FastLED.setBrightness(brightness);
FastLED.show();
}
if(key == '#' && brightness < 255)
{
brightness += 15;
FastLED.setBrightness(brightness);
FastLED.show();
}
}
void loop()
{
char key = keypad.getKey();
if(key == '1')
{
if(pick_color_variable < 18)
{
pick_color_variable++;
}
else
{
pick_color_variable = 1;
}
pick_color( array_colors[ pick_color_variable - 1 ] , brightness);
}
else if(key == '2')
{
fade_in_fade_out( array_colors[ fade_variable - 1 ] , key , 0 , true , false , true);
}
control_brightness(key);
}
I think it's a problem of breaking 'for loops' or 'while loop'. I 'm testing it for about 3 hours now and I can't figure it out.
Your help is appreciated a lot.
Thank you.