Arduino 运行Attiny85的代码,带有集成了RGB LED的WS2812

Arduino 运行Attiny85的代码,带有集成了RGB LED的WS2812,arduino,led,Arduino,Led,我正在尝试使用此芯片: 。。。fgodFpsAow + 阁楼85 我使用的代码如下所示,它与Arduino配合使用效果很好,但与Attiny85配合使用效果不佳(只需打开并保持在绿色和黄色之间)。有什么想法吗?谢谢 我的ATtiny设置为8MHz,Neopix.h库中有800Khz的数据流 代码如下 #include <Adafruit_NeoPixel.h> /* WS2811/Neopixel pattern switcher for ATtiny85 (and Arduin

我正在尝试使用此芯片:

。。。fgodFpsAow + 阁楼85

我使用的代码如下所示,它与Arduino配合使用效果很好,但与Attiny85配合使用效果不佳(只需打开并保持在绿色和黄色之间)。有什么想法吗?谢谢

我的ATtiny设置为8MHz,Neopix.h库中有800Khz的数据流

代码如下

#include <Adafruit_NeoPixel.h> 
/*
 WS2811/Neopixel pattern switcher for ATtiny85 (and Arduino)
 Requires Adafruit NeoPixel Library
 WS2811 Signal, Digital Pin 4
 Button, Digital Pin 0
 GPL v3
 */
/*
shin:random pattern selector
*/


// Define
#define NUM_LEDS 1 //60LED per strip
#define DATA_PIN 0 //output pin on ATTiny85
#define BTN_PIN 1 //input pin on ATTiny85
#define BTN_DELAY 250 //add delay for debounce
#define NUM_PATTERNS 12 //patterns avail
#define CTR_THRESH 16

// Init Vars
uint8_t j = 0;
uint8_t pattern=1;
uint8_t buttonState=0;
uint8_t lastPix=0; 
uint8_t myPix=0;
uint8_t direction=1;
uint8_t counter=0;
uint8_t colors[3];
uint32_t setColor=0;
unsigned long mark;

// Start Strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);

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

void loop() {
    // if button pressed, advance, set mark
    //chkBtn(digitalRead(BTN_PIN));
   int rand = random(1,13);//1to12
    // if pattern greater than #pattern reset
    //if (pattern > NUM_PATTERNS) { pattern = 1; }
    if (rand > NUM_PATTERNS) { pattern = 1; }

    // choose a pattern
    //pickPattern(pattern);
    pickPattern(rand);

    // set direction
    if (direction == 1) { j++;  } else {  j--; }

    if (j > 254) { direction = 0; }
    if (j < 1) { direction = 1; }   

}

/* pick a pattern */
void pickPattern(uint8_t var) {
      switch (var) {
        case 1:
          // scanner, color and delay - RGB
          scanner(strip.Color(255,0,0),50);
          scanner(strip.Color(200,0,100),50);
          scanner(strip.Color(64,0,200),50);
        break;
        case 2:
          // color wipe random RGB
          colorWipe(strip.Color(random(255), random(255), random(255)),50);
        break;
        case 3:
          // color wave - Hue/Sat/Bright
          // hue low (0-359), high (0-359),rate,extra delay
          wavey(200,240,0.06,0);
        break;
        case 4:
          // rainbow firefly, 1px at random
          colorFirefly(60);
          counter++;
        break;
        case 5:
          // rainbow solid
          rainbow(10);
          counter++;
        break;
        case 6:
           // bounce in and out
           // tail len, counter, delay
           bounceInOut(4,counter,20);
           counter++;
        break;
        case 7:
           // color wipe from center
           colorWipeCenter(strip.Color(255,0,0),100);
           colorWipeCenter(strip.Color(255,64,0),100);
        break;
        case 8:
           // solid color
           colorFast(strip.Color(255,0,0),0);
        break;
        case 9:
          // fade even or odd
          // 0-359 Hue value, even/odd, delay
          fadeEveOdd(200,0,20);
          fadeEveOdd(300,1,20);
        break;
        case 10:
          // show rainbow
          rainbowCycle(10);
         break; 

    case 11:
        // show rainbow
        theaterChaseRainbow(50);
         break; 

      case 12:
      rainbowCycle2(20);
         break;
      }
}

/* check button state */
boolean chkBtn(int buttonState) {
   if (buttonState == HIGH && (millis() - mark) > BTN_DELAY) {
       j = 0;
       mark = millis();
       pattern++;
       return true;
    } 
    else { return false; }
}

void colorFirefly(int wait) {
        if(myPix != lastPix) {
          if(counter<CTR_THRESH) {
            float colorV = sin((6.28/30)*(float)(counter)) *255;
            HSVtoRGB((359/CTR_THRESH)*counter, 255, colorV, colors);
            strip.setPixelColor(myPix, colors[0], colors[1], colors[2]);
           strip.show();
           delay(wait);
          } else {
            lastPix=myPix;
            counter=0;
            colorFast(0,0);
          }
        } else {
          myPix=random(0,strip.numPixels());
        }

}

// Fill the dots one after the other with a color
// Modified from Neopixel sketch to break on button press

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      if(chkBtn(digitalRead(BTN_PIN))) { break; }
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

// color wipe from center
void colorWipeCenter(uint32_t c, uint8_t wait) {
  uint8_t mid=strip.numPixels()/2;
  strip.setPixelColor(mid,c);
  for(uint16_t i=0; i<=strip.numPixels()/2; i++) {
      if(chkBtn(digitalRead(BTN_PIN))) { break; }
      strip.setPixelColor(mid+i, c);
      strip.setPixelColor(mid-i, c);
      strip.show();
      delay(wait);
  }
}

// fast version 
void colorFast(uint32_t c, uint8_t wait) {
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
    }
    strip.show();
    delay(wait);
}

// Rainbow Cycle, modified from Neopixel sketch to break on button press
void rainbowCycle(uint8_t wait) {
    uint16_t i;

    //  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);
    // }
}

void rainbow(uint8_t wait) {
    uint16_t i;

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

// scanner

void scanner(uint32_t c,uint8_t wait) {
        for(int i=0; i< strip.numPixels(); i++) {
            if(chkBtn(digitalRead(BTN_PIN))) { break; }

            colorFast(0,0);
            strip.setPixelColor(i,c);
            strip.show();
            delay(wait);
        }
        for(int i=strip.numPixels(); i>0; i--) {
           if(chkBtn(digitalRead(BTN_PIN))) { break; }
            colorFast(0,0);
            strip.setPixelColor(i,c);
            strip.show();
            delay(wait);
        }    
}

// scanner to midpoint
void bounceInOut(uint8_t num, uint8_t start,uint8_t wait) {
  colorFast(0,0);
  uint8_t color=200;
  for(int q=0; q < num; q++) {
    if(strip.numPixels()-start >= 0 && start < NUM_LEDS) {
          strip.setPixelColor(start+q, strip.Color(0,color,0));
          strip.setPixelColor(strip.numPixels()-start-q, strip.Color(0,color,0));
      }   
    color=round(color/2.0);
    strip.show();
    delay(wait);
  }
  if(counter > strip.numPixels()) { counter=0; }
}

void fadeEveOdd(int c1,byte rem,uint8_t wait) {
              for(int j=0; j < CTR_THRESH; j++) {
                      for(int i=0; i< strip.numPixels(); i++) {
                        if(i % 2== rem) {
                           HSVtoRGB(c1,255,(255/CTR_THRESH)*j,colors);
                           strip.setPixelColor(i,colors[0],colors[1],colors[2]);
                         }
                      }           
                      if(chkBtn(digitalRead(BTN_PIN))) { break; }
                      strip.show();
                      delay(wait);
                }
                for(int j=CTR_THRESH; j >= 0; j--) {
                      for(int i=0; i< strip.numPixels(); i++) {
                        if(i % 2== rem) {
                           HSVtoRGB(c1,255,(255/CTR_THRESH)*j,colors);
                           strip.setPixelColor(i,colors[0],colors[1],colors[2]);
                         }
                      }             
                     if(chkBtn(digitalRead(BTN_PIN))) { break; }
                      strip.show();
                      delay(wait);
                } 
}

// twinkle random number of pixels
void twinkleRand(int num,uint32_t c,uint32_t bg,int wait) {
    // set background
     colorFast(bg,0);
     // for each num
     for (int i=0; i<num; i++) {
       strip.setPixelColor(random(strip.numPixels()),c);
     }
    strip.show();
    delay(wait);
}

// sine wave, low (0-359),high (0-359), rate of change, wait
void wavey(int low,int high,float rt,uint8_t wait) {
  float in,out;
  int diff=high-low;
  int step = diff/strip.numPixels();
  for (in = 0; in < 6.283; in = in + rt) {
       for(int i=0; i< strip.numPixels(); i++) {
           out=sin(in+i*(6.283/strip.numPixels())) * diff + low;
           HSVtoRGB(out,255,255,colors);
           strip.setPixelColor(i,colors[0],colors[1],colors[2]);
       }
           strip.show();
           delay(wait);
           if(chkBtn(digitalRead(BTN_PIN))) { break; }
  }
}

// sine wave, intensity
void waveIntensity(float rt,uint8_t wait) {
  float in,level;
  for (in = 0; in < 6.283; in = in + rt) {
       for(int i=0; i< strip.numPixels(); i++) {
         // sine wave, 3 offset waves make a rainbow!
        level = sin(i+in) * 127 + 128;
        // set color level 
        strip.setPixelColor(i,(int)level,0,0);
       }
           strip.show();
           delay(wait);
           if(chkBtn(digitalRead(BTN_PIN))) { break; }
  }
}

// helpers 

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);
    }
}


// HSV to RGB colors
// hue: 0-359, sat: 0-255, val (lightness): 0-255
// adapted from http://funkboxing.com/wordpress/?p=1366
void HSVtoRGB(int hue, int sat, int val, uint8_t * colors) {
    int r, g, b, base;
    if (sat == 0) { // Achromatic color (gray).
        colors[0] = val;
        colors[1] = val;
        colors[2] = val;
    } else {
        base = ((255 - sat) * val) >> 8;
        switch (hue / 60) {
        case 0:
            colors[0] = val;
            colors[1] = (((val - base) * hue) / 60) + base;
            colors[2] = base;
            break;
        case 1:
            colors[0] = (((val - base) * (60 - (hue % 60))) / 60) + base;
            colors[1] = val;
            colors[2] = base;
            break;
        case 2:
            colors[0] = base;
            colors[1] = val;
            colors[2] = (((val - base) * (hue % 60)) / 60) + base;
            break;
        case 3:
            colors[0] = base;
            colors[1] = (((val - base) * (60 - (hue % 60))) / 60) + base;
            colors[2] = val;
            break;
        case 4:
            colors[0] = (((val - base) * (hue % 60)) / 60) + base;
            colors[1] = base;
            colors[2] = val;
            break;
        case 5:
            colors[0] = val;
            colors[1] = base;
            colors[2] = (((val - base) * (60 - (hue % 60))) / 60) + base;
            break;
        }

    }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
        }
        strip.show();

        delay(wait);

        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, 0);        //turn every third pixel off
        }
    }
  }
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle2(uint8_t wait) {
  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);
  }
}
#包括
/*
WS2811/ATtiny85(和Arduino)用Neopix图案切换器
需要Adafruit Neopix库
WS2811信号,数字引脚4
按钮,数字引脚0
GPLv3
*/
/*
shin:随机模式选择器
*/
//定义
#定义每个条带1//60LED的数量
#定义ATTiny85上的数据_引脚0//输出引脚
#定义BTN_引脚1//ATTiny85上的输入引脚
#定义BTN_延迟250//为去抖动添加延迟
#定义NUM_模式12//PATTERNS avail
#定义CTR_阈值16
//初始化变量
uint8_t j=0;
uint8_t模式=1;
uint8_t buttonState=0;
uint8_t lastPix=0;
uint8_t myPix=0;
uint8_t方向=1;
uint8_t计数器=0;
uint8_t颜色[3];
uint32_t setColor=0;
无符号长标记;
//起始带
Adafruit_NeoPixel条=Adafruit_NeoPixel(数个LED、数据管脚、NEO_GRB+NEO_KHZ800);
无效设置(){
//引脚模式(BTN_引脚,输入);
随机种子(BTN_-PIN);
strip.begin();
strip.show();//将所有像素初始化为“关闭”
}
void循环(){
//如果按下按钮,则前进,设置标记
//chkBtn(数字读取(BTN_引脚));
int rand=random(1,13);//1到12
//如果图案大于#图案重置
//如果(pattern>NUM_PATTERNS){pattern=1;}
如果(rand>NUM_PATTERNS){pattern=1;}
//选择一种模式
//选择模式(模式);
选择模式(兰德);
//设定方向
如果(方向==1){j++;}其他{j--;}
如果(j>254){direction=0;}
如果(j<1){方向=1;}
}
/*选择一种模式*/
无效选取模式(uint8\u t变量){
开关(var){
案例1:
//彩色和延迟扫描仪-RGB
扫描仪(条带颜色(255,0,0),50);
扫描仪(彩色条带(200,0100),50);
扫描仪(彩色条带(64,0200),50);
打破
案例2:
//彩色擦除随机RGB
彩色擦拭(条纹颜色(随机(255)、随机(255)、随机(255))、50);
打破
案例3:
//色波-色调/饱和度/亮度
//色调低(0-359)、高(0-359)、速率、额外延迟
韦维(200240,0.06,0);
打破
案例4:
//彩虹萤火虫,随机1px
彩色萤火虫(60);
计数器++;
打破
案例5:
//彩虹固体
彩虹(10);
计数器++;
打破
案例6:
//进进出出
//尾透镜、计数器、延迟
弹跳(4,计数器,20);
计数器++;
打破
案例7:
//从中心擦除颜色
颜色中心(条带颜色(255,0,0),100);
颜色中心(条带颜色(255,64,0),100);
打破
案例8:
//纯色
不褪色(条纹颜色(255,0,0),0);
打破
案例9:
//淡出偶数或奇数
//0-359色调值,偶数/奇数,延迟
fadeEveOdd(200,0,20);
fadeEveOdd(300,1,20);
打破
案例10:
//显示彩虹
彩虹循环(10);
打破
案例11:
//显示彩虹
剧院(50);
打破
案例12:
彩虹循环2(20);
打破
}
}
/*检查按钮状态*/
布尔值chkBtn(int按钮状态){
如果(buttonState==高和(毫秒()-标记)>BTN\U延迟){
j=0;
标记=毫秒();
模式++;
返回true;
} 
else{return false;}
}
void colorFirefly(int等待){
如果(myPix!=lastPix){
如有需要(8号柜台;;
开关(色调/60){
案例0:
颜色[0]=val;
颜色[1]=((val-基色)*色调)/60)+基色;
颜色[2]=基色;
打破
案例1:
颜色[0]=((val-基)*(60-(色调%60))/60)+基;
颜色[1]=val;
颜色[2]=基色;
打破
案例2:
颜色[0]=基色;
颜色[1]=val;
颜色[2]=((val-基)*(色调%60))/60)+基;
打破
案例3:
颜色[0]=基色;
颜色[1]=((val-基)*(60-(色调%60))/60)+基;
颜色[2]=val;
打破
案例4:
颜色[0]=((val-基)*(色调%60))/60)+基;
颜色[1]=基色;
颜色[2]=val;
打破
案例5:
颜色[0]=val;
颜色[1]=基色;
颜色[2]=((val-基)*(60-(色调%60))/60)+基;
打破
}
}
}
//具有彩虹效果的剧院式爬行灯
无效剧场彩虹(等待8分钟){
对于(int j=0;j<256;j++){//循环控制盘中的所有256种颜色
对于(int q=0;q<3;q++){
对于(int i=0;i对于(j=0;j而言,对这些电路板进行故障排除可能是非常具有挑战性的。我最初的想法是,您可能在阁楼上内存不足。您是否尝试过仅使用一个动画功能的少量代码进行测试

此外,在电气工程堆栈交换中,似乎还有更多关于此主题的活动:


对这些电路板进行故障排除可能非常具有挑战性。我最初的想法是,您可能正在运行
ifdef AVR
include <avr/power.h>
endif
 #if defined (__AVR_ATtiny45__)
    if (F_CPU == 8000000) clock_prescale_set(clock_div_1);
  #endif
// This is a demonstration on how to use an input device to trigger changes on your neo pixels.
// You should wire a momentary push button to connect from ground to a digital IO pin.  When you
// press the button it will change to a new pixel animation.  Note that you need to press the
// button once to start the first animation!

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define BUTTON_PIN   0    // Digital IO pin connected to the button.  This will be
                          // driven with a pull-up resistor so the switch should
                          // pull the pin to ground momentarily.  On a high -> low
                          // transition the button press logic will execute.

#define PIXEL_PIN    4    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 8

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // Blue
            break;
    case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
            break;
    case 5: theaterChase(strip.Color(127,   0,   0), 50); // Red
            break;
    case 6: theaterChase(strip.Color(  0,   0, 127), 50); // Blue
            break;
    case 7: rainbow(20);
            break;
    case 8: rainbowCycle(20);
            break;
    case 9: theaterChaseRainbow(50);
            break;
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  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);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  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);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// 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);
}