2015年1月28日 星期三

移動滑鼠開/關 LED

移動滑鼠到畫面中的小方塊裏面,就會點亮 LED,移開滑鼠 LED 會跟著變暗。

原文詳 http://arduino.cc/en/Tutorial/PhysicalPixel


電路圖




Arduino Code

/*
  Physical Pixel

 An example of using the Arduino board to receive data from the
 computer.  In this case, the Arduino boards turns on an LED when
 it receives the character 'H', and turns off the LED when it
 receives the character 'L'.

 The data can be sent from the Arduino serial monitor, or another
 program like Processing (see code below), Flash (via a serial-net
 proxy), PD, or Max/MSP.

 The circuit:
 * LED connected from digital pin 13 to ground

 created 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Tom Igoe and Scott Fitzgerald

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/PhysicalPixel
 */

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}


Processing Code

// mouseover serial

// Demonstrates how to send data to the Arduino I/O board, in order to
// turn ON a light if the mouse is over a square and turn it off
// if the mouse is not.

// created 2003-4
// based on examples by Casey Reas and Hernando Barragan
// modified 30 Aug 2011
// by Tom Igoe
// This example code is in the public domain.



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);

  // List all the available serial ports in the output pane.
  // You will need to choose the port that the Arduino board is
  // connected to from this list. The first port in the list is
  // port #0 and the third port in the list is port #2.
  println(Serial.list());

  // Open the port that the Arduino board is connected to (in this case #0)
  // Make sure to open the port at the same speed Arduino is using (9600bps)
  port = new Serial(this, "COM5", 9600);
}

void draw()
{
  background(0);

  // Test if the cursor is over the box
  if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
    mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
    mouseOverBox = true;
    // draw a line around the box and change its color:
    stroke(255);
    fill(153);
    // send an 'H' to indicate mouse is over square:
    port.write('H');
  }
  else {
    // return the box to it's inactive state:
    stroke(153);
    fill(153);
    // send an 'L' to turn the LED off:
    port.write('L');    
    mouseOverBox = false;
  }

  // Draw the box
  rect(boxX, boxY, boxSize, boxSize);
}


提醒您

1. 雖然電路圖的 LED 是插在 pin 13,但因為 Arduino 板子上面的 pin 13 已經內建一只 LED,所以您也可以不必插入任何 LED,只要觀察 Arduino 板子上面的 LED 即可。
2. 檢查 COM 埠有沒有正確。
3. 檢查 鮑率 是不是 9600。

移動滑鼠控制 LED 明暗

您可以透過左右移動滑鼠來控制 LED 的明暗,往右變亮,往左變暗。

原文詳 http://arduino.cc/en/Tutorial/Dimmer


電路圖



Arduino Code

const int ledPin = 9;      // the pin that the LED is attached to

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
  }
}


Processing Code

// Dimmer - sends bytes over a serial port
// by David A. Mellis
//This example code is in the public domain.

import processing.serial.*;
Serial port;

void setup() {
  size(256, 150);

  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, "COM5", 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() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, 150);
  }

  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
}


提醒您

1. LED 要插在 pin 9,或是其它 PWM 埠。
2. 檢查 COM 埠有沒有正確。
3. 檢查 鮑率 是不是 9600。


建議

您也可以修改程式碼,把它改成可以控制舵機角度。






2015年1月25日 星期日

照片轉 3D

2D 轉 3D 大體上有兩種方式,一種是沿著 Z 軸擠出高度;另一種是透過許多的 2D 圖片資料,利用數學運算的方式算出物體在空間中的座標,而後建立出 3D 模型。

用一張照片擠出為 3D 模型

Cura 3D 列印軟體可以讀入 JPG 等點陣圖,然後將它另存為 STL 檔。之後可以用 SketchUp 或 ThinkerCAD 編輯它。

Cura 官網與下載 https://ultimaker.com/en/products/software

SketchUp 詳 http://pizgchen.blogspot.tw/2014/06/stl.html

ThinkerCAD 詳 http://pizgchen.blogspot.tw/2015/01/thinkercad.html




用許多照片轉換為 3D 模型

AutoDesk 的 123D Catch 軟體可以幫您把照片轉成 3D 模型。

官網 http://www.123dapp.com/catch

應用詳 http://iphone4.tw/forums/showthread.php?t=210261

ThinkerCAD 下載、安裝與設定

ThinkerCAD 是 AutoDesk 公司開發的一款線上繪圖軟體,您可以用它編輯 STL 檔,也可以匯入 SVG 檔,將兩者結合在一起,成為一個新的 3D 模型。

官網 https://www.tinkercad.com/


下載

ThinkerCAD 是線上繪圖軟體,無須下載,但使用前要先註冊。


註冊

1. 填寫您「所在地區」與「生日」。點擊「下一步」。


2. 您可以用您的 Facebook 帳號註冊,也可以在下方填寫您的「電子郵件地址」與「登入密碼」。點擊「建立帳戶」。



基本操作 http://www.minwt.com/free/9511.html

應用操作 http://inplus.tw/archives/368



十種錯誤操作而毀壞Arduino的方式

請詳 http://lunglungdesign.blogspot.tw/2012/11/arduino.html

2015年1月22日 星期四

畫出頻率圖形

用 Arduino 偵測頻率,並畫出圖形。

電路接法:




下載頻率程式庫 http://interface.khm.de/wp-content/uploads/2009/01/FreqCounter_1_12.zip

首先我們先來練習如何偵測頻率,請將下列程式貼到 Arduino IDE 裏:

#include <FreqCounter.h>

void setup() {
  Serial.begin(57600);                    // connect to the serial port
  Serial.println("Frequency Counter");
}

long int frq;
Void loop() {

  FreqCounter::f_comp= 8;             // Set compensation to 12
  FreqCounter::start(100);            // Start counting with gatetime of 100ms
  while (FreqCounter::f_ready == 0)         // wait until counter ready

    frq=FreqCounter::f_freq;            // read result
  Serial.println(frq);                // print result
  delay(20);
}


接下來我們要將頻率數據畫出圖形,您可以用 Processing、Python、C 或 Matplotlib 等程式化出圖形。

在這裡我將介紹使用 Bridge Control Panel 軟體化出圖形。

下載 Bridge Control Panel http://www.cypress.com/?rID=38050

畫出的圖形如下:



原文詳:
1. http://interface.khm.de/index.php/lab/interfaces-advanced/arduino-frequency-counter-library/
2. http://www.instructables.com/id/Plotting-Data-From-Arduino/












2015年1月21日 星期三

把 Arduino 讀取的數據資料轉化為圖形

Arduino 可以透過各種感測器獲取資料,您可以將這些數據透過 Serial Port 或藍芽送給 Processing 畫出圖形。

本文介紹如何在 Arduino 的 Analog Pin 0 接上一個可變電阻,藉由調整可變電阻獲取變動的數據,然後將這些數據傳送給 Procesing 畫出圖形。




電路圖



Arduino Code:

/*
  Graph

 A simple example of communication from the Arduino board to the computer:
 the value of analog input 0 is sent out the serial port.  We call this "serial"
 communication because the connection appears to both the Arduino and the
 computer as a serial port, even though it may actually use
 a USB cable. Bytes are sent one after another (serially) from the Arduino
 to the computer.

 You can use the Arduino serial monitor to view the sent data, or it can
 be read by Processing, PD, Max/MSP, or any other program capable of reading
 data from a serial port.  The Processing code below graphs the data received
 so you can see the value of the analog input changing over time.

 The circuit:
 Any analog input sensor is attached to analog in pin 0.

 created 2006
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe and Scott Fitzgerald

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Graph
 */

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter
  // to stabilize after the last reading:
  delay(2);
}



Processing Code:

// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return

// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph

void setup () {
  // set the window size:
  size(400, 300);      

  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0);
}
void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    float inByte = float(inString);
    inByte = map(inByte, 0, 1023, 0, height);

    // draw the line:
    stroke(127,34,255);
    line(xPos, height, xPos, height - inByte);

    // at the edge of the screen, go back to the beginning:
    if (xPos >= width) {
      xPos = 0;
      background(0);
    }
    else {
      // increment the horizontal position:
      xPos++;
    }
  }
}



原文詳 http://arduino.cc/en/pmwiki.php?n=Tutorial/Graph


也許你對這個有興趣:

1. LAB: SERIAL OUTPUT FROM AN ARDUINO 
https://itp.nyu.edu/physcomp/labs/labs-serial-communication/serial-output-from-an-arduino/



安裝 RigMesh

這是一套很棒的3D繪圖軟體,我個人覺得它非常適合小孩學習,因為它使用起來就像捏黏土一樣。



下載

http://dl.dropbox.com/u/4964558/rigmesh_v1.01.zip


安裝

解壓縮後免安裝,直接執行即可。


教學影片:

https://www.youtube.com/watch?v=1prInV9ZNY0
https://www.youtube.com/watch?v=wxHA--l44yo