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
|
// OLED 屏幕分辨率
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// RGB 引脚 const int redPin = 9; const int greenPin = 10; const int bluePin = 11;
void setup() { // 初始化 RGB 引脚 pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);
// 初始化 OLED if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C 是常见地址 for (;;); // 如果初始化失败就卡住 }
display.clearDisplay(); display.setTextSize(2); // 字体大小 display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.println("RGB TEST"); display.display(); delay(1000); }
void loop() { // 红色 setColor(HIGH, LOW, LOW); showText("RED"); delay(1000);
// 绿色 setColor(LOW, HIGH, LOW); showText("GREEN"); delay(1000);
// 蓝色 setColor(LOW, LOW, HIGH); showText("BLUE"); delay(1000); }
// 控制 RGB 灯亮灭 void setColor(int r, int g, int b) { digitalWrite(redPin, r); digitalWrite(greenPin, g); digitalWrite(bluePin, b); }
// OLED 显示文字 void showText(const char *text) { display.clearDisplay(); display.setCursor(0, 20); display.println(text); display.display(); }
|