后一修订版 | 前一修订版 |
zh:learing:examples:physicalpixel [2016/03/11 07:09] – 董凯萍 | learing:examples:physicalpixel [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 |
---|
| ====== 物理像素 ====== |
| |
| <WRAP left round info 65%> |
| 这个例子演示了使用arduino接收来自电脑的数据。收到 H 字符的时候点亮LED,收到 L 的时候熄灭。 |
| |
| 数据使用arduino自带的串口监视器发送,也可以使用processing(代码在下面)。 |
| </WRAP> |
| |
| |
| |
| <WRAP left round box 100%> |
| ===== ALPHA 8F328D-U核心 ===== |
| |
| ==== 硬件 ==== |
| * [[ocrobot:alpha:parallelexpansion:index|ALPHA 并行扩展板]] |
| * [[ocrobot:alpha:8f328d-u:index|ALPHA 8F328D-U]] |
| * [[ocrobot:alpha:11led:index|ALPHA 11 LED模块]] |
| |
| ==== 搭建电路 ==== |
| |
| - ALPHA 11 LED模块插入并行扩展版1号槽位。 |
| - ALPHA 8F328D-U模块插入并行扩展板2号槽位。 |
| - USB线连接计算机与ALPHA 8F328D-U。 |
| |
| ==== 代码 ==== |
| </WRAP> |
| |
| <WRAP left round box 100%> |
| ===== MangoII ===== |
| |
| ==== 硬件要求 ==== |
| |
| |Arduino| |
| |电位计| |
| |
| ==== 软件要求 ==== |
| |
| |
| Processing |
| |
| 连接LED到13号脚(或者不连接,使用arduino自带的13号引脚的LED)。 |
| |
| |
| |
| ==== Code ==== |
| |
| |
| <code cpp>/* |
| Physical Pixel |
| |
| */ |
| |
| const int ledPin = 13; // LED连接的引脚 |
| int incomingByte; // 变量 |
| |
| void setup() { |
| // 串口通信: |
| Serial.begin(9600); |
| // LED引脚输出: |
| pinMode(ledPin, OUTPUT); |
| } |
| |
| void loop() { |
| // 监听串口数据: |
| if (Serial.available() > 0) { |
| // 读串口缓冲区第一个字节 : |
| incomingByte = Serial.read(); |
| // 如果是大写字母H(ASCII码值是72), 点亮 LED: |
| if (incomingByte == 'H') { |
| digitalWrite(ledPin, HIGH); |
| } |
| //如果是 L (ASCII值是76) 熄灭 LED: |
| if (incomingByte == 'L') { |
| digitalWrite(ledPin, LOW); |
| } |
| } |
| }</code> |
| |
| |
| Processing代码 |
| |
| <code cpp> // mouseover serial |
| |
| |
| |
| import processing.serial.*; |
| |
| float boxX; |
| float boxY; |
| int boxSize = 20; |
| boolean mouseOverBox = false; |
| |
| Serial port; |
| |
| void setup() { |
| size(200, 200); |
| boxX = width/2.0; |
| boxY = height/2.0; |
| rectMode(RADIUS); |
| |
| //列出可用的串口 |
| // 选择arduino连接的串口. 列表第一个是port #0 |
| println(Serial.list()); |
| |
| // 打开串口 |
| // 波特率9600bps |
| port = new Serial(this, Serial.list()[0], 9600); |
| |
| } |
| |
| void draw() |
| { |
| background(0); |
| |
| // 检测鼠标是否在方块外面 |
| if (mouseX > boxX-boxSize && mouseX < boxX+boxSize && |
| mouseY > boxY-boxSize && mouseY < boxY+boxSize) { |
| mouseOverBox = true; |
| // 在方块周围画线,并且涂不同的颜色: |
| stroke(255); |
| fill(153); |
| // 发送H表示鼠标移出方块了: |
| port.write('H'); |
| } |
| else { |
| // return the box to it's inactive state: |
| stroke(153); |
| fill(153); |
| // 发送'L'来熄灭LED: |
| port.write('L'); |
| mouseOverBox = false; |
| } |
| |
| //画盒方块 |
| rect(boxX, boxY, boxSize, boxSize); |
| } |
| |
| </code> |
| |
| |
| |
| 下载好程序之后,移动鼠标到方块里,看LED亮灭变化 |
| </WRAP> |
| 。 |