Neopixel is blinking when using toggle for on/off

Hi all.. Im new to arduino and neopixel..
im wanna build multi switch to turn on/off some pixel.
here my code what happen right now is when switch turn on led/pixel chossen to turn on is blinking

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 9
#define PIXEL_COUNT 4

Adafruit_NeoPixel strip = Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

const int LeftTurnInput = 2;
const int RightTurnInput = 5;
const int StandbyInput = 3;
const int BrakeInput = 4;
const int LED = 9;

void setup() 
{
  pinMode(LED, OUTPUT); 
  pinMode(LeftTurnInput, INPUT);  
  pinMode(RightTurnInput, INPUT);
  pinMode(StandbyInput, INPUT);
  pinMode(BrakeInput, INPUT);
  strip.begin();
  strip.show();
  Serial.begin(9600);
}


void loop(){
  
 //Left Turn Signal Switch
  if( (digitalRead(LeftTurnInput) == HIGH) ) {
    strip.setPixelColor (0, 255, 75, 0);
  	strip.show();
  }
  else if( (digitalRead(LeftTurnInput) == LOW) ) {
  	strip.clear();
    strip.show();
  }
 
 //Right Turn Signal Switch
  if( (digitalRead(RightTurnInput) == HIGH) ) {
    strip.setPixelColor (3, 255, 75, 0);
  	strip.show();
  }
  else if( (digitalRead(RightTurnInput) == LOW) ) {
  	strip.clear();
    strip.show();
  }
 
 //Standby Switch
  if( (digitalRead(StandbyInput) == HIGH) ) {
    strip.setPixelColor (0, 255, 0, 0);
    strip.setPixelColor (1, 255, 0, 0);
    strip.setPixelColor (2, 255, 0, 0);
    strip.setPixelColor (3, 255, 0, 0);
    delay(100);
  	strip.show();
  }
  else if( (digitalRead(StandbyInput) == LOW) ) {
  	strip.clear();
    strip.show();
  }
 
 //Brake Switch
  if( (digitalRead(BrakeInput) == HIGH) ) {
	strip.setPixelColor (0, 255, 0, 0);
    strip.setPixelColor (1, 255, 0, 0);
    strip.setPixelColor (2, 255, 0, 0);
    strip.setPixelColor (3, 255, 0, 0);
   	delay(100);
  	strip.show();
    strip.setPixelColor (0, 0, 0, 0);
    strip.setPixelColor (1, 0, 0, 0);
    strip.setPixelColor (2, 0, 0, 0);
    strip.setPixelColor (3, 0, 0, 0);
   	delay(100);
  	strip.show();
  }
  else if( (digitalRead(BrakeInput) == LOW) ) {
  	strip.clear();
    strip.show();
  }
}

and here my circuit

tinkercad

could your buttons be bouncing?

type of button?
this is tinkercad of my circuit
tinkercad

what does it exactly do that you don't want?

BTW - you don't need to test twice the digitalRead(), if it's not HIGH it means it's LOW so

  //Left Turn Signal Switch
  if ( (digitalRead(LeftTurnInput) == HIGH) ) {
    strip.setPixelColor (0, 255, 75, 0);
    strip.show();
  }
  else if ( (digitalRead(LeftTurnInput) == LOW) ) {
    strip.clear();
    strip.show();
  }

should be

  //Left Turn Signal Switch
  if ( (digitalRead(LeftTurnInput) == HIGH) ) {
    strip.setPixelColor (0, 255, 75, 0);
    strip.show();
  }  else {
    strip.clear();
    strip.show();
  }

do you understand that you test all the buttons in a row, so some clear() calls are made

im wanna is simple..
just led is turn on when toggle on and off when toggle off
not blinking when toggle is on

sorry my english is everywhere :sweat_smile:

what you mean is i need to divide all the button?

the code tests LeftTurnInput, if it's LOW it shuts everything off
then the code tests RightTurnInput, if it's HIGH, it lights a specific pattern
but then you keep testing so if StandbyIn is LOW, you turn everything off again

➜ hence the blinking you see

makes sense?

you want to asses the global status of all the buttons and decide what to do only once.

you have 4 switches, so 16 combinations !

1 Like

like this?

if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 255, 75, 0);
  	strip.show();
  }

need to make 15 combination more and no need to use "else" ?

thanks man i got it

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 9
#define PIXEL_COUNT 4

Adafruit_NeoPixel strip = Adafruit_NeoPixel (PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

const int LeftTurnInput = 2;
const int RightTurnInput = 5;
const int StandbyInput = 3;
const int BrakeInput = 4;
const int LED = 9;

void setup() 
{
  pinMode(LED, OUTPUT); 
  pinMode(LeftTurnInput, INPUT);  
  pinMode(RightTurnInput, INPUT);
  pinMode(StandbyInput, INPUT);
  pinMode(BrakeInput, INPUT);
  strip.begin();
  strip.show();
  Serial.begin(9600);
}


void loop(){
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 0, 0);
    strip.setPixelColor (2, 0, 0, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 0, 250, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 0, 250, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 0, 250, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == HIGH)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 250, 0, 0);
    strip.setPixelColor (1, 0, 0, 0);
    strip.setPixelColor (2, 0, 0, 0);
    strip.setPixelColor (3, 0, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 0, 250, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 0, 250, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 0, 250, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == HIGH)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 0, 0, 0);
    strip.setPixelColor (1, 0, 0, 0);
    strip.setPixelColor (2, 0, 0, 0);
    strip.setPixelColor (3, 250, 0, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 0, 250, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 0, 250, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == HIGH)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 0, 250, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 0, 250, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == HIGH) ) {
    strip.setPixelColor (0, 0, 250, 0);
    strip.setPixelColor (1, 0, 250, 0);
    strip.setPixelColor (2, 0, 250, 0);
    strip.setPixelColor (3, 0, 250, 0);
  	strip.show();
  }
  
  if( (digitalRead(LeftTurnInput) == LOW)&&(digitalRead(RightTurnInput) == LOW)&&(digitalRead(StandbyInput) == LOW)&&(digitalRead(BrakeInput) == LOW) ) {
    strip.setPixelColor (0, 0, 0, 0);
    strip.setPixelColor (1, 0, 0, 0);
    strip.setPixelColor (2, 0, 0, 0);
    strip.setPixelColor (3, 0, 0, 0);
  	strip.show();
  }
 
}

i will try make it less longer code

You have 16 possible combinations. As @J-M-L has suggested, fill in the table. Determine how many are unique. Read the 4 inputs, determine a "state", decide what the LED condition should be in each state, then do one strip.show(). Otherwise, your repetitive actions in non-active states are clobbering your previous settings for active states(those else clauses that do the clearing). Here's the order:

//read switches, determine switch state; at a minimum, you'll need five, one for each switch plus a "none"
//switch case(switchstate)  (the following cases are one possibility)
    //left
    //right
    //brake
    //standby
    //none
    //default case (could handle all the 'unusual' states, with two or more inputs asserted)
//strip.show(), done once when all is set up

Remember, it doesn't matter that some of the cases, e.g. Left AND Right, aren't logically possible, you really still should have a default that handles that possibility, because your
switch can be set that way.
C

1 Like

yes i make it in my last post :smile:

No, actually, you used a "waterfall" of if statements. Suggest you explore the switch() statement, it's far more clear what you're doing when you compartmentalize, and it's a step on the way to a finite state machine, which is a very powerful tool for compact and comprehensible code.
But, YMMV.
C

yes

you could do something like this


byte leftTurnState  = (digitalRead(LeftTurnInput) == HIGH)  ? 1 : 0;
byte rightTurnState = (digitalRead(RightTurnInput) == HIGH) ? 2 : 0;
byte standbyState   = (digitalRead(StandbyInput) == HIGH)   ? 4 : 0;
byte brakeState     = (digitalRead(BrakeInput) == HIGH)     ? 8 : 0;

byte globalState = brakeState | standbyState |  rightTurnState | leftTurnState; // 4 bits of a byte

switch (globalState) {
case 0b0000:  // none
  break;

case 0b0001:  // only leftTurnState
  break;

case 0b0010: // only rightTurnState
  break;

case 0b0011: // both leftTurnState and rightTurnState
  break;

  ... // do all the possibilities from 0 to 15

case 0b1111:  // all of them
  break;

}

and if some patterns are the same for different options you can group those cases together

Or, explore the use of an enumeration.

i dunno how implement that in my code
coding is hard :pensive:

i thing im go with my code earlier :pensive:

something like this:

void loop() {

  byte leftTurnState  = (digitalRead(LeftTurnInput) == HIGH)  ? 1 : 0;
  byte rightTurnState = (digitalRead(RightTurnInput) == HIGH) ? 2 : 0;
  byte standbyState   = (digitalRead(StandbyInput) == HIGH)   ? 4 : 0;
  byte brakeState     = (digitalRead(BrakeInput) == HIGH)     ? 8 : 0;

  byte globalState = brakeState | standbyState |  rightTurnState | leftTurnState; // 4 bits of a byte

  switch (globalState) {
    case 0b0000:  // none
      strip.setPixelColor (0, 0, 0, 0);
      strip.setPixelColor (1, 0, 0, 0);
      strip.setPixelColor (2, 0, 0, 0);
      strip.setPixelColor (3, 0, 0, 0);
      strip.show();
      break;

    case 0b0001:  // only leftTurnState
      strip.setPixelColor (0, 0, 250, 0);
      strip.setPixelColor (1, 0, 250, 0);
      strip.setPixelColor (2, 0, 250, 0);
      strip.setPixelColor (3, 0, 250, 0);
      strip.show();
      break;

    case 0b0010: // only rightTurnState
      strip.setPixelColor (0, 0, 250, 0);
      strip.setPixelColor (1, 0, 250, 0);
      strip.setPixelColor (2, 0, 250, 0);
      strip.setPixelColor (3, 0, 250, 0);
      strip.show()
      break;

    case 0b0011: // both leftTurnState and rightTurnState
      strip.setPixelColor (0, 0, 250, 0);
      strip.setPixelColor (1, 0, 250, 0);
      strip.setPixelColor (2, 0, 250, 0);
      strip.setPixelColor (3, 0, 250, 0);
      strip.show();
      break;

    // ......... do all the other possibilities

    case 0b0111:
      strip.setPixelColor (0, 250, 0, 0);
      strip.setPixelColor (1, 0, 250, 0);
      strip.setPixelColor (2, 0, 250, 0);
      strip.setPixelColor (3, 250, 0, 0);
      strip.show();
      break;

    case 0b1111:  // all of them
      strip.setPixelColor (0, 250, 0, 0);
      strip.setPixelColor (1, 0, 250, 0);
      strip.setPixelColor (2, 0, 250, 0);
      strip.setPixelColor (3, 250, 0, 0);
      strip.show();
      break;
  }
}

basically the same code you have in all the tests assigned to the right case

this way you read the status of the pins only once in a loop

And, it's obvious what section of code you have to modify when you want to add something to only one state. No parsing up and down the if-then-else logic chain hoping to find the one place you want to add that one little statement to, for example, turn on a dashboard light for just the Brake state.
C

whoa thx @J-M-L for more detailed explanation with example

@camsysca now i get it how its work thx for more explanation to.. my code is more simple now

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