//WS2812B RGB LED 16X16 五子棋棋盤
#define FASTLED_RMT5_RECYCLE 1
#include <FastLED.h> //For FastLED
const uint8_t kMatrixWidth = 16; //棋盤矩陣寬度
const uint8_t kMatrixHeight = 16; //棋盤矩陣高度
const bool kMatrixSerpentineLayout = true;
const bool kMatrixVertical = false;
//定義FAST LED========================================================
#define LED_PIN 15 //RGB_LED接腳
#define NUM_LEDS (kMatrixWidth * kMatrixHeight) //棋盤大小
#define CHIPSET WS2812B //LED strip type
#define COLOR_ORDER GRB //常用GRB,RGB可依個人配置顏色順序,
#define LED_VOLTS 5 //使用電壓
#define MAX_AMPS 5000 //限制最大電流
#define SERPENTINE true //預設蛇形(serpentine)排列
CRGB leds[NUM_LEDS]; //初始化設定
//顏色定義==================================================-
#define Red1 CRGB(255,0,0)
#define Green1 CRGB(0,255,0)
#define Blue1 CRGB(0,0,255)
#define Black1 CRGB(0,0,0)
uint8_t Red= 100;//RGB顏色分量--
uint8_t Blue=50;
uint8_t Green=50;
uint8_t BRIGHTNESS =250; //預設RGB亮度
uint8_t M=15,N=15; //棋盤大小
uint8_t arr[15][15]={0}; //儲存下棋陣列
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS)
.setCorrection(TypicalSMD5050);
FastLED.setMaxPowerInVoltsAndMilliamps(LED_VOLTS, MAX_AMPS);
FastLED.setBrightness(BRIGHTNESS);// 設置全局亮度
FastLED.clear();
FastLED.show();
}
void loop() {
demo(); //執行demo程式
delay(2000);
}
//======================================================
void demo(){
leds[ XY( 7, 7) ] = CRGB(0, 0, 70); //在指定座標下棋,藍色棋子
FastLED.show(); //輸出
delay(1000); //延時1秒
leds[ XY( 7, 6) ] = CRGB(70, 0, 0);
FastLED.show();
delay(1000);
leds[ XY( 6, 6) ] = CRGB(0, 0, 70);
FastLED.show();
delay(1000);
leds[ XY( 6, 7) ] = CRGB(70, 0, 0);
FastLED.show();
delay(1000);
leds[ XY( 8, 8) ] = CRGB(0, 0, 70);
FastLED.show();
delay(1000);
leds[ XY( 8, 5) ] = CRGB(70, 0, 0);
FastLED.show();
delay(1000);
leds[ XY( 9, 4) ] = CRGB(0, 0, 70);
FastLED.show();
delay(1000);
leds[ XY( 5, 8) ] = CRGB(70, 0, 0);
FastLED.show();
delay(1000);
leds[ XY( 4, 9) ] = CRGB(0, 0, 70);
FastLED.show();
delay(1000);
leds[ XY( 8, 6) ] = CRGB(70, 0, 0);
FastLED.show();
delay(1000);
leds[ XY( 9, 9) ] = CRGB(0, 0, 70);
FastLED.show();
delay(1000);
leds[ XY( 10, 10) ] = CRGB(70, 0, 0);
FastLED.show();
delay(1000);
leds[ XY( 5, 5) ] = CRGB(0, 0, 70);
FastLED.show();
delay(1000);
for(uint8_t i=0;i<=15;i++){
leds[ XY( 5, 5) ] = CRGB(0, 0, 0);
leds[ XY( 6, 6) ] = CRGB(0, 0, 0);
leds[ XY( 7, 7) ] = CRGB(0, 0, 0);
leds[ XY( 8, 8) ] = CRGB(0, 0, 0);
leds[ XY( 9, 9) ] = CRGB(0, 0, 0);
FastLED.show();
delay(200);
leds[ XY( 5, 5) ] = CRGB(0, 0, 70);
leds[ XY( 6, 6) ] = CRGB(0, 0, 70);
leds[ XY( 7, 7) ] = CRGB(0, 0, 70);
leds[ XY( 8, 8) ] = CRGB(0, 0, 70);
leds[ XY( 9, 9) ] = CRGB(0, 0, 70);
FastLED.show();
delay(200);
}
FastLED.clear ();
FastLED.show();
}
//==================================================================
// 適用於MxN=16x16 LED矩陣,將座標 (列, 行) 轉換為對應的 LED 索引
int16_t XY( uint8_t y, uint8_t x)
{
uint16_t i; // 定義變數 i,儲存計算出的 LED 索引
uint8_t a = M - x; // 計算變數 a=是矩陣寬度-對應座標中的 x
if( kMatrixSerpentineLayout == false) { // 如果矩陣不是蛇形排列
if (kMatrixVertical == false) { // 如果矩陣排列是水平的
i = (y * kMatrixWidth) + a; // 列優先排列,索引=(行*矩陣寬度) + a
} else { // 如果矩陣排列是垂直的
i = kMatrixHeight * (kMatrixWidth - (a+1)) + y; // 索引 = 垂直翻轉公式
}
}
if( kMatrixSerpentineLayout == true) { // 如果矩陣是蛇形排列
if (kMatrixVertical == false) { // 如果矩陣排列是水平的
if( y & 0x01) { // 如果行號是奇數
uint8_t reverseX = (kMatrixWidth - 1) - a; // 奇數行:x 軸翻轉
i = (y * kMatrixWidth) + reverseX;// 計算奇數行索引
} else {
i = (y * kMatrixWidth) + a; // 偶數行:正常計算索引
}
} else { // 如果矩陣排列是垂直的
if ( x & 0x01) { // 如果列號是奇數(x 的最低位是否為 1)
i = kMatrixHeight * (kMatrixWidth - (a+1)) + y; // 垂直奇數列索引計算
} else {
i = kMatrixHeight * (kMatrixWidth - a) - (y+1); // 垂直偶數列索引計算
}
}
}
return i; // 返回計算出的 LED索引
}