2013年4月10日 星期三

Arduino + process


//利用滑鼠左右鍵 控制顯示 橢圓或正方形  及控制可變電阻


int potPin = 0;                // 宣告類比腳位輸入的PIN腳
int val = 0;                   // 宣告變數Val = 0
void setup() {
  Serial.begin(9600);          //設定SerialPort的速度9600 = 9600bit/s
}
void loop() {
 val = analogRead(potPin);     // 用analogRead()這個函式讀取特定腳位的數值到變數val


//1024/4=256  將0-1023 轉成 0-255範圍  Serial.print(val/4);           //印出數值在Serial.Monitor
  delay(150);                  //延遲讀入的時間為150ms
}








//利用滑鼠左右鍵 控制顯示 橢圓或正方形  及控制可變電阻
import processing.serial.*;

Serial port; 
float val; 

// 2D Array of objects
Cell[][] grid;

// Number of columns and rows in the grid
int cols = 10;
int rows = 10;
void setup()
{
  String arduinoPort = Serial.list()[1];//憒?[0]銝?嚗停?兞1]
  port = new Serial(this, arduinoPort, 9600);
  
  size(600,600);
  smooth(); 
  grid = new Cell[cols][rows];
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      // Initialize each object
      grid[i][j] = new Cell(i*60,j*60,60,60,i+j);
    }
  } 
}

void draw()
{
  
  if ( port.available()> 0) { 
      val = port.read(); 
  
  if(mousePressed)
  {      
     if(mouseButton==LEFT)
     {
        background(255);
        noFill();
        for(int d=0;d<75;d+=4)
        {
          for(int x1=0;x1<650;x1+=75)
          {
            for(int y1=0;y1<650;y1+=75)
            {
              stroke(random(255),random(255),120);
              strokeWeight(4);
              ellipse(x1+val,y1,d,d+val);
            }
          }
        }
      }
    else if(mouseButton==RIGHT)
    {
      background(255);
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          // Oscillate and display each object
          grid[i][j].oscillate();
          grid[i][j].display();
        }
      }
    }
  }
}
  
}



// A Cell object
class Cell {
  // A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
  float x,y;   // x,y location
  float w,h;   // width and height
  float angle; // angle for oscillating brightness

  // Cell Constructor
  Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    angle = tempAngle;
  } 
  
  // Oscillation means increase angle
  void oscillate() {
    angle += 0.08; 
  }

  void display() {
    stroke(255,10+val,20+val);
    strokeWeight(2);
    // Color calculated using sine wave
    fill(127+(val/10)+127*sin(angle));
    rect(x+val,y,w,h+val); 
  }
}

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...