• LED燈(LED):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #define LED_PIN 13

    void setup() {
    pinMode(LED_PIN, OUTPUT);
    }

    void loop() {
    digitalWrite(LED_PIN, HIGH); // 點亮LED燈
    delay(1000);
    digitalWrite(LED_PIN, LOW); // 關閉LED燈
    delay(1000);
    }

  • 光敏電阻(Photoresistor):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #define PHOTO_PIN A0

    void setup() {
    // 初始化其他設置
    }

    void loop() {
    int sensorValue = analogRead(PHOTO_PIN);
    // 使用讀取到的光敏電阻數值進行其他操作
    }

  • 蜂鳴器(Buzzer):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #define BUZZER_PIN 9

    void setup() {
    // 初始化其他設置
    }

    void loop() {
    tone(BUZZER_PIN, 1000); // 發出1000Hz的聲音
    delay(1000);
    noTone(BUZZER_PIN); // 停止聲音
    delay(1000);
    }

  • 土壤感測器(Soil Moisture Sensor):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #define SOIL_PIN A0

    void setup() {
    // 初始化其他設置
    }

    void loop() {
    int sensorValue = analogRead(SOIL_PIN);
    // 使用讀取到的土壤濕度數值進行其他操作
    }

  • 溫溼度感測器(DHT11):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include <DHT.h>

    #define DHT_PIN 2
    #define DHT_TYPE DHT11

    DHT dht(DHT_PIN, DHT_TYPE);

    void setup() {
    dht.begin();
    }

    void loop() {
    float temperature = dht.readTemperature(); // 讀取溫度
    float humidity = dht.readHumidity(); // 讀取濕度
    // 使用讀取到的溫溼度數值進行其他操作
    }

  • 超音波傳感器(Ultrasonic Sensor):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #define TRIGGER_PIN 12
    #define ECHO_PIN 11

    void setup() {
    // 初始化其他設置
    }

    void loop() {
    long duration, distance;

    digitalWrite(TRIGGER_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGGER_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIGGER_PIN, LOW);

    duration = pulseIn(ECHO_PIN, HIGH);
    distance = duration * 0.034 / 2;

    // 使用讀取到的距離進行其他操作
    }

  • I2C LCD:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>

    LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C地址可以根據具體的LCD模組進行調整

    void setup() {
    lcd.begin(16, 2); // 初始化LCD,指定列數和行數
    lcd.backlight(); // 打開背光

    // 顯示初始訊息
    lcd.setCursor(0, 0);
    lcd.print("Hello,");
    lcd.setCursor(0, 1);
    lcd.print(" Yun! ");
    }

    void loop() {
    // 這裡可以放置其他程式碼或更新LCD顯示內容
    }


  • 按鈕(Button):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #define BUTTON_PIN 2

    void setup() {
    pinMode(BUTTON_PIN, INPUT);
    }

    void loop() {
    if (digitalRead(BUTTON_PIN) == HIGH) {
    // 按鈕被按下時執行的程式碼
    }
    }

  • WIFI(ESP32):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include <WiFi.h>

    const char* ssid = "YourNetworkSSID";
    const char* password = "YourNetworkPassword";

    void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
    }

    Serial.println("Connected to WiFi!");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    }

    void loop() {
    // 其他操作
    }

  • 太陽能機械手臂(大略)

    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
    #include <Servo.h>

    Servo baseServo;
    Servo armServo;
    Servo gripperServo;

    int basePin = 9;
    int armPin = 10;
    int gripperPin = 11;

    void setup() {
    baseServo.attach(basePin);
    armServo.attach(armPin);
    gripperServo.attach(gripperPin);
    }

    void loop() {
    // 控制基座旋轉
    baseServo.write(90); // 90度表示中心位置
    delay(2000);

    // 控制手臂上下移動
    armServo.write(45); // 45度表示上升位置
    delay(2000);

    // 控制手爪開合
    gripperServo.write(180); // 180度表示完全開啟
    delay(2000);

    // 將手爪關閉
    gripperServo.write(0); // 0度表示完全關閉
    delay(2000);
    }

  • 藍芽連機械手臂(大略)

    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
    #include <Servo.h>
    #include <SoftwareSerial.h>

    SoftwareSerial bluetooth(10, 11); // 設定藍芽模組的RX和TX腳位
    Servo baseServo;
    Servo armServo;
    Servo gripperServo;

    int basePin = 9;
    int armPin = 10;
    int gripperPin = 11;

    void setup() {
    baseServo.attach(basePin);
    armServo.attach(armPin);
    gripperServo.attach(gripperPin);

    bluetooth.begin(9600); // 初始化藍芽連接

    // 等待藍芽連接成功
    while (!bluetooth) {
    ; // 空迴圈等待連接
    }
    }

    void loop() {
    if (bluetooth.available()) {
    char command = bluetooth.read();

    // 根據接收到的指令控制機械手臂
    if (command == 'A') {
    baseServo.write(90); // 控制基座旋轉到90度位置
    } else if (command == 'B') {
    armServo.write(45); // 控制手臂上升到45度位置
    } else if (command == 'C') {
    gripperServo.write(180); // 控制手爪開啟
    } else if (command == 'D') {
    gripperServo.write(0); // 控制手爪關閉
    }
    }
    }

  • 太陽能滑軌(大略)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include <Servo.h>

    Servo motor;

    int motorPin = 9;

    void setup() {
    motor.attach(motorPin);
    }

    void loop() {
    // 控制滑軌移動到起始位置
    motor.write(0); // 0度表示起始位置
    delay(2000);

    // 控制滑軌移動到終點位置
    motor.write(180); // 180度表示終點位置
    delay(2000);
    }

  • ESP32

    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
    #include <WiFi.h>

    const char* ssid = "your_SSID";
    const char* password = "your_PASSWORD";

    void setup() {
    Serial.begin(115200);
    delay(2000);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi...");
    }

    Serial.println("WiFi connected!");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    }

    void loop() {
    // 主迴圈內的程式碼
    }

  • ESP32-CAM

    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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    #include "esp_camera.h"
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <ESPAsyncWebServer.h>

    // 定義相機引腳
    #define CAMERA_MODEL_AI_THINKER
    #define PWDN_GPIO_NUM 32
    #define RESET_GPIO_NUM -1
    #define XCLK_GPIO_NUM 0
    #define SIOD_GPIO_NUM 26
    #define SIOC_GPIO_NUM 27
    #define Y9_GPIO_NUM 35
    #define Y8_GPIO_NUM 34
    #define Y7_GPIO_NUM 39
    #define Y6_GPIO_NUM 36
    #define Y5_GPIO_NUM 21
    #define Y4_GPIO_NUM 19
    #define Y3_GPIO_NUM 18
    #define Y2_GPIO_NUM 5
    #define VSYNC_GPIO_NUM 25
    #define HREF_GPIO_NUM 23
    #define PCLK_GPIO_NUM 22

    // 設定網路連接資訊
    const char* ssid = "YourNetworkSSID";
    const char* password = "YourNetworkPassword";

    // 建立伺服器
    AsyncWebServer server(80);

    void setup() {
    // 初始化串流伺服器
    camera_config_t config;
    config.ledc_channel = LEDC_CHANNEL_0;
    config.ledc_timer = LEDC_TIMER_0;
    config.pin_d0 = Y2_GPIO_NUM;
    config.pin_d1 = Y3_GPIO_NUM;
    config.pin_d2 = Y4_GPIO_NUM;
    config.pin_d3 = Y5_GPIO_NUM;
    config.pin_d4 = Y6_GPIO_NUM;
    config.pin_d5 = Y7_GPIO_NUM;
    config.pin_d6 = Y8_GPIO_NUM;
    config.pin_d7 = Y9_GPIO_NUM;
    config.pin_xclk = XCLK_GPIO_NUM;
    config.pin_pclk = PCLK_GPIO_NUM;
    config.pin_vsync = VSYNC_GPIO_NUM;
    config.pin_href = HREF_GPIO_NUM;
    config.pin_sscb_sda = SIOD_GPIO_NUM;
    config.pin_sscb_scl = SIOC_GPIO_NUM;
    config.pin_pwdn = PWDN_GPIO_NUM;
    config.pin_reset = RESET_GPIO_NUM;
    config.xclk_freq_hz = 20000000;
    config.pixel_format = PIXFORMAT_JPEG;
    if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
    } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
    }

    // 初始化相機
    esp_err_t err = esp_camera_init(&config);
    if (err != ESP_OK) {
    Serial.printf("相機初始化失敗:%s\n", esp_err_to_name(err));
    return;
    }

    // 連接Wi-Fi網路
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("正在連接到Wi-Fi網路...");
    }
    Serial.println("Wi-Fi連接成功!");

    // 設定伺服器路由
    server.on("/", handleRoot);
    server.on("/stream", handleStream);
    server.begin();
    Serial.println("伺服器已啟動!");
    }

    void loop() {
    // 空迴圈
    }

    void handleRoot(AsyncWebServerRequest *request) {
    request->send(200, "text/plain", "ESP32-CAM 影像串流");
    }

    void handleStream(AsyncWebServerRequest *request) {
    // 設定MIME類型為MJPEG
    request->send_P(200, "multipart/x-mixed-replace; boundary=esp32cam",
    [&](const String &contentType) {
    camera_fb_t *fb = NULL;
    if (contentType.startsWith("multipart/x-mixed-replace")) {
    while (true) {
    fb = esp_camera_fb_get();
    if (!fb) {
    Serial.println("無法取得相機影像");
    break;
    }
    // 發送HTTP標頭
    request->sendContent("--esp32cam\r\n");
    request->sendContent("Content-Type: image/jpeg\r\n");
    request->sendContent("Content-Length: " + String(fb->len) + "\r\n");
    request->sendContent("\r\n");
    // 發送影像數據
    request->sendContent((const char *)fb->buf, fb->len);
    request->sendContent("\r\n");
    esp_camera_fb_return(fb);
    delay(10);
    }
    }
    });
    }