Neopixel animation need to moving together/without delay

Hello guys.. I have this code that have fuction in

case 0b0011: // both rightTurnState and leftTurnState

that need to moving together or without delay..
how i can manage this? try to use blink without delay but not succes i dunno why

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 9
#define PIXEL_COUNT 24

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() {
  
  strip.clear();
  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
      for(int i=0; i<24; i++) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
  		}
      break;

    case 0b0001:  // only leftTurnState
        leftturn();
      break;

    case 0b0010: // only rightTurnState
		rightturn();
      break;  

    case 0b0011: // both rightTurnState and leftTurnState
		rightturn();
    	delay(0);
		leftturn();
      break;  
    
  	}
}

void leftturn(){
	for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
		strip.show();
		delay(50);
      	}
    for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
		delay(0);
      	}
}

void rightturn(){
      for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
		strip.show();
		delay(50);
      	}
      for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
		delay(0);
      	}
}

tinkercad

Please post your best effort to use millis() for timing and explain what problems you encountered

You need to combine "for" loops for left turn and right turn in one cycle.

What are you doing this?? ... some weird piece of code...

strip.setPixelColor(i, strip.Color(250, 75, 0));
strip.setPixelColor(i, strip.Color(0, 0, 0));

like this.. but stil the same.. left turn move after right turn

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN 9
#define PIXEL_COUNT 24

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;

unsigned long interval=0;  // the time we need to wait
unsigned long previousMillis=0;

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() {
  
  strip.clear();
  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
      for(int i=0; i<24; i++) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
  		}
      break;

    case 0b0001:  // only leftTurnState
        leftturn();
      break;

    case 0b0010: // only rightTurnState
		rightturn();
      break;  

    case 0b0011: // both rightTurnState and leftTurnState
    if ((unsigned long)(millis() - previousMillis) >= interval) {
  		previousMillis = millis();
		rightturn();
		leftturn();
    }
      break;  
    
  	}
}

void leftturn(){
	for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
		strip.show();
      	}
    for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
      	}
}

void rightturn(){
      for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
		strip.show();
      	}
      for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
      	}
}

opps wrong code :sweat_smile:

like this?

void leftrightturn(){
    for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
		strip.show();
      	}
    for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
      	}
	for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
		strip.show();
      	}
    for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(0, 0, 0));
		strip.show();
      	}
}

Do you test it?

You make the same mistake as this every time. Take the strip.show call outside the for loop.

for(int i=16;i<24;i++) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));
      	}
		strip.show();

not in this case. OP needs the effect of a gradually lit line

Then he is wasting everyone's time with this code. It needs to be a state machine and the whole thing needs to be completely rewritten.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html[/url]
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0%5B/url%5D

yes that code i use before and still the same effect.. all this code i get from everywhere i can find.. i cant coding just copy paste.. new to this :pensive:

I agree with you in general
but this small task can be easy written just in for loop without of SM power :slight_smile:

OK then you probably can't do this project with your current state of knowledge. You need to learn:-

  1. First how to read code, and understand what it is doing.
  2. Know what things you can change to affect what the code produces.
  3. Then you can start writing code.

yes im sorry guys wasting your time with my silly question.. i will learn more from begining.. not jump to far

i will learn more.. maybe close this thread for now until i understand what im doing

@yucos , loop to turn on:

for(int i=7;i>-1;i--) {
		strip.setPixelColor(i, strip.Color(250, 75, 0));  // left part
        strip.setPixelColor(23-i, strip.Color(250, 75, 0));  // right
		strip.show();
      	}

test it and let me know, is it works.

To turn off try to write yourself

@b707 yesss it works.. thanks man.. and yes i will learn more from begining

I would use:

uint32_t Red = strip.Color(250, 75, 0);
uint32_t Black = strip.Color(0, 0, 0);

    for(int i=0; i < 8; i++) 
    {
      strip.setPixelColor(7-i, Red);  // left side, right to left
      strip.setPixelColor(16+i, Red);  // right side, left to right
      strip.show();
      delay(50);
    }

thank its more simpler to choosing colour now

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