пятница, 6 ноября 2015 г.

Нарастающие и убывающие огни на ленте с WS2812 через Arduino

Скетч во время удержания кнопки светодиоды один за другим загораются, если кнопку отпустить - в обратном порядке гаснут.




#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6

int buttonPin = 9;
int led = 13;
int val = 0;
long previousMillis = 0;


Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 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.

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pinMode(led, OUTPUT);
  pinMode(buttonPin, INPUT);

  strip.begin();
  strip.setBrightness(100);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  if (digitalRead(buttonPin) == HIGH) //если кнопка нажата ...
  {
    if (millis() - previousMillis > 200)
    {
      previousMillis = millis();
      val++;
    }
  }
  else
  {
    if (val != 0) {
    colorOff(250, val);
    val = 0;
    }
    else {
      val = 0;
      }
    // strip.clear();
    // strip.show();
  }

  if (val > 0)
  {
    strip.setPixelColor(val-1, strip.Color(0, 0, 255));
    strip.show();
  }

}

void colorOff(uint8_t wait, int pixs) {

  for (uint16_t i = pixs; (i >= 0 & i < 64000) ; i--) {
    
    strip.setPixelColor(i, 0);
    strip.show();
    delay(wait);
  }
}

// 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) {

  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Комментариев нет:

Отправить комментарий