這個範例所使用的是加速度感應器

耐心好重要...我常常缺乏看程式的耐心

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
int i;
int X[] = {0};//x軸加速度
int Y[] = {0};//y軸加速度
int Z[] = {0}//;z軸加速度

void setup()
{
  size(400, 400);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0)
  {
    String val = myPort.readStringUntil('\n');
    if (val != null)
    {      
      val = trim(val);//如果val = X123 Y456 Z789,空白會被清除掉
      String getX = val.substring(val.indexOf("X")+1,val.indexOf("Y"));//從X擷取到Y,會擷取出123 // intdexOf可以指出某個元素的位置
      String getY = val.substring(val.indexOf("Y")+1,val.indexOf("Z"));//從Y擷取到Z,會擷取出456
      String getZ = val.substring(val.indexOf("Z")+1);////從Z擷取,會擷取出789
      //println("X" + getX + "Y" + getY + "Z" + getZ); //(學習筆記,可以這樣顯示)
      X[0] = Integer.parseInt(getX);//x軸加速度,把string轉int儲存
      Y[0] = Integer.parseInt(getY);//y軸加速度,把string轉int儲存
      Z[0] = Integer.parseInt(getZ);//z軸加速度,把string轉int儲存       
      plot();
    }
  }
}

void plot()
{
  background(0);
  text(X[0],100,100);
  text(Y[0],200,100);
  text(Z[0],300,100);
}

//arduino
/*

// ADXL3xx
//
// Reads an Analog Devices ADXL3xx accelerometer and communicates the
// acceleration to the computer.  The pins used are designed to be easily
// compatible with the breakout boards from Sparkfun, available from:
// http://www.sparkfun.com/commerce/categories.php?c=80
//
// http://www.arduino.cc/en/Tutorial/ADXL3xx
//
// The circuit:
// analog 0: accelerometer self test
// analog 1: z-axis
// analog 2: y-axis
// analog 3: x-axis
// analog 4: ground
// analog 5: vcc
//
// created 2 Jul 2008
// by David A. Mellis
// modified 26 Jun 2009
// by Tom Igoe



// these constants describe the pins. They won't change:
const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = 1;                   // x-axis of the accelerometer
const int ypin = 2;                   // y-axis
const int zpin = 3;                   // z-axis (only on 3-axis models)

void setup()
{
  // initialize the serial communications:
  Serial.begin(9600);
 
  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);
}

void loop()
{
  // print the sensor values:
  int x = analogRead(xpin);
  int y = analogRead(ypin);
  int z = analogRead(zpin);
  Serial.print("X");
  Serial.print(x);
  Serial.print("Y");
  Serial.print(y);
  Serial.print("Z");
  Serial.print(z);
  Serial.println();
  // delay before next reading:
  delay(100);
}
*/

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 蘇兄 的頭像
    蘇兄

    Mens et manus

    蘇兄 發表在 痞客邦 留言(0) 人氣()