yucos
July 20, 2022, 11:04am
1
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
b707
July 20, 2022, 11:11am
3
yucos:
how i can manage this?
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));
yucos
July 20, 2022, 11:50am
4
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();
}
}
yucos
July 20, 2022, 12:01pm
6
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();
}
}
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();
b707
July 20, 2022, 12:06pm
9
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
yucos
July 20, 2022, 12:11pm
11
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
b707
July 20, 2022, 12:13pm
12
I agree with you in general
but this small task can be easy written just in for loop without of SM power
OK then you probably can't do this project with your current state of knowledge. You need to learn:-
First how to read code, and understand what it is doing.
Know what things you can change to affect what the code produces.
Then you can start writing code.
yucos
July 20, 2022, 12:17pm
14
yes im sorry guys wasting your time with my silly question.. i will learn more from begining.. not jump to far
yucos
July 20, 2022, 12:18pm
15
i will learn more.. maybe close this thread for now until i understand what im doing
b707
July 20, 2022, 12:20pm
16
@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
yucos
July 20, 2022, 12:27pm
17
@b707 yesss it works.. thanks man.. and yes i will learn more from begining
b707:
for(int i=7;i>-1;i--) {
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);
}
yucos
July 20, 2022, 4:46pm
19
thank its more simpler to choosing colour now
system
Closed
January 16, 2023, 4:47pm
20
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.