つけじょにーのすぱげていコード

主に、競技プログラミング、セキュリティのお勉強の際に書いたすぱげていコードを書き込みます

Arduino&Processing楽器作成3 書き換えてみた

まとめも兼ねて、主にProcessingのコードを書き換えました。
コメントも付け加え、少しわかりやすくなったかなと思います。

Arduino近辺の写真
f:id:tukejonny:20141215223349j:plain

Arduino(変更なし)

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

void loop() {
  String cdsVal = String(analogRead(0));
  String tuning = String(analogRead(1));
  Serial.println(cdsVal + "," + tuning);
  delay(500);
}

・Processing (変更あり)

import processing.serial.*;
import ddf.minim.*;
import ddf.minim.signals.*;
PFont myFont;
Minim minim;
Serial arduino;
AudioOutput out;

//setup function
void setup() {
  size(1000, 1000); //display-size
  
  arduino = new Serial(this, Serial.list()[2], 19200);
  arduino.bufferUntil('\n');
  
  minim = new Minim(this);
  out = minim.getLineOut(Minim.STEREO);
  
  prevTime = millis(); //preview Time
  currentTime = millis(); //current Time
  
  textSize(64); //set text-size
}

//fadeout function
void fadeToBlack() {
  noStroke();
  fill(0, 10);
  rectMode(CORNER);
  rect(0, 0, width, height);
}

int tuneEllipse[] = {500, 400, 300, 200, 100, 50};
int x = width/2, y = height/2;
//draw function
void draw() {
  //draw division
  for(int r = 0; r < 6; r++) {
    for(int c = 0; c <= 360; c+=(int)random(5)) {
      ellipse(tuneEllipse[r]*cos(c), tuneEllipse[r]*sin(c), 5, 5);
      fill(0, 0, 255, 100);
    }
  }
  //draw ellipse
  for(int r = 0; r <= 360; r++) {
    ellipse(x * cos(r), y * sin(r), 5, 5);
    fill(255, 0, 0);
  }
  fadeToBlack(); //fadeout
}

String indata = ""; //tuning
String indata2 = "";//cdsVal
int currentTime, prevTime;
void serialEvent(Serial port) {
  try {
    indata = port.readStringUntil('\n');
    print(indata); print(indata2);
    if(indata != null && indata2 != null) {
      indata = trim(indata); indata2 = trim(indata2); //triming
      int tuning = Integer.parseInt(split(indata, ",")[0]);
      int cdsVal = Integer.parseInt(split(indata, ",")[1]);
      print("cdsVal:" + cdsVal + "\n");
      print("tuning:" + tuning + "\n");
      
      x = tuning; y = tuning; //set (x,y)
      //display tuning value
      String displayTuning = "tuning=" + str(tuning);
      text(displayTuning, width/2, height/2-100);
      
      //Displaying text which express tone.
      if(tuning >= 500) {
        text("do", width/2, height/2); //do
      } else if(tuning >= 400) {
        text("re", width/2, height/2); //re
      } else if(tuning >= 300) {
        text("mi", width/2, height/2); //mi
      } else if(tuning >= 200) {
        text("fa", width/2, height/2); //fa
      } else if(tuning >= 100) {
        text("so", width/2, height/2); //so
      } else if(tuning >= 50) {
        text("ra", width/2, height/2);; //la
      } else if(tuning >= 0) {
        text("si", width/2, height/2); //si
      } else {                   
        text("out of volume", width/2, height/2);
      }
    
      // many sounds
      //do-re-mi-fa-so-la-si
      if(cdsVal >= 850) { //outer if statement
        if(tuning >= 500) { //inner if statement
          out.playNote(261.6); //do
        } else if(tuning >= 400) {
          out.playNote(293.7); //re
        
        } else if(tuning >= 300) {
          out.playNote(329.6);//mi
          
        } else if(tuning >= 200) {
          out.playNote(349.2);//fa
          
        } else if(tuning >= 100) {
          out.playNote(392.1);//so
          
        } else if(tuning >= 50) {
          out.playNote(440);//la
        } else if(tuning >= 0) {
          out.playNote(494.1);//si
        } //end of inner if statement
      } //end of outer if statement
    }
  } catch(Exception e) { //catch Exception
    e.printStackTrace();
  }
}