2018年10月8日

bpi-bit LED 數位亮燈

arduino ide 實作led亮燈

開一個arduino新檔,板子選擇bpi-bit(板子設定方法請參考https://wdpsestea.blogspot.com/2018/10/bpi-bit-arduino-ide.html ,ubuntu使用者參考https://wdpsestea.blogspot.com/2018/09/bpi-bit-arduino-ide.html ) 選擇連擁port(win下是comX,linux下是ttyUSBX),燒錄速度建議降低些。




void setup() {
  // put your setup code here, to run once:

}
前include下列檔案
我們有25個LED和使用PixelPin 4來溝通。

#include <Arduino.h>
#include <NeoPixelBus.h>
#include "WiFi.h"
//WS2812 config
const uint16_t PixelCount = 25;
const uint8_t PixelPin = 4;
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);


#define colorSaturation 32
//定義WS2812 腳位 2
#define ledPower 2
//定義顏色
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);
HslColor hslRed(red);
HslColor hslGreen(green);
HslColor hslBlue(blue);
HslColor hslWhite(white);
HslColor hslBlack(black);


void setup() {
  // put your setup code here, to run once:

}
中加入
  Serial.begin(115200); //Serial Port Config 115200-8-N-1
  while (!Serial); // wait for serial attach

  Serial.println();
  Serial.println("Initializing...");
  Serial.flush();

  // this resets all the neopixels to an off state
  strip.Begin();
  strip.Show();

  pinMode(ledPower,OUTPUT);
  digitalWrite(ledPower, HIGH); //It's very important for the v1.2, if you use the v1.4 ,you can delete it


在void loop() {
  // put your main code here, to run repeatedly:
           
}
中插入下列
            strip.SetPixelColor(0, red);
            strip.Show();
            delay(100);
            strip.SetPixelColor(0, black);
            strip.Show();
            delay(100);
亮燈,strip.SetPixelColor(0, red);0是第幾個燈按照下圖排列,顏色則按RgbColor定義的內容

 要熄滅則用黑色
strip.SetPixelColor(0, black);
間隔時間使用delay(微秒),1秒=1000微秒。
 delay(1000);


沒有留言: