上次的上課內容有三 -ABC

A.在processing 利用旋鈕控制線條高高低低 (見圖片A)

1.開啟ARDUINO軟體
file > Sketchbook > examples > communication > Graph

2.將旋鈕插入ARDUINO板
正(紅線)插入5V
負(黑線)插入GRN
訊號線接旁邊的0

3.並將程式寫入板子

4.開啟Serial Monitor 鈕
看一下 轉一下旋鈕  是否可以用

5. 關掉 Serial Monitor 鈕



6.(1)複製ARDUINO以下語法貼入 processing

/* Processing code for this example
.
.
.
    // Add the received value to the array.
    values[63] = val;
  }
}
*/

(2).記得剪下最底下的*/ 貼到最上面//們的下面 做一個註解的結尾

(3)找出程式裡的[0]
將他填入 0 or 1  or  2 看個人電腦的設定
可以到ARDUINO > Tool > Serial Port > (COM 1 23... 由上而下是0.1.2.3)


7.按RUN 即可

--------------------------------------------------------------------------------------------------------------



B.在processing 可以利用旋鈕控制圖型大大小小 (見圖片A)
利用A的城市去修改


1. make a rectangle in "draw"
就是在程式區塊draw裡面
加入以下
rect(30, 20, 55, 55);


2. 設定一個全域變數 : newValue
就是在void setup的前面
加入以下
int newValue = 512;



3.在 “void serialEvent(int serial)“ 程式方塊中
  設定newValue = 旋鈕 (analog in)的數值 (0-1023)
就是在最下面values[63] = val;的下面
加入
newValue = val;


4. 設定x, y, width, or height = newValue;
去更改rect(30, 20, 55, 55); 中間的各種數值
例如改為
rect(30, 20, 55, newValue);


----------------------------------------------------------------------------------------
以下是正確語法可複製測試
----------------------------------------------------------------------------------------


/* Processing code for this example

// Graph
// by David A. Mellis
//
// based on Analog In
// by <a href="http://itp.jtnimoy.com">Josh Nimoy</a>.
*/
import processing.serial.*;

Serial port;
String buff = "";
int NEWLINE = 10;

// Store the last 64 values received so we can graph them.
int[] values = new int[64];
int newValue = 512;
void setup()
{
  size(512, 256);
 
  println("Available serial ports:");
  println(Serial.list());
 
  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[2], 9600); 
 
  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}

void draw()
{
  background(53);
  rect(30, 20, newValue, 55);
  stroke(255);
 
  // Graph the stored values by drawing a lines between them.
  for (int i = 0; i < 63; i++)
    line(i * 8, 255 - values[i], (i + 1) * 8, 255 - values[i + 1]);
   
  while (port.available() > 0)
    serialEvent(port.read());
}

void serialEvent(int serial)
{
  if (serial != NEWLINE) {
    // Store all the characters on the line.
    buff += char(serial);
  } else {
    // The end of each line is marked by two characters, a carriage
    // return and a newline.  We're here because we've gotten a newline,
    // but we still need to strip off the carriage return.
    buff = buff.substring(0, buff.length()-1);
   
    // Parse the String into an integer.  We divide by 4 because
    // analog inputs go from 0 to 1023 while colors in Processing
    // only go from 0 to 255.
    int val = Integer.parseInt(buff)/4;
   
    // Clear the value of "buff"
    buff = "";
   
    // Shift over the existing values to make room for the new one.
    for (int i = 0; i < 63; i++)
      values[i] = values[i + 1];
   
    // Add the received value to the array.
    values[63] = val;
    newValue = val;
  }
}

----------------------------------------------------------------------------------------------------------

C.利用processing裡的圖形按鈕 去控制arduino板的LED燈明暗

1.開啟ARDUINO軟體
file > Sketchbook > examples > communication > physicalpixel
並將程式寫入板子


2. (1).複製以下語法貼入processing

/* Processing code for this example
.
.
  rect(50, 50, 100, 100);  // draw square
}
*/


(2).記得剪下最底下的*/ 貼到最上面//們的下面 做一個註解的結尾

(3)找出程式裡的[0]
將他填入 0 or 1  or  2 看個人電腦的設定
可以到ARDUINO > Tool > Serial Port > (COM 1 23... 由上而下是0.1.2.3)



3.將LED燈 插入ARDUINO板的PIN13
(LED燈腳長為正接PIN13
腳短為負接GND)

測試即可


---------------------------------------bye Bobi陳

全站熱搜
創作者介紹
創作者 bbhouse123 的頭像
bbhouse123

晃哥之arduino教學3

bbhouse123 發表在 痞客邦 留言(2) 人氣()