Arduino程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <Arduino.h> #include <Adafruit_NeoPixel.h> // 依照你原本的腳位 #define EYES_CTRL_PIN 6 #define TOUCH_SW_PIN 4 #define EYES_NUM 2 Adafruit_NeoPixel strip(EYES_NUM, EYES_CTRL_PIN, NEO_GRB + NEO_KHZ800); // 顏色清單(紅、綠、藍、黃、紫、白) uint8_t colors[][3] = { {255, 0, 0 }, // 紅 {0, 255, 0 }, // 綠 {0, 0, 255}, // 藍 {255, 255, 0 }, // 黃 {255, 0, 255}, // 紫 {255, 255, 255} // 白 }; int colorIndex = 0; void setEyes(uint8_t r, uint8_t g, uint8_t b, uint8_t brightness) { for (int i = 0; i < EYES_NUM; i++) { strip.setPixelColor(i, strip.Color(r, g, b)); } strip.setBrightness(brightness); // 0~255 strip.show(); } void setup() { Serial.begin(115200); // 眼睛燈初始化 strip.begin(); strip.show(); // 先清空 // 觸碰按鍵(你原本用的是 INPUT_PULLDOWN) pinMode(TOUCH_SW_PIN, INPUT_PULLDOWN); // 先亮白色當作「開機成功」 setEyes(255, 255, 255, 80); } void loop() { // 讀按鍵:按下是 HIGH if (digitalRead(TOUCH_SW_PIN) == HIGH) { delay(30); // 防彈跳(避免按一下變很多次) // 等到放開(只算一次) while (digitalRead(TOUCH_SW_PIN) == HIGH) { delay(10); } // 換到下一個顏色 colorIndex++; if (colorIndex >= (int)(sizeof(colors) / sizeof(colors[0]))) { colorIndex = 0; } // 顯示顏色(亮度用 80,不會刺眼) setEyes(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2], 80); // 印出目前顏色編號(給老師看) Serial.print("Color index = "); Serial.println(colorIndex); } delay(10); } |
執行結果:
Color index = 1
Color index = 2
沒有留言:
張貼留言