Keypad + WS2812 - Looping problem + Blynk

Hello,

I have one quite annoying problem which I am trying to fix last couple days.

I would like to have a system which is indicating the status of the machine by colour.
For example machine is running - green light.
Machine stop - red light, etc.

Bacause regular lights are boring I am trying to add some effects by FastLED & WS2812 led strip, but seems that I am getting some loop problem.

The idea is to click '1' on the keypad and then the green light with glowing effects done by FastLED would come up.

Then when '2' would be pressed the red light (also with some effects).

Now the keypad is working, because when '1' would be pressed the green light is coming, then when '2' is pressed the red light is coming - but the main problem is that the effects are not there.

Below it the code which I am using right now.

Keypad_and_WS2812.ino (2.1 KB)

#include <Keypad.h>
#include <FastLED.h>

byte myLEDis = 255;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad
boolean keyStateChanged();

Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define NUM_LEDS 10
CRGB leds[NUM_LEDS];
#define PIN 9

uint8_t colorIndex[NUM_LEDS];

DEFINE_GRADIENT_PALETTE( greenblue_gp ) {
  0, 5, 130, 5,
  125, 0, 200, 10,
  255, 5, 130, 5,
};
CRGBPalette16 greenblue = greenblue_gp;

DEFINE_GRADIENT_PALETTE( red_gp ) {
  0, 155, 0, 5,
  125, 200, 200, 10,
  255, 155, 0, 5,
};
CRGBPalette16 red = red_gp;

//******************************************************************************
void setup()
{
  Serial.begin(9600);
  pinMode(PIN, OUTPUT);
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(50);
  
  for (int i = 0; i < NUM_LEDS; i++) {
    colorIndex[i] = random8();
  }
} //END of setup()

//******************************************************************************
void loop()
{
    char key = myKeypad.getKey();
  FastLED.clear();

        if (key == '1') {
          for (int i = 0; i < NUM_LEDS; i++) {
            leds[i] = ColorFromPalette(greenblue, colorIndex[i]);
          }
          EVERY_N_MILLISECONDS(5) {
            for (int i = 0; i < NUM_LEDS; i++) {
              colorIndex[i]++;
            }
          }
          FastLED.show();
}
              
        else if (key  == '2') {
          for (int i = 0; i < NUM_LEDS; i++) {
            leds[i] = ColorFromPalette(red, colorIndex[i]);
          }
          EVERY_N_MILLISECONDS(5) {
            for (int i = 0; i < NUM_LEDS; i++) {
              colorIndex[i]++;
            }
          }
          FastLED.show();
        }
    }

EVERY_N_MILLISECONDS is useful clever, but it can't work miracles, which is what you are asking of it here.

You will have to come to a better understanding of how it can be used, or better, come to grips with the ideas it is using, and hiding, and use similar techniques yourself.

google/read

 arduino Blink without delay 

and

Arduino two things at once

Or was it

Arduino several things at the same time

You get the idea, I hope.

Blink without delay is in the IDE I think.

You didn't say, I assume you are using a Nano or UNO or similar board.

There are ways to put the work on someone else, but the principles involved in getting this going with plain code are well worth learning.

a7

Hi, try this code:

#include <Keypad.h>
#include <FastLED.h>

byte myLEDis = 255;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad
boolean keyStateChanged();

Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define NUM_LEDS 10
CRGB leds[NUM_LEDS];
#define PIN 9

uint8_t colorIndex[NUM_LEDS];

DEFINE_GRADIENT_PALETTE( greenblue_gp ) {
  0, 5, 130, 5,
  125, 0, 200, 10,
  255, 5, 130, 5,
};
CRGBPalette16 greenblue = greenblue_gp;

DEFINE_GRADIENT_PALETTE( red_gp ) {
  0, 155, 0, 5,
  125, 200, 200, 10,
  255, 155, 0, 5,
};
CRGBPalette16 red = red_gp;
char key;
unsigned myTime = 0;
//-----------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  pinMode(PIN, OUTPUT);
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(50);

  for (int i = 0; i < NUM_LEDS; i++) {
    colorIndex[i] = random8();
  }
  myTime = millis();
} //END of setup()
//-----------------------------------------------------------
void loop()
{
  // Any char different from 1 or 2 turns off the led strip.
  char myKBkey = myKeypad.getKey();
  if (myKBkey)
  {
    key = myKBkey;
  }
  FastLED.clear();
  if (key == '1')
  {
    for (int i = 0; i < NUM_LEDS; i++)
    {
      leds[i] = ColorFromPalette(greenblue, colorIndex[i]);
    }
  }
  else if (key  == '2')
  {
    for (int i = 0; i < NUM_LEDS; i++)
    {
      leds[i] = ColorFromPalette(red, colorIndex[i]);
    }
  }

  if (millis() - myTime > 5)
  {
    myTime = millis();
    lightShine();
  }
  FastLED.show();
}
//-----------------------------------------------------------
void lightShine() {
  for (int i = 0; i < NUM_LEDS; i++) {
    colorIndex[i] += 20;
  }
}

Hello,
You are awesome!
Thank you for your reply, it helped me a lot!
Your code works fine at the beginning but after a while LEDs starts to blink very fast till the reset.

Now I modified a bit the code and additionally I would like to add Blynk functionalities.

I would like to add virtual leds in the Blynk. 1st led would be green, 2nd would be yellow and 3rd would be red.
Once the 1st button on the keypad would be pushed, the green sequence of WS2812 would be started and at the same time the Virtual Led in Blynk app would turn on and so forth.

Below is my code but I get stucked and the Blynk Virtual Leds are working but then the WS2812 are not working.

#include <FastLED.h>
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";

WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
WidgetLED led4(V4);

#include <Keypad.h>
char key;
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] = {16, 5, 4, 0};
byte colPins [COLS] = {2, 14, 12};

Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);


#define NUM_LEDS 10
CRGB leds[NUM_LEDS];
#define PIN 13


BlynkTimer timer;

uint8_t colorIndex[NUM_LEDS];

DEFINE_GRADIENT_PALETTE (GreenLightPalette) {
  0, 120, 0, 5,
  125, 200, 200, 25,
  255, 155, 0, 5,
};
CRGBPalette16 GreenLight = GreenLightPalette;

BLYNK_CONNECTED () {
  Blynk.syncVirtual(V1, V2, V3, V4);
}

//**********************************************

void TurnOnWS2812() {
  if (key == '1')
  {
    FastLED.setBrightness(120);
    for (int i = 0; i < NUM_LEDS; i++)
    {
      leds[i] = ColorFromPalette(GreenLight, colorIndex[i]);
    }
  }

  EVERY_N_MILLISECONDS(5) {
    for (int i = 0; i < NUM_LEDS; i++) {
      colorIndex[i]++;
    }
    FastLED.show();
  }

  else if (key  == '2')
  {

    FastLED.setBrightness(200);
    uint16_t sinBeat = beatsin8(50, 0, NUM_LEDS - 1, 0, 0);
    uint16_t sinBeat2 = beatsin8(50, 0, NUM_LEDS - 1, 0, 30);

    leds[sinBeat] = CRGB::Yellow;
    leds[sinBeat2] = CRGB::Orange;

    fadeToBlackBy(leds, NUM_LEDS, 20);

    EVERY_N_MILLISECONDS(10) {
      Serial.println(sinBeat);
      Serial.print(",");
      Serial.println(sinBeat2);
      Serial.print(",");
    }
    FastLED.show();
  }

  else if (key  == '3')
  {
    FastLED.setBrightness(200);
    uint16_t sinBeat = beatsin8(100, 0, NUM_LEDS - 1, 0, 0);
    uint16_t sinBeat2 = beatsin8(100, 0, NUM_LEDS - 1, 0, 30);

    leds[sinBeat] = CRGB::Red;
    leds[sinBeat2] = CRGB::Red;

    fadeToBlackBy(leds, NUM_LEDS, 15);

    EVERY_N_MILLISECONDS(10) {
      Serial.println(sinBeat);
      Serial.print(",");
      Serial.println(sinBeat2);
      Serial.print(",");
    }
    FastLED.show();
  }
}

void TurnOnVirtualLEDs() {
  if (key == '1') {
    led1.on();
    led2.off();
    led3.off();
    led4.off();
  }
  else if (key == '2') {
    led2.on();
    led1.off();
    led3.off();
    led4.off();
  }
  else if (key == '3') {
    led3.on();
    led1.off();
    led2.off();
    led4.off();
  }
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  FastLED.addLeds <WS2812, PIN, GRB> (leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  FastLED.setBrightness(100);
  FastLED.show();
  timer.setInterval(500L, TurnOnWS2812);
  timer.setInterval(1000L, TurnOnVirtualLEDs);
}

void loop() {
  Blynk.run();
  timer.run();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.