indicator aplication

hi all

so I have been busy making a program that does flowing indicators via ws2812b leds.
im very new to this so please bare with me :slight_smile:

using these I have it so the same leds do a red rear light, sweeping left sweeping right and brake

ive used a optocoupler to pick up the 12v signal to then switch the digital. all is fine and working

my actual problem is, when the signal for the indicator drops out, as it should during low points in it flashing, my program thinks its time to revert back to normal back light. im trying to have the leds off other than the amber ones during the sweeping function. as it is at the moment the red lights keep coming back in

I essentially need to ignore a change of state for a set time to allow for the indicators flashing

many thanks for any help
andy

Post the code you have so far.

Optiocoupler for 12v interface. Okay, bit of overkill. A resistor and a zener diode would have done you.

Simple answer, don’t turn the red LEDs on unless you want them on??? Really that’s all there is to it.

If you don’t understand WHY they are coming on, sorry, but you’ll need to post some code so we can look at it.

andycrosse:
my actual problem is, when the signal for the indicator drops out, as it should during low points in it flashing, my program thinks its time to revert back to normal back light. im trying to have the leds off other than the amber

i think you need wait long enough to recognize that the signal is done alternating before you change modes. restart the timer (capture timestamp) whenever the signal becomes active

Optiocoupler for 12v interface. Okay, bit of overkill. A resistor and a zener diode would have done you.

i think a resistor and npn transistor (2n3904) with the digital input configured as INPUT_PULLUP would be more conventional.

#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif
#define PIN 6
#define NUMPIXELS 40
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const int buttonPinL = 44; // turn left
const int buttonPinR = 48; // turn right
const int buttonPinB = 52; // brake lights

int buttonStateL = 0;
int buttonStateR = 0;
int buttonStateB = 0;
int stateLT = 0;
int stateRT = 0;
int stateB = 0;

//delays
int delayval = 50; // delay for indicators
int delayvalb = 75; // delay for start up sequence

//indicator colour and brightness
int a = 150;
int b = 20;
int c = 0;
int g =0;

//rear light
int d = 50;
int e = 0;
int f = 0;

// brake light
int m = 255;
int n = 0;
int o = 0;

//number plate light
int s=250;
int t=250;
int u=250;

//start up colour
int x = 255;
int y = 0;
int z = 0;

void setup() {
pinMode(buttonPinL, INPUT);
pinMode(buttonPinR, INPUT);
pinMode(buttonPinB, INPUT);

pixels.begin(); // This initializes the NeoPixel library.

//START UP DISPLAY

pixels.setPixelColor(6, pixels.Color(x,y,z));
pixels.setPixelColor(7, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(5, pixels.Color(x,y,z));
pixels.setPixelColor(8, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(4, pixels.Color(x,y,z));
pixels.setPixelColor(9, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(3, pixels.Color(x,y,z));
pixels.setPixelColor(10, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(2, pixels.Color(x,y,z));
pixels.setPixelColor(11, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(1, pixels.Color(x,y,z));
pixels.setPixelColor(12, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(0, pixels.Color(x,y,z));
pixels.setPixelColor(13, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(28, pixels.Color(x,y,z));
pixels.setPixelColor(14, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(27, pixels.Color(x,y,z));
pixels.setPixelColor(15, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(26, pixels.Color(x,y,z));
pixels.setPixelColor(16, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(25, pixels.Color(x,y,z));
pixels.setPixelColor(17, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(24, pixels.Color(x,y,z));
pixels.setPixelColor(18, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(23, pixels.Color(x,y,z));
pixels.setPixelColor(19, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(22, pixels.Color(x,y,z));
pixels.setPixelColor(20, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(21, pixels.Color(x,y,z));
pixels.setPixelColor(21, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

pixels.setPixelColor(1, pixels.Color(x,y,z));
pixels.setPixelColor(1, pixels.Color(x,y,z));
pixels.show();
delay(delayvalb);

delay(delayvalb);
}

void loop() {
pixels.setPixelColor(29, pixels.Color(s,t,u));
pixels.setPixelColor(30, pixels.Color(s,t,u));
pixels.setPixelColor(31, pixels.Color(s,t,u));

buttonStateL = digitalRead(buttonPinL);
buttonStateR = digitalRead(buttonPinR);
buttonStateB = digitalRead(buttonPinB);

if(buttonStateL == HIGH){

Only half your code posted.
Please read the sticky on posting code.
In particular please use code tags.

Yep, transistor would work also. Optocoupler only really needed if isolation or level shifting required.

pcbbc:
Yep, transistor would work also. Optocoupler only really needed if isolation or level shifting required.

this is a case of level shifting. open-collector drivers support interfaces at different voltages

sorry not allowed to post twice in 5 mins as a newbie, yes i only want the red on when they are required...thats all the time the indicators are off, however during each flash they are off, and to make them stand out the reds need to be off thanks andy

// LEFT INDICATOR

pixels.setPixelColor(6, pixels.Color(a,b,0));
pixels.setPixelColor(21, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(5, pixels.Color(a,b,0));
pixels.setPixelColor(22, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(4, pixels.Color(a,b,0));
pixels.setPixelColor(23, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(3, pixels.Color(a,b,0));
pixels.setPixelColor(24, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(2, pixels.Color(a,b,0));
pixels.setPixelColor(25, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(1, pixels.Color(a,b,0));
pixels.setPixelColor(26, pixels.Color(a,b,0));
pixels.show();
delay(delayval);

pixels.setPixelColor(0, pixels.Color(a,b,0));
pixels.setPixelColor(27, pixels.Color(a,b,0));
pixels.show();

pixels.clear();

delay(delayvalb);

}

if(buttonStateR == HIGH){

// RIGHT INDICATOR
pixels.setPixelColor(30, pixels.Color(s,t,u));
pixels.setPixelColor(31, pixels.Color(s,t,u));
pixels.setPixelColor(29, pixels.Color(s,t,u));
pixels.show();

pixels.setPixelColor(8, pixels.Color(a,b,0));
pixels.setPixelColor(20, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(9, pixels.Color(a,b,0));
pixels.setPixelColor(19, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(10, pixels.Color(a,b,0));
pixels.setPixelColor(18, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(11, pixels.Color(a,b,0));
pixels.setPixelColor(17, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(12, pixels.Color(a,b,0));
pixels.setPixelColor(16, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(13, pixels.Color(a,b,0));
pixels.setPixelColor(15, pixels.Color(a,b,0));
pixels.show();
delay(delayval);
pixels.setPixelColor(14, pixels.Color(a,b,0));
pixels.show();
delay(delayval);

}

buttonStateB = digitalRead(buttonPinB);
if(buttonStateB == HIGH){
//BRAKE LIGHT

pixels.setPixelColor(30, pixels.Color(s,t,u));
pixels.setPixelColor(31, pixels.Color(s,t,u));
pixels.setPixelColor(29, pixels.Color(s,t,u));
pixels.show();

pixels.setPixelColor(0, pixels.Color(m,n,o));
pixels.setPixelColor(1, pixels.Color(m,n,o));
pixels.setPixelColor(2, pixels.Color(m,n,o));
pixels.setPixelColor(3, pixels.Color(m,n,o));
pixels.setPixelColor(4, pixels.Color(m,n,o));
pixels.setPixelColor(5, pixels.Color(m,n,o));
pixels.setPixelColor(6, pixels.Color(m,n,o));
pixels.setPixelColor(7, pixels.Color(m,n,o));
pixels.setPixelColor(8, pixels.Color(m,n,o));
pixels.setPixelColor(9, pixels.Color(m,n,o));
pixels.setPixelColor(10, pixels.Color(m,n,o));
pixels.setPixelColor(11, pixels.Color(m,n,o));
pixels.setPixelColor(12, pixels.Color(m,n,o));
pixels.setPixelColor(13, pixels.Color(m,n,o));
pixels.setPixelColor(14, pixels.Color(m,n,o));
pixels.setPixelColor(15, pixels.Color(m,n,o));
pixels.setPixelColor(16, pixels.Color(m,n,o));
pixels.setPixelColor(17, pixels.Color(m,n,o));
pixels.setPixelColor(18, pixels.Color(m,n,o));
pixels.setPixelColor(19, pixels.Color(m,n,o));
pixels.setPixelColor(20, pixels.Color(m,n,o));
pixels.setPixelColor(21, pixels.Color(m,n,o));
pixels.setPixelColor(22, pixels.Color(m,n,o));
pixels.setPixelColor(23, pixels.Color(m,n,o));
pixels.setPixelColor(24, pixels.Color(m,n,o));
pixels.setPixelColor(25, pixels.Color(m,n,o));
pixels.setPixelColor(26, pixels.Color(m,n,o));
pixels.setPixelColor(27, pixels.Color(m,n,o));
pixels.setPixelColor(28, pixels.Color(m,n,o));

pixels.show();

}

if(buttonStateB == LOW && buttonStateL == LOW ){
//DEFAULT REAR LIGHT LEFT SIDE
pixels.setPixelColor(30, pixels.Color(s,t,u));
pixels.setPixelColor(31, pixels.Color(s,t,u));
pixels.setPixelColor(29, pixels.Color(s,t,u));
pixels.show();

pixels.setPixelColor(0, pixels.Color(d,e,f));
pixels.setPixelColor(1, pixels.Color(d,e,f));
pixels.setPixelColor(2, pixels.Color(d,e,f));
pixels.setPixelColor(3, pixels.Color(d,e,f));
pixels.setPixelColor(4, pixels.Color(d,e,f));
pixels.setPixelColor(5, pixels.Color(d,e,f));
pixels.setPixelColor(6, pixels.Color(d,e,f));

pixels.setPixelColor(22, pixels.Color(d,e,f));
pixels.setPixelColor(23, pixels.Color(d,e,f));
pixels.setPixelColor(24, pixels.Color(d,e,f));
pixels.setPixelColor(25, pixels.Color(d,e,f));
pixels.setPixelColor(26, pixels.Color(d,e,f));
pixels.setPixelColor(27, pixels.Color(d,e,f));
pixels.setPixelColor(28, pixels.Color(d,e,f));
pixels.show();
}

if(buttonStateB == LOW && buttonStateR == LOW){
//DEFAULT REAR LIGHT RIGHT SIDE
pixels.setPixelColor(30, pixels.Color(s,t,u));
pixels.show();
pixels.setPixelColor(31, pixels.Color(s,t,u));
pixels.show();
pixels.setPixelColor(29, pixels.Color(s,t,u));
pixels.show();

pixels.setPixelColor(8, pixels.Color(d,e,f));
pixels.setPixelColor(9, pixels.Color(d,e,f));
pixels.setPixelColor(10, pixels.Color(d,e,f));
pixels.setPixelColor(11, pixels.Color(d,e,f));
pixels.setPixelColor(12, pixels.Color(d,e,f));
pixels.setPixelColor(13, pixels.Color(d,e,f));
pixels.setPixelColor(14, pixels.Color(d,e,f));
pixels.setPixelColor(15, pixels.Color(d,e,f));
pixels.setPixelColor(16, pixels.Color(d,e,f));
pixels.setPixelColor(17, pixels.Color(d,e,f));
pixels.setPixelColor(18, pixels.Color(d,e,f));
pixels.setPixelColor(19, pixels.Color(d,e,f));
pixels.setPixelColor(20, pixels.Color(d,e,f));
pixels.setPixelColor(21, pixels.Color(d,e,f));
pixels.show();

}

}

leds 29 30 31 are white above the number plate

gcjr:
this is a case of level shifting. open-collector drivers support interfaces at different voltages

Yes indeed. I meant level shifting without a common voltage rail.

The OP needs to learn to use code tags AND a loop...

for (int i = 8; i <= 21; i++)
{
   pixels.setPixelColor(i, pixels.Color(d,e,f));
}

At the moment just way too much code.

thanks will tidy up, like I said new to this, and I can follow it simply as it is :slight_smile:

  1. Remove this from the middle of your logic buttonStateB = digitalRead(buttonPinB);
    You’ve already read the state at the start of the loop, you don’t need to read it again.

  2. In your code for LEFT INDICATOR and RIGHT INDICATOR start by turning off all the LEDs

  3. You probably then want to make the BRAKE LIGHT section into two heft and right) and then only illuminate if the indicators on. I’m assuming you are in the US here, and don’t have to display brakes simultaneously with flashers (although the fact you call them indicators and not flashers has me wondering).

  4. Really this needs the delays removing and changing to using millis() so you are not blocking doing the indicator flash pattern. Otherwise there will be a delay displaying the brake lights if in the middle of an indicator delay sequence.

And please recode using for loops, or at the very least functions. All those lengthy setPixelColor sequences make your code very hard to read.

hi
in the uk
yes just seen the repeat of the buttonStateB = digitalRead(buttonPinB);

and have also split the brake feature in left and right
left side on, if brake high and left indicator low

hadn't thought about the delay of the brake light mid sequence

still have original problem though

You need to record the time (using millis()) when the indicators where on.
Then only respond to the brake lights if a certain amount of time has elapsed (probably around 0.5 seconds) after they went off again.
If in the UK not showing brake lights won’t be road legal.

Also take a look at the Tutorials forum sticky. In particular...
How to do several things at a time
How to do multiple things at once
State machines

gcjr:
i think you need wait long enough to recognize that the signal is done alternating before you change modes. restart the timer (capture timestamp) whenever the signal becomes active

thanks will do

its not the brake lights that are the issue, the lights revert to a low level red as a rear light.
this is coming on whilst the indicators are running, so it goes across with amber but red back ground. I only want amber and no other colour.

I think I need

if indicator off
wait 1 second
if indicator still off then bring back ground red on.

I still have the other half of the light working while in indicator state, so if right is active then the left brake is working

sorry missed that reply will research that thanks

but red back ground. I only want amber and no other colour.

In the code where you start the indicator sequence (both left and right sides), begin by turning all the other LEDs for that side (the ones that form the brake light pattern) OFF.
It won’t be perfect (brake lights will come ON during indicator OFF periods - although maybe that’s what you want), but it will be a start.

After that you really do need to read and understand the simple examples in the tutorials first. Then get away from thinking linearly about your programming...
Do this, wait, do something else, wait.... Etc

And start thinking about event driven programming (aka state machine logic). That is just a series of statements of the form...
When in this state, and this event happens, do this and move to new state

Then you program shouldn’t have any delay() calls and will just be a continuous loop, waiting for inputs, or time to pass, before performing actions.

Then you will be able to handle things like instantaneous brake light illumination (presumably on the inactive indicator side) in the middle of a indicator flash sequence.

void loop() {
 pixels.setPixelColor(29, pixels.Color(s,t,u));
 pixels.setPixelColor(30, pixels.Color(s,t,u));
 pixels.setPixelColor(31, pixels.Color(s,t,u));

 int RightSwitchVal = digitalRead(RightSwitch);
 int BrakeSwitchVal = digitalRead(BrakeSwitch); 
 int LeftSwitchVal = digitalRead(LeftSwitch);
 unsigned long currentMillis = millis(); //initiallizes the clock 



           if (RightSwitchVal == HIGH && sequencerightrunning == LOW )
    {
     RightSwitchMillisHigh = currentMillis; //resets timer
     sequencerightrunning = HIGH; //starts sequence 
        //blank leds
          {
        for (int i = 8; i <= 19; i++)
{
  pixels.setPixelColor(i, pixels.Color(0,0,0));

   pixels.show();

         }
          }
    

 pixels.show();
   
    }


    if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervala)                                                                                                                               
         {
                       pixels.setPixelColor(8, pixels.Color(a,b,c));  
                       pixels.setPixelColor(20, pixels.Color(a,b,c));      
     pixels.show(); 
         }

 if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervalb)                                                                                                                               
         {
                       pixels.setPixelColor(9, pixels.Color(a,b,c));   
                       pixels.setPixelColor(19, pixels.Color(a,b,c));     
     pixels.show(); 
         }
 if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervalc)                                                                                                                               
         {
                       pixels.setPixelColor(10, pixels.Color(a,b,c));  
                       pixels.setPixelColor(18, pixels.Color(a,b,c));      
     pixels.show(); 
         }
 if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervald)                                                                                                                               
         {
                       pixels.setPixelColor(11, pixels.Color(a,b,c));  
                       pixels.setPixelColor(17, pixels.Color(a,b,0));      
     pixels.show(); 
         }
 if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervale)                                                                                                                               
         {
                      pixels.setPixelColor(12, pixels.Color(a,b,0)); 
                      pixels.setPixelColor(16, pixels.Color(a,b,0));       
     pixels.show(); 
         }


 if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervalf)                                                                                                                               
         {
                      pixels.setPixelColor(13, pixels.Color(a,b,0)); 
                      pixels.setPixelColor(15, pixels.Color(a,b,0));       
     pixels.show(); 
         }

 if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= intervalg)                                                                                                                               
         {
                    pixels.setPixelColor(14, pixels.Color(a,b,0)); 
                            
     pixels.show(); 
         }


         
if(sequencerightrunning == HIGH && currentMillis - RightSwitchMillisHigh >= interval)                                                                                                                               
         {
                   sequencerightrunning = LOW;         
      
         }





//default right
if(sequencerightrunning == LOW && BrakeSwitchVal == LOW && currentMillis - RightSwitchMillisHigh >= intervali ) 
         {
        for (int i = 8; i <= 20; i++)
{
  pixels.setPixelColor(i, pixels.Color(d,e,f));
}

   pixels.show();
}
         

//BRAKE right
if(sequencerightrunning == LOW && BrakeSwitchVal == HIGH && currentMillis - RightSwitchMillisHigh >= intervali )                                                                                                                               
         {
        for (int i = 8; i <= 20; i++)
{
  
   pixels.setPixelColor(i, pixels.Color(m,n,o));
   pixels.show();
}
         }
         


         
}

snippet attached of final working code
just like to thank you for your help, your few pointers steered me in the direction I needed to go. works perfectly now thanks again ,andy