Hello, I am trying to get the event listener to work. I want to turn each led on strip on and off with each button on a keypad and change the colour with a different button each time. the code compiled but it did not work.
#include <Keypad.h>
#include <FastLED.h>
#define LED_PIN 4
#define RED_LED_PIN 2
#define BLUE_LED_PIN 3
#define GREEN_LED_PIN 12
#define YELLOW_LED_PIN 13
#define NUM_LEDS 15
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
//#define keypad.getState();
int buttonState = 0; // variable for reading the pushbutton status
int pressedState = 0; // variable for reading the pushbutton status
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'0','A','B'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the kpd
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
pinMode(RED_LED_PIN, INPUT);
pinMode(BLUE_LED_PIN, INPUT);
pinMode(GREEN_LED_PIN, INPUT);
pinMode(YELLOW_LED_PIN, INPUT);
customKeypad.addEventListener(keypadEvent);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//read button state:
void keypadEvent(KeypadEvent hexaKeys){
switch (customKeypad.getState()){
buttonState = digitalRead(RED_LED_PIN);
if (buttonState == HIGH) {
pressedState = 1;
}
buttonState = digitalRead(BLUE_LED_PIN);
if (buttonState == HIGH) {
pressedState = 2;
}
buttonState = digitalRead(GREEN_LED_PIN);
if (buttonState == HIGH) {
pressedState = 3;
}
buttonState = digitalRead(YELLOW_LED_PIN);
if (buttonState == HIGH) {
pressedState = 4;
}
customKeypad.getState();
}
case HOLD:
char customKey = customKeypad.getKey();
Serial.println(hexaKeys);
if (customKey=='1'){
// leds[0]= CRGB::Red;
for (int i = 0; i <= 0; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='2'){
//leds[0]= CRGB::Blue;
for (int i = 1; i <= 1; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='3'){
//leds[0]= CRGB::Yellow;
for (int i = 2; i <= 2; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='4'){
//leds[0]= CRGB::Green;
for (int i = 3; i <= 3; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='5'){
//leds[0]= CRGB::Purple;
for (int i = 4; i <= 4; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='6'){
//leds[0]= CRGB::Orange;
for (int i = 5; i <= 5; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='7'){
// leds[0]= CRGB::Pink;
for (int i = 6; i <= 6; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='8'){
// leds[0]= CRGB::Gray;
for (int i = 7; i <= 13; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
FastLED.show();
break;
case RELEASED:
if (hexaKeys) {
for (int i = 0; i <= 20; i++){
leds[i]= CRGB::Black;
}
}
break;
FastLED.show();
}
}
I have made the correction and the code still did not work. Let me explain my goal here.
I want to use the four individual buttons to change the colors of the led strip then the buttons on the keypad will just turn each LED on the strip on when it is pressed and held and off when it is released. Could you tell me what I am doing wrong?
#include <Keypad.h>
#include <FastLED.h>
#define LED_PIN 4
#define RED_LED_PIN 2
#define BLUE_LED_PIN 3
#define GREEN_LED_PIN 12
#define YELLOW_LED_PIN 13
#define NUM_LEDS 15
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
//#define keypad.getState();
int buttonState = 0; // variable for reading the pushbutton status
int pressedState = 0; // variable for reading the pushbutton status
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'0','A','B'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the kpd
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
pinMode(RED_LED_PIN, INPUT);
pinMode(BLUE_LED_PIN, INPUT);
pinMode(GREEN_LED_PIN, INPUT);
pinMode(YELLOW_LED_PIN, INPUT);
customKeypad.addEventListener(keypadEvent);
Serial.begin(9600);
}
void loop() {
customKeypad.getState();
//read button state:
buttonState = digitalRead(RED_LED_PIN);
if (buttonState == HIGH) {
pressedState = 1;
}
buttonState = digitalRead(BLUE_LED_PIN);
if (buttonState == HIGH) {
pressedState = 2;
}
buttonState = digitalRead(GREEN_LED_PIN);
if (buttonState == HIGH) {
pressedState = 3;
}
buttonState = digitalRead(YELLOW_LED_PIN);
if (buttonState == HIGH) {
pressedState = 4;
}
}
void keypadEvent(KeypadEvent hexaKeys){
switch (customKeypad.getState()){
case HOLD:
char customKey = customKeypad.getKey();
Serial.println(hexaKeys);
if (customKey=='1'){
// leds[0]= CRGB::Red;
for (int i = 0; i <= 0; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='2'){
//leds[0]= CRGB::Blue;
for (int i = 1; i <= 1; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='3'){
//leds[0]= CRGB::Yellow;
for (int i = 2; i <= 2; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='4'){
//leds[0]= CRGB::Green;
for (int i = 3; i <= 3; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='5'){
//leds[0]= CRGB::Purple;
for (int i = 4; i <= 4; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='6'){
//leds[0]= CRGB::Orange;
for (int i = 5; i <= 5; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='7'){
// leds[0]= CRGB::Pink;
for (int i = 6; i <= 6; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='8'){
// leds[0]= CRGB::Gray;
for (int i = 7; i <= 13; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
FastLED.show();
break;
case RELEASED:
if (hexaKeys) {
for (int i = 0; i <= 15; i++){
leds[i]= CRGB::Black;
}
}
break;
FastLED.show();
}
}
I don't see anything happening in loop except you get the keypad state and set your pressedState value. You don't call FastLED() in there, or alternatively do a digitalWrite().
Until you do something inside your loop() function, there will be no action taken on the inputs, other than bits in an int changing.
It is working now. I did pass the right value (customkey) to KeyPadEvent(). Another problem is that the input buttons dont seem to change all LED at once when pressed. Is there another way I could write this code to work efficiently?
#include <Keypad.h>
#include <FastLED.h>
#define LED_PIN 4
#define RED_LED_PIN 2
#define BLUE_LED_PIN 3
#define GREEN_LED_PIN 12
#define YELLOW_LED_PIN 13
#define NUM_LEDS 15
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
//#define keypad.getState();
int buttonState = 0; // variable for reading the pushbutton status
int pressedState = 0; // variable for reading the pushbutton status
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'0','A','B'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the kpd
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
pinMode(RED_LED_PIN, INPUT);
pinMode(BLUE_LED_PIN, INPUT);
pinMode(GREEN_LED_PIN, INPUT);
pinMode(YELLOW_LED_PIN, INPUT);
customKeypad.addEventListener(keypadEvent);
Serial.begin(9600);
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey) {
Serial.println(customKey);
}
// if (customKey=='1'){
// // leds[0]= CRGB::Red;
// for (int i = 0; i <= 0; i++) {
// switch (pressedState) {
// case 1: // your hand is on the sensor
// leds[i]= CRGB::Red;
// break;
// case 2: // your hand is close to the sensor
// leds[i]= CRGB::Blue;
// break;
// case 3: // your hand is a few inches from the sensor
// leds[i]= CRGB::Green;
// break;
// case 4: // your hand is nowhere near the sensor
// leds[i]= CRGB::Yellow;
// break;
// }
// }
// }
//read button state:
buttonState = digitalRead(RED_LED_PIN);
if (buttonState == HIGH) {
pressedState = 1;
}
buttonState = digitalRead(BLUE_LED_PIN);
if (buttonState == HIGH) {
pressedState = 2;
}
buttonState = digitalRead(GREEN_LED_PIN);
if (buttonState == HIGH) {
pressedState = 3;
}
buttonState = digitalRead(YELLOW_LED_PIN);
if (buttonState == HIGH) {
pressedState = 4;
}
}
void keypadEvent(KeypadEvent customKey){
switch (customKeypad.getState()){
case PRESSED:
if (customKey=='0'){
for (int i = 9; i <= 9; i++) {
leds[i]= CRGB::Blue;
}
}
break;
case HOLD:
// char customKey = customKeypad.getKey();
// Serial.println(customKey);
if (customKey=='1'){
// leds[0]= CRGB::Red;
for (int i = 3; i <= 3; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='2'){
//leds[0]= CRGB::Blue;
for (int i = 1; i <= 1; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='3'){
//leds[0]= CRGB::Yellow;
for (int i = 2; i <= 2; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='4'){
//leds[0]= CRGB::Green;
for (int i = 0; i <= 0; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='5'){
//leds[0]= CRGB::Purple;
for (int i = 4; i <= 4; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='6'){
//leds[0]= CRGB::Orange;
for (int i = 5; i <= 5; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='7'){
// leds[0]= CRGB::Pink;
for (int i = 6; i <= 6; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='8'){
// leds[0]= CRGB::Gray;
for (int i = 7; i <= 7; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='9'){
//leds[0]= CRGB::BlueViolet;
for (int i = 8; i <= 8; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
// else if (customKey=='0'){
// // leds[0]= CRGB::LightSteelBlue;
//
// for (int i = 9; i <= 9; i++) {
//
// switch (pressedState) {
// case 1: // your hand is on the sensor
// leds[i]= CRGB::Red;
// break;
// case 2: // your hand is close to the sensor
// leds[i]= CRGB::Blue;
// break;
// case 3: // your hand is a few inches from the sensor
// leds[i]= CRGB::Green;
// break;
// case 4: // your hand is nowhere near the sensor
// leds[i]= CRGB::Yellow;
// break;
// }
//
// }
// }
else if (customKey=='A'){
//leds[0]= CRGB::Magenta;
for (int i = 10; i <= 10; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='B'){
// leds[0]= CRGB::SaddleBrown;
for (int i = 11; i <= 11; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
FastLED.show();
break;
case RELEASED:
if (customKey != NO_KEY) {
for (int i = 0; i <= 15; i++){
leds[i]= CRGB::Black;
FastLED.show();
}
}
break;
}
}
the values of i are 1-15 except for the PRESSED case. My current concern is the button not changing the LED colors like i want it to.
I have not tried printing.
I have clean the code alittle..
#include <Keypad.h>
#include <FastLED.h>
#define LED_PIN 4
#define RED_LED_PIN 2
#define BLUE_LED_PIN 3
#define GREEN_LED_PIN 12
#define YELLOW_LED_PIN 13
#define NUM_LEDS 15
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
//#define keypad.getState();
int buttonState = 0; // variable for reading the pushbutton status
int pressedState = 0; // variable for reading the pushbutton status
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'0','A','B'}
};
byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {7, 6, 5}; //connect to the column pinouts of the kpd
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );
void setup() {
// put your setup code here, to run once:
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
pinMode(RED_LED_PIN, INPUT);
pinMode(BLUE_LED_PIN, INPUT);
pinMode(GREEN_LED_PIN, INPUT);
pinMode(YELLOW_LED_PIN, INPUT);
customKeypad.addEventListener(keypadEvent);
Serial.begin(9600);
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey) {
Serial.println(customKey);
}
//read button state:
buttonState = digitalRead(RED_LED_PIN);
if (buttonState == HIGH) {
pressedState = 1;
}
buttonState = digitalRead(BLUE_LED_PIN);
if (buttonState == HIGH) {
pressedState = 2;
}
buttonState = digitalRead(GREEN_LED_PIN);
if (buttonState == HIGH) {
pressedState = 3;
}
buttonState = digitalRead(YELLOW_LED_PIN);
if (buttonState == HIGH) {
pressedState = 4;
}
}
void keypadEvent(KeypadEvent customKey){
switch (customKeypad.getState()){
case HOLD:
if (customKey=='1'){
for (int i = 3; i <= 3; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='2'){
for (int i = 1; i <= 1; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='3'){
for (int i = 2; i <= 2; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='4'){
for (int i = 0; i <= 0; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='5'){
for (int i = 4; i <= 4; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='6'){
for (int i = 5; i <= 5; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='7'){
for (int i = 6; i <= 6; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='8'){
for (int i = 7; i <= 7; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='9'){
for (int i = 8; i <= 8; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='0'){
// leds[0]= CRGB::LightSteelBlue;
for (int i = 9; i <= 9; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='A'){
for (int i = 10; i <= 10; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
else if (customKey=='B'){
for (int i = 11; i <= 11; i++) {
switch (pressedState) {
case 1: // your hand is on the sensor
leds[i]= CRGB::Red;
break;
case 2: // your hand is close to the sensor
leds[i]= CRGB::Blue;
break;
case 3: // your hand is a few inches from the sensor
leds[i]= CRGB::Green;
break;
case 4: // your hand is nowhere near the sensor
leds[i]= CRGB::Yellow;
break;
}
}
}
FastLED.show();
break;
case RELEASED:
if (customKey != NO_KEY) {
for (int i = 0; i <= 15; i++){
leds[i]= CRGB::Black;
FastLED.show();
}
}
break;
}
}