Good day,
I have issue with my sketch, the sketch is for a radio controlled robot which I wish to had addressable individual LED strip.
The remote control has two thumb joystick, joystick 1 is to turn on/off lights when pressed, joystick 2 is to cycle through LED patterns when pressed.
Problem : ELse if doesn't seem to work, once it goes through the first else if the serial monitor display 255. I treid removing 2 Else IF and it behaved differently.
-
How to fix else if?
-
I have tried to see how to clear led so next mode can have correct LED lit but I am unable to figure this as well, I have tried FastLED.clear(); but seem to not do anything.
thank you in advance.
this is a sketch I am testing with to figure out things.
#include <FastLED.h>
#define LED_PIN 3
#define NUM_LEDS 30
//Predefined group of LEDS
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};
//predefined Joysticks input
const int JoystickPushButton = A5; // the pin that the pushbutton is attached to
const int Joystick1PushButton = A4;
//array of lEDS
CRGB leds[NUM_LEDS];
//predefined state for push joystick 1
int JoystickbuttonState = 0; // current state of the button
int JoysticklastButtonState = 0; // previous state of the button
int JoystickmodeState = 0; // remember current led state
int JoystickstateNum;
//predefined state for push joystick 2
int Joystick1buttonState = 0; // current state of the button
int Joystick1lastButtonState = 0; // previous state of the button
int Joystick1modeState = 0; // remember current led state
int Joystick1stateNum;
//function to lit all lights
void lightsOnAll() {
for (int i = 0; i < 30; i++)
{
leds[allLEDS[i] ] = CRGB::White;
}
FastLED.show();
}
//function to turn all lights off
void lightsOffAll() {
for (int i = 0; i < 30; i++)
{
leds[allLEDS[i] ] = CRGB::Black;
}
FastLED.show();
}
//function to lit front lights
void lightsforwardOnly() {
for (int i = 0; i < 30; i++)
{
leds[frontLEDS[i] ] = CRGB::White;
}
FastLED.show();
}
//function to lit back lights
void lightsbackOnly() {
for (int i = 0; i < 30; i++)
{
leds[backLEDS[i] ] = CRGB::White;
}
FastLED.show();
}
//function to lit right lights
void lightsrightOnly() {
for (int i = 0; i < 30; i++)
{
leds[rightLEDS[i] ] = CRGB::White;
}
FastLED.show();
}
//function to lit left lights
void lightsleftOnly() {
for (int i = 0; i < 30; i++)
{
leds[leftLEDS[i] ] = CRGB::White;
}
FastLED.show();
}
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.setBrightness(255);
pinMode(JoystickPushButton, INPUT_PULLUP); // initialize the button pin as a input
pinMode(Joystick1PushButton, INPUT_PULLUP); // initialize the button pin as a input
}
void loop() {
JoystickbuttonState = digitalRead(JoystickPushButton);
Joystick1buttonState = digitalRead(Joystick1PushButton);
// check if the button is pressed or released
// by comparing the buttonState to its previous state
if (JoystickbuttonState != JoysticklastButtonState) {
// change the state of the led when someone pressed the button
if (JoystickbuttonState == 1) {
JoystickstateNum++;
if (JoystickstateNum > 2) JoystickstateNum = 1;
}
// remember the current state of the button
// JoysticklastButtonState = JoystickbuttonState;
}
// Serial.println(JoystickstateNum);
Serial.println(Joystick1stateNum);
// adding a small delay prevents reading the buttonState to fast
// ( debouncing )
delay(20);
// check if the button is pressed or released
// by comparing the buttonState to its previous state
if (Joystick1buttonState != Joystick1lastButtonState) {
// change the state of the led when someone pressed the button
if (Joystick1buttonState == 1) {
Joystick1stateNum++;
if (Joystick1stateNum > 5) Joystick1stateNum = 1;
}
// remember the current state of the button
//Joystick1lastButtonState = Joystick1buttonState;
}
// adding a small delay prevents reading the buttonState to fast
// ( debouncing )
delay(20);
if (JoystickstateNum == 1) {
lightsOffAll();
Serial.println("OFF");
}
else if (JoystickstateNum == 2 && Joystick1stateNum == 1) {
lightsOnAll();
Serial.println("Mode ON");
}
else if (JoystickstateNum == 2 && Joystick1stateNum == 2) {
Serial.println("Mode 1A set front Lights On only");
lightsforwardOnly();
}
else if (JoystickstateNum == 2 && Joystick1stateNum == 3) {
Serial.println("Mode 1A set left Lights On only");
lightsleftOnly();
}
else if (JoystickstateNum == 2 && Joystick1stateNum == 4) {
Serial.println("Mode 1A set right Lights On only");
lightsrightOnly();
}
else if (JoystickstateNum == 2 && Joystick1stateNum == 5) {
Serial.println("Mode 1A set back Lights On only");
lightsbackOnly();
}
// remember the current state of the button
JoysticklastButtonState = JoystickbuttonState;
Joystick1lastButtonState = Joystick1buttonState;
}