STEM|如何活用arduino nano 及I2C OLED 製作一個簡單的蘋果被吃了一口的動畫
使用以下步驟來製作一個簡單的蘋果被吃了一口的動畫:
連接硬體:
將您的 Arduino Nano 連接到 SSD1306 0.96 吋 I2C OLED 顯示器。您可以在網上的資源來了解如何連接它們。
安裝所需的庫:
在 Arduino IDE 中,安裝 adafruit_SSD1306.h 和 adafruit_GFX.h 這兩個庫。您可以在 Library Manager 中搜索並安裝它們。
編寫程式碼:
使用 display.drawPixel(x, y, color) 函數繪製蘋果的形狀。您可以使用 display.fillRect(x, y, width, height, color) 函數來填充蘋果的一部分,模擬被吃掉的效果。
使用 display.display() 函數來更新 OLED 顯示器。
以下是一個簡單的範例程式碼,您可以根據您的需求進行修改:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000);
display.clearDisplay();
}
void loop() {
// Draw an apple shape
display.drawPixel(40, 30, WHITE);
display.drawPixel(41, 30, WHITE);
display.drawPixel(42, 30, WHITE);
// ... (add more pixels to complete the apple shape)
// Simulate taking a bite
display.fillRect(43, 30, 10, 5, BLACK);
display.display();
delay(2000);
}
請注意,您需要根據您的 OLED 顯示器的尺寸和位置調整座標和形狀。這只是一個簡單的範例,您可以根據自己的需求進行修改和擴展。祝您成功製作出有趣的動畫
Comments