Hoi,
Ik ben met een projectje bezig waarbij ik een setup heb gemaakt om 2 neopixel sticks, 2 neopixels en een 1 Watt LED te laten runnen als de lichten voor mijn fiets. ![]()
De 2 neopixel sticks van 8 ceopixels functioneren als het achterlicht. De 2 losse neopixels zitten in mijn stuur, links en rechts, de 1 Watt als mijn voorlicht, en dan heb ik nog 2 knoppen op mijn stuur zitten om de linker of rechter nepixels aan te sturen.
Mijn eesrte vraag:
Ik heb al een code gemaakt voor alleen de achterlichten(de twee 8neopixel sticks), deze werke goed; wanneer ik de linker knop in drukte, de linker neopixel begon the knipperen en hetzelfde geval voor de rechter knop en neopixel stick.
Alleen zat er wat lag in, aangezien hij de heletijd in een loop zit, kon het nog wel eens zijn dat hij niet reageerde wanneer ik op de knop drukte. Een vriend van mij zei dat dit kwam omdat hij in de loop er voor bij was en dus dat niet meer ziet als een functie. Hiervoor moet ik dus een Interrupt in de code instellen. Ik heb als ik eerlijk ben geen ervaring met dit en op het web kon ik weinig vinden over hoe ik dit kon gebruiken in mijn code.
Mijn tweede vraag is;
Aangezien ik 2 losse neopixels heb zitten in mijn stuur, links en rechts, moet ik deze ook laten aansturen via de knoppen. Nu heb ik de code aangepast en voor mijn gevoel moet dit kloppen.
MAAR; kan ik gewoon 2 keer "for" gebruiken bij een "if" statement?
EN: hoe geef ik aan dat dit alleen 1 neopixel is, en niet een stick van 8?
Alvast heel erg bedankt,
![]()
gr bobby
Hier mijn code:
#include <Adafruit_NeoPixel.h>
//#define PIN 6
//#define PIN 5
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 2000 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
// changed number of pixels
Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(8, 5); // left rear strip
Adafruit_NeoPixel strip_b = Adafruit_NeoPixel(8, 6); // right rear strip
Adafruit_NeoPixel strip_c = Adafruit_NeoPixel(1, 10); // left front dot
Adafruit_NeoPixel strip_d = Adafruit_NeoPixel(1, 11); // right front dot
const int buttonPinL = 2;
const int buttonPinR = 3;
boolean buttonStateL = 0; // modified to boolean
boolean buttonStateR = 0; // modified to boolean
//const int brakebuttonPin = 4;
//boolean buttonStateBrake = 0; // modified to boolean
const int frontPin = 13;
void setup() {
pinMode(buttonPinL, INPUT);
pinMode(buttonPinR, INPUT);
// pinMode(brakebuttonPin, INPUT);
pinMode(frontPin, OUTPUT);
strip_a.begin();
strip_a.show(); // Initialize all pixels to 'off'
strip_b.begin();
strip_b.show(); // Initialize all pixels to 'off'
strip_c.begin();
strip_c.show(); // Initialize all pixels to 'off'
strip_d.begin();
strip_d.show(); // Initialize all pixels to 'off'
}
void loop() {
buttonStateL = digitalRead(buttonPinL);
buttonStateR = digitalRead(buttonPinR);
//buttonStateBrake = digitalRead(brakebuttonPin);
digitalWrite(frontPin, HIGH);
if (buttonStateL == HIGH) {
for(int i=0; i<10; i++) {
colorWipeL(strip_a.Color(240, 140, 0), 10); //Orange blinking IF button is pressed
strip_a.setBrightness(255); // set brithness of backlight to full
strip_b.setBrightness(128); // dim brithness of opposite stick
delay(250);
colorWipeL(strip_a.Color(0, 0, 0), 0); //Blink
delay(250);
}
for (int i=0; i<10; i++) {
colorWipeLF(strip_c.Color(240, 140, 0), 10); //Orange blinking dot IF button is pressed
strip_c.setBrightness(255); // set brithness of left dot to full
delay(250);
colorWipeLF(strip_c.Color(0, 0, 0), 0); //Blink
delay(250);
}
}
else {
strip_a.setBrightness(120); // set brithness of backlight to 3/4
colorWipeL(strip_a.Color(255, 0, 0), 50); // Red rest of time
strip_c.setBrightness(255); // set brithness of left dot
colorWipeLF(strip_a.Color(123, 104, 238), 50); // Blue rest of time
}
if (buttonStateR == HIGH) {
for(int i=0; i<10; i++) {
colorWipeR(strip_b.Color(240, 140, 0), 10); //Orange IF button is pressed
strip_b.setBrightness(255); // set brithness of backlight to full
strip_a.setBrightness(128); // dim brithness of opposite stick
delay(250);
colorWipeR(strip_b.Color(0, 0, 0), 0); //Blink
delay(250);
}
for (int i=0; i<10; i++) {
colorWipeRF(strip_d.Color(240, 140, 0), 10); //Orange blinking dot IF button is pressed
strip_d.setBrightness(255); // set brithness of left dot to full
delay(250);
colorWipeRF(strip_d.Color(0, 0, 0), 0); //Blink
delay(250);
}
}
else {
strip_b.setBrightness(120); // set brithness of backlight to 3/4
colorWipeR(strip_b.Color(255, 0, 0), 50); // Red rest of time
strip_d.setBrightness(255); // set brithness of left dot
colorWipeRF(strip_d.Color(123, 104, 238), 50); // Blue rest of time
}
/* if (buttonStateBrake == LOW) {
strip_b.setBrightness(255);
strip_a.setBrightness(255);
colorWipeR(strip_b.Color(100, 100, 50), 10); //Orange IF button is pressed
colorWipeR(strip_a.Color(100, 100, 50), 10); //Orange IF button is pressed
// delay(5);
}
else {
strip_b.setBrightness(120); // set brithness of backlight to 3/4
strip_a.setBrightness(120); // set brithness of backlight to 3/4
colorWipeR(strip_b.Color(255, 0, 0), 10); // Red rest of time
colorWipeR(strip_a.Color(255, 0, 0), 10); // Red rest of time
} */
}
// Fill the dots one after the other with a color
void colorWipeL(uint32_t c, uint8_t wait) {
for(uint8_t i=0; i<strip_a.numPixels(); i++) {
strip_a.setPixelColor(i, c);
strip_a.show();
delay(wait);
}
}
// Fill the dots one after the other with a color
void colorWipeR(uint32_t c, uint8_t wait) {
for(uint8_t i=0; i<strip_b.numPixels(); i++) {
strip_b.setPixelColor(i, c);
strip_b.show();
delay(wait);
}
}
// Fill the dots one after the other with a color
void colorWipeLF(uint32_t c, uint8_t wait) {
for(uint8_t i=0; i<strip_c.numPixels(); i++) {
strip_c.setPixelColor(i, c);
strip_c.show();
delay(wait);
}
}
// Fill the dots one after the other with a color
void colorWipeRF(uint32_t c, uint8_t wait) {
for(uint8_t i=0; i<strip_d.numPixels(); i++) {
strip_d.setPixelColor(i, c);
strip_d.show();
delay(wait);
}
}