• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
ws2812 - przyciski przełączające animacje(jak ?)
#8
Przez ten czas coś tam próbowałem ale nie wyszło. Usunąłem delay z kodu dodałem  - interrupte - Ale to nie działa jak powinno. Przycisk nie działają, nie włącza animację albo kolor czerwony.

Nie wiem co miał na myśli kaczakat z tym:
"W funkcji wywołanej przez przerwanie ustaw sobie zmienną volatile cośtam by przyjmowała stan 1, w miejscu gdzie możesz opuścić funkcję sprawdzaj czy jest 1, zeruj i przerywaj działanie."


Problem jest teraz taki że nie wiem jak dać przerwanie, bo może dla tego nie działają przyciski
kod do przerwania animacji:
Kod:
void rainbow(uint8_t wait) {
 for (;;) {
   uint16_t i, j;

   for (j = 0; j < 256; j++) {
     for (i = 0; i < strip.numPixels(); i++) {
       strip.setPixelColor(i, Wheel((i + j) & 255));
     }
     strip.show();
     delay(wait);

// jak teraz to dodać poniżej jeżeli mam więcej przycisków(czyli 20 przycisków)
     if (digitalRead(switch_0) == LOW) {
       return;
       showType = 0;
     }
   }
 }
}

Jak ktoś ma pomysł pisać jaka linijka kodu albo np. -  przed uint16_t i, j; dodaj delay  - bo to lepiej w tedy rozumiem.

kod cały:
Kod:
/*
 Modified by combining these tutorials
   Blink without Delay  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
   State change detection (edge detection) http://arduino.cc/en/Tutorial/ButtonStateChange
   Adafruit Strandtest https://github.com/adafruit/Adafruit_NeoPixel
   http://www.arduino.cc/en/Tutorial/Debounce
   https://github.com/EternalCore/NeoPixel_Cylon_Scrolling_Eye
*/
#include <Adafruit_NeoPixel.h>

// constants won't change.
const int buttonPin = 0; // the number of the pushbutton pin
const int buttonPin1 = 1;
#define PIN 6 //the number of the neopixel pic
#define numPixelsInStrip 25
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixelsInStrip, PIN, NEO_GRB + NEO_KHZ800);


// przycisk 1 //
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int programState = 0; // current program seleted -- buttonpresses rotate between programs
int numOfPrograms = 7; // how many programs are there total, not including the off state


// przycisk 2 //
int ledState1 = HIGH; // the current state of the output pin
int buttonState1; // the current reading from the input pin
int lastButtonState1 = LOW; // the previous reading from the input pin
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long debounceDelay1 = 50; // the debounce time; increase if the output flickers
int programState1 = 0; // current program seleted -- buttonpresses rotate between programs
int numOfPrograms1 = 7; // how many programs are there total, not including the off state



//unsigned long currentMillis = 0;  //for programs
long previousMillis = 0;        // will store last time LED was updated
int neoPixelToChange = 0; //track which neoPixel to change
int neoPixel_j = 0; //stores values for program cycles
int defaultBrightness = 64;

//cylon variables
int fadeDirection = -1;//change sigen to fade up or down
boolean cylonDirection = true; //keeps track of the direction the pixels should swipe
boolean cylonPause = false; //keeps track of the pause inbetween swipes
long delayMillis = 0; // will store the last time the cylon swipe was paused

void setup() {
 pinMode(buttonPin, INPUT_PULLUP);
 attachInterrupt(digitalPinToInterrupt(buttonPin), StartShow, FALLING);
 pinMode(buttonPin1, INPUT_PULLUP);
 attachInterrupt(digitalPinToInterrupt(buttonPin1), StartShow, FALLING);
 strip.begin();
 strip.show(); // Initialize all pixels to 'off'
 strip.setBrightness(defaultBrightness); // initialize brightness
}

void loop() {

 // przycisk 1 //
 int reading = digitalRead(buttonPin);
 if (reading != lastButtonState) {
   lastDebounceTime = millis();
 }
 if ((millis() - lastDebounceTime) > debounceDelay) {
   if (reading != buttonState) {
     buttonState = reading;
     if (buttonState == HIGH) {
       programState = 0;
       StartShow(programState);
     }
   }
 }

 lastButtonState = reading;
 // przycisk 2 //
 int reading1 = digitalRead(buttonPin1);
 if (reading1 != lastButtonState1) {
   lastDebounceTime1 = millis();
 }
 if ((millis() - lastDebounceTime1) > debounceDelay1) {
   if (reading1 != buttonState1) {
     buttonState1 = reading1;
     if (buttonState1 == HIGH) {
       programState = 1;
       StartShow(programState);
     }
   }
 }

 lastButtonState = reading;
}


// Co dany przycisk ma wykonać(jaką animację) //
void StartShow(int i) {
 switch (i) {
   case 0:
     colorWipe(strip.Color(0, 255, 0), 500); // green
     break;
   case 1:
     strip.setBrightness(defaultBrightness); // initialize brightness
     rainbow(5);
     break;
 }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {

 unsigned long currentMillis = millis();
 if (neoPixelToChange <= strip.numPixels()) {
   if (currentMillis - previousMillis > wait) {
     previousMillis = currentMillis;
     strip.setPixelColor(neoPixelToChange, c);
     strip.show();
     neoPixelToChange++;
   }
 }
}

// Fill the dots one after the other with a color


void rainbow(uint8_t wait) {

 unsigned long currentMillis = millis();

 if (currentMillis - previousMillis > wait) {

   // save the last time you changed a NeoPixel
   previousMillis = currentMillis;

   //change the colors of the pixels
   uint16_t i;

   for (i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel((i + neoPixel_j) & 255));
   }
   strip.show();
   neoPixel_j = (neoPixel_j + 1) % 255; //increment j until all colors are used, then start over
 }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {

 unsigned long currentMillis = millis();

 if (currentMillis - previousMillis > wait) {

   // save the last time you changed a NeoPixel
   previousMillis = currentMillis;

   //change the colors of the pixels
   uint16_t i;

   for (i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + neoPixel_j) & 255));
   }
   strip.show();
   neoPixel_j = (neoPixel_j + 1) % 1279; // 5 cycles of all colors on wheel, then start over
 }

}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
 if (WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
 } else if (WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
}

void cylonEye(uint8_t swipeSpeed, uint8_t pauseTime) {

 if (cylonPause) { //we are on a pause break from swipes

   unsigned long currentPauseMillis = millis();

   //check to see if we've waited long enough
   if ((currentPauseMillis - delayMillis) > pauseTime) { /////////////////////////////// this is not getting called. Why????????????????z
     // save the last time you checked the pause
     delayMillis = currentPauseMillis;
     cylonPause = false; //end the pause
   }

 } else {

   //if needed, change directions
   if (neoPixelToChange > numPixelsInStrip) {
     cylonDirection = false;
   }
   if (neoPixelToChange < 0) {
     cylonDirection = true;
     cylonPause = true; //take a break from the swipe
     //turn all pixels off
     for (uint16_t i = 0; i < strip.numPixels(); i++) {
       strip.setPixelColor(i, strip.Color(0, 0, 0));
     }
     strip.show();
     delayMillis = millis();
   }

   //run the swipe
   if (cylonDirection) {
     cylonUp(strip.Color(255, 0, 0), strip.Color(72, 0, 0), swipeSpeed); // red
   } else {
     cylonDown(strip.Color(255, 0, 0), strip.Color(72, 0, 0), swipeSpeed); // red
   }
 }
}

void cylonUp(uint32_t c0, uint32_t c1, uint8_t wait) {

 unsigned long currentMillis = millis();

 //neoPixelToChange * wait
 if (currentMillis - previousMillis > wait) {

   //turn all pixels off
   for (uint16_t i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, strip.Color(0, 0, 0));
   }

   // save the last time you changed a NeoPixel
   previousMillis = currentMillis;

   //change a pixel
   strip.setPixelColor(neoPixelToChange, c0); //primary color
   strip.setPixelColor(neoPixelToChange - 1, c1); //secondary color
   strip.setPixelColor(neoPixelToChange + 1, c1); //secondary color
   strip.show();
   neoPixelToChange++;
 }
}

void cylonDown(uint32_t c0, uint32_t c1, uint8_t wait) {

 unsigned long currentMillis = millis();

 //neoPixelToChange * wait
 if (currentMillis - previousMillis > wait) {

   //turn all pixels off
   for (uint16_t i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, strip.Color(0, 0, 0));
   }

   // save the last time you changed a NeoPixel
   previousMillis = currentMillis;

   //change a pixel
   strip.setPixelColor(neoPixelToChange, c0); //primary color
   strip.setPixelColor(neoPixelToChange - 1, c1); //secondary color
   strip.setPixelColor(neoPixelToChange + 1, c1); //secondary color
   strip.show();
   neoPixelToChange--; //is there any way to combine this with cylonUp, since this is the only line that is different?
 }
}
 
Odpowiedź
  


Wiadomości w tym wątku
RE: ws2812 - przyciski przełączające animacje(jak ?) - przez Jakub428 - 18-06-2019, 14:46

Skocz do:


Przeglądający: 1 gości