PIXHAWK DIY LED扩展板

板载的状态LED灯,因为各种灰机的外壳有可能会被挡住看不到状态。那么我们也是可以用arduino板子来扩展实现外置,其实就是用328P芯片来实现。

这程序支持WS2812B的全彩LED灯。

默认的信号控制引脚是A1(15),LED数量是8个,你可以根据你的实际情况修改。

 

带FastLed类库的包下载 

代码使用了FastLed的第三方类库可以自己下载

代码如下:

#include <Wire.h>
#include "FastLED.h"

#define NUM_LEDS 8

// Data pin that led data will be written out over
#define DATA_PIN 15
// Clock pin only needed for SPI based chipsets when not using hardware SPI
//#define CLOCK_PIN 8

long lasttimeLED = 0;
#define delayLED 10 //ms
long lasttimeCYC = 0;
#define delayCYC 50 //ms

CRGB leds[NUM_LEDS];
bool thergbled[3] = {0, 0, 0};
bool changeflag = false;
bool i2cflag = false;
#define downrate 1

void setup() {
  Wire.begin(0x55);                // join i2c bus with address
  Wire.onReceive(receiveEvent); // register event
//  Serial.begin(9600);           // start serial for output
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);  // init LED
}

void loop() {

  LED();
  CYC();

}

void LED() {
  if ((millis() - lasttimeLED) > delayLED) {
    lasttimeLED = millis();
//    for (int i = 0; i < 8; i++) {
//      leds[i] = CRGB(255 * thergbled[2], 255 * thergbled[1], 255 * thergbled[0]);
//    }
     if(i2cflag) {
      leds[0] = CRGB(255 * thergbled[2], 255 * thergbled[1], 255 * thergbled[0]);
      FastLED.show();
      i2cflag = false;
      changeflag = false; // skip CYC change since already update in this loop
    }
    else if(changeflag) {
      FastLED.show();
      changeflag = false;
    }
  }
}

void CYC() {
  if ((millis() - lasttimeCYC) > delayCYC) {
    lasttimeCYC = millis();
    for(int i=(NUM_LEDS-1);i>=1;i--) {
      leds[i] = CRGB(leds[i-1].r *downrate , leds[i-1].g *downrate, leds[i-1].b *downrate );
//        leds[i+1] = leds[i];
    }
    changeflag = true;
  }
}


// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {

  while ( Wire.available() >= 2 ) { // loop through all but the last
    int ledreg = Wire.read(); // receive byte as a regid
    int ledval = Wire.read(); // receive byte as a ledcolorsonoff
//    Serial.print(ledreg, HEX);        // print the character
//    Serial.print(" : ");         // print the character
//    Serial.println(ledval, HEX);        // print the character
    switch (ledreg) {
      case 0x81:
        thergbled[0] = ledval; // B
        break;
      case 0x82:
        thergbled[1] = ledval; // G
        break;
      case 0x83:
        thergbled[2] = ledval; // R
        i2cflag = true;
        break;
      default:
        break;
    }
  }
}

 

转载于:https://www.cnblogs.com/Tranquilty/p/6228053.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值