• 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 ?)
#1
Witam
Od dłuższego czasu próbuję zrobić przyciski które będą włączały animację na poszczególnym przycisku daną animację. Problem jest taki że nigdzie coś takiego nie znajdę. Mam jakieś gotowy program poniżej, może pomoże to w modyfikacji tak aby działało.

1. Ten program polega na tym, że jak nacisnę przycisk to zmienia się w kolejność animacji jak się napisało, czyli w tym przypadku tak:
Kod:
void loop() {
 colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
 debounce();
 rainbow(20);
 debounce();
 rainbowCycle(20);
debounce();
 colorWave(20);
 debounce();
}

To jest cały kod:

Kod:
#include <Adafruit_NeoPixel.h>
float  czas;
#define PIN 6
#define STRIPSIZE 25

// Parameter 1 = number of pixels in strip
// Parameter 2 = 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPSIZE, PIN, NEO_GRB + NEO_KHZ800);

#define switch_0 A0
int tim = 60;
bool oldState = HIGH;
int stan = 0;

void setup() {
 strip.begin();
 strip.show(); // Initialize all pixels to 'off'
 pinMode(switch_0, INPUT_PULLUP);
}

void debounce() {
 unsigned long sta = millis();
 while (millis() - sta < 20UL) {
   if (digitalRead(switch_0) == LOW) {
     sta = millis();
   }
 }
}


// Main activity.
void loop() {
 colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
 debounce();
 rainbow(20);
 debounce();
 rainbowCycle(20);
debounce();
 colorWave(20);
 debounce();
}

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);
     if (digitalRead(switch_0) == LOW) {
       return;
     }
   }
 }
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
 for (;;) {
   for (uint16_t i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, c);
     strip.show();
     delay(wait);
     if (digitalRead(switch_0) == LOW) {
       return;
     }
   }
 }
}


// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
 for (;;) {
 uint16_t i, j;

 for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
   for (i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();

   delay(wait);
   if(digitalRead(switch_0) == LOW) {
               break;
           }
 }
}
}
// 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);
 }
}

/**
       ^   ^   ^
  ~~~~~ ColorWave ~~~~~
         V   V   V
*/
void colorWave(uint8_t wait) {
 for (;;){
 int i, j, stripsize, cycle;
 float ang, rsin, gsin, bsin, offset;

 static int tick = 0;

 stripsize = strip.numPixels();
 cycle = stripsize * 25; // times around the circle...

 while (++tick % cycle) {
   offset = map2PI(tick);

   for (i = 0; i < stripsize; i++) {
     ang = map2PI(i) - offset;
     rsin = sin(ang);
     gsin = sin(2.0 * ang / 3.0 + map2PI(int(stripsize / 6)));
     bsin = sin(4.0 * ang / 5.0 + map2PI(int(stripsize / 3)));
     strip.setPixelColor(i, strip.Color(trigScale(rsin), trigScale(gsin), trigScale(bsin)));
   }

   strip.show();
   delay(wait);
   if(digitalRead(switch_0) == LOW) {
               return;
           }
 }

}
}
/**
  Scale a value returned from a trig function to a byte value.
  [-1, +1] -> [0, 254]
  Note that we ignore the possible value of 255, for efficiency,
  and because nobody will be able to differentiate between the
  brightness levels of 254 and 255.
*/
byte trigScale(float val) {
 val += 1.0; // move range to [0.0, 2.0]
 val *= 127.0; // move range to [0.0, 254.0]

 return int(val) & 255;
}

/**
  Map an integer so that [0, striplength] -> [0, 2PI]
*/
float map2PI(int i) {
 return PI * 2.0 * float(i) / float(strip.numPixels());
}


Do każdej animacji został dodany funkcja która przerywa animację, czyli pętla się zatrzymuje:

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);
     if (digitalRead(switch_0) == LOW) {      // to przerywa animację
       return;                                //
     }
   }
 }
}


Pracuję teraz na płytce genuino 101 więc na dość mocnej płytce
 
Odpowiedź
  


Wiadomości w tym wątku
ws2812 - przyciski przełączające animacje(jak ?) - przez Jakub428 - 16-06-2019, 13:31

Skocz do:


Przeglądający: 1 gości