int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600); // 啟動通訊 (你須開啟 Serial Monitor 序列埠監視器或稱串口監視器)
Serial.println(" I am 你的學號 你的英文姓名(自己拼音)"); // 我是誰?
pinMode(ledPin, OUTPUT);
} // setup(
void myPrint(int ggyy) { // 把 ggyy(即sensorValue) 連同一些信息印到串口監視器
Serial.print( ggyy ); // 印到串口監視器
Serial.print(String(" at ") + millis( ) ); // 印出當時開機後的 milli second
Serial.println(" I am 你的學號 你的英文姓名(自己拼音)");
} // myPrint(
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin); // 讀取 sensorPin 類比腳的值 0 ~ 1023
myPrint( sensorValue ); // 印到串口監視器
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for milliseconds:
delay(sensorValue); // 關燈後也延遲 (Delay) 一下下
}
(c)請參考上面程式碼把紅色的那幾列加入你的程式碼; 重新編譯上傳測試;
**其實開新檔(File > New) 然後複製我這版本貼入修改即可 :-)
記得開啟 序列埠監控視窗(以後我們跟著老共稱"串口監視器"):
Tool 工具 > Serial Monitor 序列埠監控視窗(串口監視器)
(此時會彈出一個小視窗, 用來監控從 Arduino 傳入電腦的資料,
其實, 必要時也可用鍵盤打字到在該小窗並傳送給連接在 USB 線的 Arduino 喔 !)
把Serial Monitor窗拉長拉大, 類似下圖的模樣,
且與 IDE 窗 拉開 適當擺放螢幕中間兩側, 準備抓畫面或拍照繳交囉。
(d)轉動可變電阻觀察, 必要時找同學幫忙轉,
確定 Serial Monitor 有變化, 你自己用 CTRL_PrintScr 抓下今天要繳交的第一張圖片 !
注意這張圖片上方要寫(1)我是..你學號你姓名, 圖片中可看出..(自己寫出狀況!)
(2)修改程式碼, PC 端改用 processing 接收資訊做事, 並抓畫面 !?
其實是更換用另一個範例啦 !
修改過的程式碼如下, 包括 Arduino 這邊的, 以及 PC 端的 processing 程式碼!
你的學號與姓名改為在 PC 端的 processing 程式內秀到電腦螢幕上 !
注意, Arduino 的 Serial Monitor 一定要關閉以免搶走 processing "接收資訊" 的權力 !
還有, 請確認 processing 程式碼內的 COM port 是否正確!
/// /// ///
//Arduino 程式
//讀取接在 Analog pin 0 (A0)上的可變電阻,並將讀值列印到 Serial Port :
int potPin = 0; // 就是 A0, 可變電阻接 analog pin 0 (注意此時 0 就是 A0 喔)
int ledPin = 9; // 把三色 LED 的紅色 R 連接到 Arduino D9 數位腳 9
long delayTime = 100; // 0.1 秒
void setup( ) {
// 開啟 Serial port, 通訊速率為 9600 bps
Serial.begin(9600);
}
void loop( ) {
int sensorValue = analogRead(potPin); // 讀取可變電阻
int gg = map(sensorValue, 0, 1023, 0, 255);
// 相當於把讀值除以 4, 把 0-1023 的數值按比例縮放為 0-255 之間的數值
// 就是 int gg = sensorValue/4;
analogWrite(ledPin, gg); // 送去給 LED
// 以下將讀值列印到 Serial Port, 以一個位元組(Byte)的格式寫出
Serial.write(gg); // 輸出一個位元組( one Byte )到 PC 端
delay(delayTime);
} // loop(
//////// 以下是 processing
//////////////////////////////////////
///
/// Processing 程式, 讀取從 Serial Port 傳進來的 Sensor 讀值,
///// 然後利用 Sensor 的讀取值控制矩形的水平移動:
/* Processing 範例:
* 讀取從 Serial Port 傳進來的 Sensor 讀值
* 利用 Sensor 讀值移動矩形 (Rectangle)
*/
import processing.serial.*;
Serial ardu; // 定義串口為 ardu
int sensorValue;
int xx, yy = 66;
int rectSize = 50; // 方形大小
String ggyy = "我是008007 張大頭"; // 你的學號與中文姓名
String myArduino = "COM4"; // 請確認你的Arduino 是否接在 COM port #
PFont f; // 字型
void drawMyName( ) {
fill( color(255, 33, 255) ); // 紫光
text(ggyy, width/2-56, 32); // 你可以改印出的位置
fill( color(22, 255, 255) ); // 民進黨+國民黨
text("哈哈哈", 58, height-28); // 左下角
} // drawMyName(
void showValue(int gg) {
println("" + gg + " by " + ggyy);
// 在座標 (xx, yy) 的位置畫一個 50 x 50 的矩形
background(222); // 白色帶點灰背景
fill(255,0,0); // 填滿顏色為紅色
rect(xx, yy, rectSize, rectSize);
fill(22, 22, 255); // 藍色
text(" " + xx, 58, 32); // 左上角
drawMyName( );
} // showValue(
void setup( ) {
// 設定畫布大小為 256+50 x 168
size(256+rectSize, 168);
// 開啟 Serial port,前面命名ardu, 通訊速率為 9600 bps
ardu = new Serial(this, myArduino, 9600);
f = createFont("SimSun", 24, true); // 仿宋體
textFont(f, 24); // 你可改字型大小
println(ggyy);
} // setup(
void draw( ) {
if ( ardu.available( ) > 0) {
// 讀取從 Serial Port 傳進來的 Sensor 值
sensorValue = ardu.read();
xx = sensorValue;
showValue(sensorValue);
}
} // draw(
//////////////////////
請至少拍下兩張照片或圖片,
一張是讀取的可變電阻值在 15到30之間, 另一張是在大於225之時!
畫面要看得到你的學號與姓名;
如果用拍照則請順便把你的可變電阻旋鈕與三色 LED 燈一起拍進去,
這樣可以獲得額外加分 :-)