Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

NeoPixel
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

NeoPixel

by MAVV on Fri Apr 10, 2020 2:34 pm

Hello Guys
I have a problem with my NeoPixel projekt and my funktion. Im very new i programming an Arduino.
The funktion shud be:
If Button 1 HIGH turn LED strip green with a little effekt after this changing brightnis like a pulse.
If Button 1 LOW turn LED strip red with a little effekt after this changing brightnis like a pulse.
If I press Butten 2 so it is HIGH turn on Strip with white light.
But the light can be over written by the other light any time.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN    6
#define LED_COUNT 80
const int Input1 = 1;
const int Input2 = 2;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int StatusEingang1 = 1;
int StatusEingang2 = 0;


void setup() {
  pinMode(Input1, INPUT);
  pinMode(Input2, INPUT);
  StatusEingang1 = digitalRead(Input1);
  StatusEingang2 = digitalRead(Input2);
 
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}

void loop() {
StatusEingang1 = digitalRead(Input1);
StatusEingang2 = digitalRead(Input2);
if (StatusEingang2 == LOW){
for (int w = 0; w <= 80; w++){
strip.setPixelColor(w, 255, 255, 255);
strip.show();
delay(100);
}
StatusEingang1 = digitalRead(Input1);
  StatusEingang2 = digitalRead(Input2);
} else {
StatusEingang1 = digitalRead(Input1);
 StatusEingang2 = digitalRead(Input2);
}
if (StatusEingang1 == HIGH){
for (int r = 0; r <= 80; r++) {
strip.setPixelColor(r, 255, 0, 0);
strip.show();
}
} else {
for (int g = 0; g <= 80; g++) {
strip.setPixelColor(g, 0, 255, 0);
strip.show();
delay(100);
}
StatusEingang1 = digitalRead(Input1);
 StatusEingang2 = digitalRead(Input2);
  }
}

chears!

MAVV
 
Posts: 2
Joined: Fri Apr 10, 2020 1:59 pm

Re: NeoPixel

by dastels on Fri Apr 10, 2020 8:49 pm

As the code is now, your NeoPixels will either be red or green, yes? Because regardless what happens due to StatusEingang2, they will be set based on the state of StatusEingang1. So maybe the first step is to look at StatusEingang1 only when StatusEingang2 is HIGH (not pressed).

Something like:

Code: Select all | TOGGLE FULL SIZE
 
if (StatusEingang2 == LOW) {
   ...
} else {
  if (StatusEingang1 == HIGH) {
      ...
  } else {
     ...
  }
}


I'd also remove all digital reads except the ones at the very beginning of the loop function. Loop is called repeatedly by the framework, so reading your input when it starts, then acting on them, usually suffices.

One other thing is that when connecting a switch, you generally want to use a pinMode of INPUT_PULLUP, unless you have an external pullup resistor.

Dave

dastels
 
Posts: 3534
Joined: Tue Oct 20, 2015 3:22 pm

Re: NeoPixel

by MAVV on Sat Apr 11, 2020 5:50 am

Thanks 4 your help. Yes it make sense to remove the digitalread. I thought that the animation could be interrupted and another step triggered. The program should then not follow a fixed course. Maybe you can just put the query in the void loop, and that or program in a void?
chears

MAVV
 
Posts: 2
Joined: Fri Apr 10, 2020 1:59 pm

Please be positive and constructive with your questions and comments.