cavedu
  • CAVEDU教育團隊
  • BOSON電子積木套件
    • 認識BOSON
      • 輸入模組
        • i1旋鈕模組
        • i2r按鈕模組
        • i3自鎖開關
        • i4光感測器
        • i5傾斜感測器
        • i6水分感測器
        • i7火焰感測器
        • i8觸碰感測器
        • i9聲音感測器
        • i10紅外線接近度感測器
        • i11溫度感測器
        • i12導電感測開關
        • i13動作感測器
        • i16土壤濕度感測器
        • i17酸鹼值感測器
        • i18濕度感測器
        • i19防水溫度感測器
        • i20心律監測感測器
        • i22超音波感測器
      • 輸出模組
        • o2LED燈模組
        • o5蜂鳴器模組
        • o6風扇模組
        • o7錄音機模組/喇叭
        • o9馬達模組
        • o10伺服機模組
        • o11OLED顯示模組
        • 七彩燈條
      • 功能模組
        • f1運算邏輯模組AND
        • f2運算邏輯模組OR
        • f3運算邏輯模組NOT
        • f4分配模組
        • f6閾模組
        • f7計數模組
        • f8計時模組(0-60秒)
    • 實作範例教學
    • 資源鏈結
  • LinkIt 7697 BlocklyDuino 使用指南
  • LinkIt 7697 - Arduino IDE 開發指南
  • 💻Raspberry Pi Pico介紹
    • Raspberry Pi Pico 擴充板
    • Raspberry Pi Pico 使用Arduino IDE
  • 洞洞么教學材料包
    • 新版LinkIt 7697開發板 V1.1版使用序列埠印出指令亂碼問題
    • 模組使用教學
      • LED模組
      • LED模組(三色)
      • 按鈕模組
      • 旋轉電位計模組
      • 光敏模組
      • 微動開關模組
      • DHT11溫溼度感測器模組
      • 無源蜂鳴器模組
      • 有源蜂鳴器模組
      • 震動開關模組
      • 滾珠開關模組
      • 超音波測距感測器
      • 伺服機
      • 紅外線偵測模組
      • I2C 1602液晶顯示模組
      • 熱敏(溫度)模組
      • DHT 22溫溼度模組(For Pico)
    • micro:bit影片教學
  • 洞洞兩教學材料包(貓咪盃硬體組材料包)
    • 模組使用教學
      • LED模組
      • 繼電器
      • 大顆LED紅綠燈整合模組
      • 旋轉可變電阻
      • 光線模組
      • 土壤溼度模組
      • 避障紅外線模組
      • 按鈕模組
      • 滾珠開關模組
      • 霍爾磁性模組
      • RGB LED共陰模組
      • 電晶體MOS模組
      • 兩路L9110S馬達模組
      • 無源蜂鳴器
      • SR-04P超音波
      • DHT11溫溼度模組
      • 舵機
      • LCD1602液晶模組
      • Max7219 8X8 矩陣LED模組
      • WS2812 RGB12燈燈環模組
  • 洞洞伍AIoT教學材料包
    • Raspberry Pi Pico W 介紹
    • 程式撰寫軟體下載
    • 模組使用教學
      • 溫溼度感測器
      • 旋轉電位計模組
      • RFID模組
      • RGB全彩LED模組(共陽極)
      • 按鈕模組
      • OLED 顯示器模組
      • 有源蜂鳴器模組
      • SG90伺服馬達模組
      • PIR生物紅外線感測器模組
      • 氣體感測器Gas Sensor(MQ7)模組
      • 數位式光強度感測器模組
  • Arduino首次接觸就上手
    • 認識Hangeekduino圖形化介面
    • Hangeekduino範例程式
      • 輸入模組
        • 按鈕模組
        • 旋轉電位計模組
        • 聲音感測模組
        • 光感測器模組
        • 溫溼度感測模組
        • 三軸加速度模組
        • 氣壓感測模組
      • 輸出模組
        • LED模組
        • 蜂鳴器模組
        • OLED顯示模組
    • 書籍內容更新_202012更新
    • [Processing 互動裝置藝術結合 Arduino ]
      • 範例01_Processing IDE 下載、基本範例
      • 範例02_連接Arduino_數位輸入與類比輸入
      • 範例03_Processing控制LED
      • 範例04_療癒海浪 - 按鈕與多個類比感測器
      • 範例05_天空飄著雲 - DHT溫溼度感測器
      • 6. 滾球小遊戲 - 三軸加速度感測器
      • 範例07_風車轉轉 - 聲音感測器
    • Arduino BLE藍牙通訊 - Android 手機
      • Example 01
      • Example 02
      • Example 03
  • 其他感測器模組
    • 火焰感測器
    • 手指偵測心跳模組
    • 心律感測器
    • 科易KEYES 雨水感測器
    • Grove–VOC與eCO2 氣體感測器 (SGP30)
    • 高精度升降壓電源模組XYS3580
  • MCS Lite 介紹及感測器網頁遠端監控
    • 5-4 MCS Lite 介紹及MCS Lite 環境建置
    • 5-5 網頁遠端監控LED
    • 5-6 雲端溫濕度監控
  • 初學Jetson Nano不說No-CAVEDU教你一次懂
    • 書籍更新修改內容
Powered by GitBook
On this page
  • 【教學】Arduino首次接觸就上手_ep3 Processing控制Arduino LED
  • Processing 程式範例 - 點選滑鼠控制 LED 亮滅
  • Processing 程式範例 - 根據滑鼠 X 座標控制 LED 漸明漸暗

Was this helpful?

  1. Arduino首次接觸就上手
  2. [Processing 互動裝置藝術結合 Arduino ]

範例03_Processing控制LED

Previous範例02_連接Arduino_數位輸入與類比輸入Next範例04_療癒海浪 - 按鈕與多個類比感測器

Last updated 3 years ago

Was this helpful?

【教學】Arduino首次接觸就上手_ep3 Processing控制Arduino LED

本篇要透過兩個小範例來說明如何透過 Processing 來控制 Arduino 的狀態,分別為數位輸出與類比輸出。前者使用 Processing 的arduino.digitalWrite() 語法來控制 LED 的亮、暗兩種狀態。

後者則使用 Processing 的arduino.analogWrite() 語法來控制 LED 的亮、暗兩種狀態。請注意後者在 Arduino 上須把 LED 接在支援 PWM 的腳位(以 Arduino 首次接觸就上手套件的 Arudino Uno 來說,支援 PWM 的腳位為 3 5 6 9 10 與11 等六隻腳位,所以無法使用 D4 LED。

Processing 程式範例 - 點選滑鼠控制 LED 亮滅

執行畫面如下:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

color off = color(4, 79, 111);
color on = color(84, 145, 158);

void setup() {
  size(1200, 400);

  // Prints out the available serial ports.
  println(Arduino.list());

  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(4, Arduino.OUTPUT);
  arduino.digitalWrite(4, Arduino.LOW);
}

void draw() {

  stroke(on);

  if (mouseX > (width/2)) {
    arduino.digitalWrite(4, Arduino.HIGH); //LED亮起
    background(on);
    println("LED on");
  } else {
    arduino.digitalWrite(4, Arduino.LOW); //LED熄滅
    background(off);
    println("LED off");
  }
}

Processing 程式範例 - 根據滑鼠 X 座標控制 LED 漸明漸暗

請在 Arduino 的 D9 腳位上接上 LED,因為預設的 D4 LED 該腳位不支援 PWM。執行畫面如下:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

void setup() {
  size(1200, 400);

  // Prints out the available serial ports.
  println(Arduino.list());

  arduino = new Arduino(this, Arduino.list()[0], 57600);
  arduino.pinMode(11, Arduino.OUTPUT);
}

void draw() {
  float pwm = map(mouseX, 0, 1200, 0, 255);
  arduino.analogWrite(11, int(pwm));  //本語法可接受的數值範圍為 0-255,2的8次方
  background(0, 0, pwm);
  println(int(pwm));
  delay(10);
}

新增一個 Processing 專案,並輸入以下程式碼,確認 Arduino 首次接觸就上手套件已正確燒錄 。 輸入完成直接執行以下 pde,會看到以下執行畫面:

檔名 -

未按下滑鼠鍵,畫面顏色不變,LED 熄滅

按下滑鼠鍵,畫面顏色改變,LED 亮起

左右移動滑鼠,可看到畫面的藍色濃度變化,LED也會根據滑鼠由左到右慢慢變亮。反方向移動則漸漸變暗

檔名 -

StandardFirmata 程式
arduino_output_easy.pde
arduino_output_pwm_easy.pde