Hello all,
I'm trying to use this library to control a small LED strip without using delay() because I'm doing other stuff at the same time.
So, when I looked up the topic of multi tasking, I came to the understanding that I need to utilize Object Oriented Programming methods to achieve multi tasking.
There are 2 buttons:
btnOrange when pressed will light the strip Orange alternating the LEDs every 50ms and will activate a motor.
btnGreen when pressed will light the strip Green alternating the LEDs every 50ms and will activate a different motor.
My problem is when I press btnGreen, only the first LED will turn green and will flicker as long as I'm pressing. This is not the case with btnOrange because I'm using a for loop and the beautiful delay().
Here's the code in the main sketch:
#include "ledControl.h"
int btnOrange = 2;
int btnGreen = 3;
void setup()
{
pinMode (btnOrange,INPUT);
pinMode (btnGreen,INPUT);
}
void loop()
{
ledControl ledObject;
if (digitalRead(btnOrange) == HIGH)
{
ledObject.orangeAlternate();
theOtherFunction1();
}
if (digitalRead(btnGreen) == HIGH)
{
ledObject.greenAlternate();
theOtherFunction2();
}
ledObject.switchOffStrip();
}
void theOtherFunction1()
{
//stuff goes here
}
void theOtherFunction2()
{
//stuff goes here
}
And here's the rest of the code inside the "ledControl.h" tab:
//FastLED definitions
#include <FastLED.h>
#define NUM_LEDS 6
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
class ledControl
{
public:
//class constructor
ledControl()
{
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}
// class public variables
int y = 0;
unsigned long now;
unsigned long elapsed;
unsigned long period = 50;
// class functions
void orangeAlternate()
{
for (int x=0; x < NUM_LEDS; x++)
{
leds[x].setRGB(255,69,0);
FastLED.show();
delay(50); //used here for demonstration, I'm trying to substitue it with millis() similar to the greenAlternate function.
leds[x] = CRGB::Black;
}
}
void greenAlternate()
{
now = millis();
if (now - elapsed >= period)
{
elapsed = now;
leds[y].setRGB(0,255,0);
FastLED.show();
leds[y] = CRGB::Black;
y++;
if (y >= NUM_LEDS) y = 0;
}
}
void switchOffStrip()
{
for (int x = 0; x < NUM_LEDS; x ++)
{
leds[x].setRGB(0,0,0);
FastLED.show();
}
}
};
BTW, If moved the code for greenAlternate to the main sketch and outside the class it works fine -providing that I include FastLED stuff in -.
I've spent the last 5 days learning about millis() and classes and I can't figure what am I doing wrong.
Thoughts please ... Thanks a lot,