2013年4月10日 星期三

Arduino + Process


//Arduino 程式
int sensorPin=0;
int val=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
val=analogRead(sensorPin)/4;
Serial.write(val); //老版本会用Serial.print(val,BYTE),现在已经不支持BYTE了。
delay(100);
}






/*Processing接受Arduino控制器的光敏电阻检测的光线越强,

Processing画布上的莫奈油画越明亮,反之,则越暗淡。*/
import processing.serial.*;//导入serial通信库
Serial duankou;//创建对象duankou
float data;//变量存储Arduino传来数据
//声明一个PImage类型的变量photo
PImage photo;
void setup()
{
  size(800,614);//画布尺寸定义
  //调用图片到变量photo
  photo=loadImage("Monet_painting1.jpg");
   //设定通讯端口为COM3,波特率为9600
   String arduinoPort = Serial.list()[1];//憒?[0]銝?嚗停?兞1]
  duankou = new Serial(this, arduinoPort, 9600);
  // duankou = new Serial(this,"COM3",9600);
}

void draw()
{
  background(0);
  if(duankou.available()>0)
  {
    //把Arduino传来的字节存入变量data
    //一个字节数值范围是0~255.
    data=duankou.read();
    //把Arduino传来的字节显示在控制台中
    println(data);        
  }
  //光敏电阻,天色光线越弱,10位的AD转换的数字量愈大
 //滤镜色调函数值越小,画面越暗,所以要255-data 
  tint(255-data);  
  image(photo,0,0);//显示图片
}

沒有留言:

張貼留言

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

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