読者です 読者をやめる 読者になる 読者になる

落書きノート

ふと自分が気になった事を書いてます

Arduino + Processing-Java

オライリーの参考書を読んで試していました。この記事で最後のプログラムです。参考書通りにやっても出来なかったので少し修正してあります。Processingにおいてのポートの指定は環境によって異なります。Arduino IDEでポート番号をある程度推測出来ます。

Arduino IDE側のプログラム

const int SENSOR = 0;
const int R_LED = 9;
const int G_LED = 10;
const int B_LED = 11;
const int BUTTON = 12;

int val = 0;

int btn = LOW;
int old_btn = LOW;
int state = 0;
char buffer[7];
int pointer = 0;
byte inByte = 0;

byte r = 0;
byte g = 0;
byte b = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(BUTTON, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = analogRead(SENSOR);
  Serial.println(val);

  if(Serial.available() > 0){
    inByte = Serial.read();

    if(inByte == '#'){
      while(pointer < 6){
        buffer[pointer] = Serial.read();
        pointer++;
      }

      r = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      g = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      b = hex2dec(buffer[5]) + hex2dec(buffer[4]) * 16;

      pointer = 0;
    }
  }

  btn = digitalRead(BUTTON);

  if((btn == HIGH) && (old_btn == LOW)){
    state = 1 - state;
  }

  old_btn = btn;

  if(state == 1){
    analogWrite(R_LED, r);
    analogWrite(G_LED, g);
    analogWrite(B_LED, b);
  }else{
    analogWrite(R_LED, 0);
    analogWrite(G_LED, 0);
    analogWrite(B_LED, 0);
  }
  delay(100);
}

int hex2dec(byte c){
  if(c >= '0' && c <= '9'){
    return c - '0';
  }else if(c >= 'A' && c <= 'F'){
    return c - 'A' + 10;
  }
}

Processing側のプログラム

import processing.serial.*;
import java.net.*;
import java.io.*;
import java.util.*;

String feed = "http://blog.arduino.cc";

int interval = 10;
int lastTime;

int make = 0;
int windows = 0;
int arduino = 0;

int light = 0;

Serial port;
color c;
String cs;

String buffer = "";

PFont font;

void setup(){
 size(640, 480);
 frameRate(10);
 
 font = loadFont("HelveticaNeue-Bold-32.vlw");
 fill(255);
 textFont(font, 32);
 
 String arduinoPort = Serial.list()[2];
 
 port = new Serial(this, arduinoPort, 9600);
 
 lastTime = 0;
 fetchData();
}

void draw(){
  background(c);
  int n = (interval - ((millis() - lastTime) / 1000));
  
  c = color(make, windows, arduino);
  cs = "#" + hex(c, 6);
  
  text("Arduino Networked Lamp", 10, 40);
  text("Reading feed:", 10, 100);
  text(feed, 10, 140);
  
  text("Next update in " + n + " seconds", 10, 450);
  text("make", 10, 200);
  text(" " + make, 130, 200);
  rect(200, 172, make, 28);
  
  text("windows", 10, 240);
  text(" " + windows, 130, 240);
  rect(200, 212, windows, 28);
  
  text("arduino", 10, 280);
  text(" " + arduino, 130, 280);
  rect(200, 252, arduino, 28);
  
  text("sending", 10, 340);
  text(cs, 200, 340);
  text("light level", 10, 380);
  
  rect(200, 352, light / 10.23, 28);
  
  if(n <= 0){
   fetchData();
   lastTime = millis();
  }
  
  port.write(cs);
  
  if(port.available() > 0){
    int inByte = port.read();
    if(inByte != 10){
     buffer = buffer + char(inByte); 
    }else{
     if(buffer.length() > 1){
       buffer = buffer.substring(0, buffer.length() - 1);
       
       light = int(buffer);
       
       buffer = "";
       
       port.clear();
     }
    }
  }
}

void fetchData(){
 String data;
 String chunk;
 
 make = 20;
 windows = 20;
 arduino = 20;
 
 try{
   URL url = new URL(feed);
   URLConnection conn = url.openConnection();
   conn.connect();
   
   BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
   
   while((data = in.readLine()) != null){
    StringTokenizer st = new StringTokenizer(data, "\"<>,.()[] ");
    while(st.hasMoreTokens()){
     chunk = st.nextToken().toLowerCase();
     
     if(chunk.indexOf("make") >= 0)
       make++;
     if(chunk.indexOf("windows") >= 0)
       windows++;
     if(chunk.indexOf("arduino") >= 0)
       arduino++;
    }
   }
   
   if(make > 64) make = 64;
   if(windows > 64) windows = 64;
   if(arduino > 64) arduino = 64;
   
   make = make * 4;
   windows = windows * 4;
   arduino = arduino * 4;
 }catch(Exception ex){
  ex.printStackTrace();
  System.out.println("ERROR: " + ex.getMessage());
 }
}

Jpeg