samedi 25 août 2012

Processing code

Here are extracts of processing code to use captors and controlling a synth (the synth is not detailed here) :

1. Variable declaration

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
boolean noteon[] = new boolean[14];
int note=0;
float analogvalue[] = new float[6];
boolean play = false;
boolean selec = false;


2. SETUP
Initialization of arduino things

void setup() {
  size(470, 280);
  arduino = new Arduino(this, "COM5", 57600);
 
  for (int i = 0; i <= 13; i++)
  {
    arduino.pinMode(i, Arduino.INPUT);
    noteon[i]=false;
  }
 
  for (int i = 0; i <= 5; i++)
  {
    analogvalue[i] = 0;
  }
}


3. Function "Findnote"
It will search which note to play when buttons are pressed

void findnote()
{
  if(noteon[10]&&noteon[8]&&noteon[7]&&noteon[6]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 1;
  if(!noteon[10]&&!noteon[9]&&noteon[8]&&noteon[7]&&noteon[6]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 2;
  if(!noteon[10]&&noteon[9]&&noteon[8]&&noteon[7]&&noteon[6]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 3;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&noteon[7]&&noteon[6]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 4;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&noteon[6]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 5;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&noteon[7]&&!noteon[6]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 6;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&!noteon[6]&&!noteon[5]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 7;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&!noteon[6]&&noteon[5]&&noteon[4]&&noteon[3]&&noteon[2])
    note = 8;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&!noteon[6]&&!noteon[4]&&noteon[3]&&noteon[2])
    note = 9;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&noteon[6]&&!noteon[4]&&!noteon[3]&&noteon[2])
    note = 10;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&!noteon[6]&&!noteon[4]&&!noteon[3]&&noteon[2])
    note = 11;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&!noteon[6]&&!noteon[4]&&noteon[3]&&!noteon[2])
    note = 12;
  if(!noteon[10]&&!noteon[9]&&!noteon[8]&&!noteon[7]&&!noteon[6]&&!noteon[4]&&!noteon[3]&&!noteon[2])
    note = 13;
}



4. Function "Draw"
 
void draw() {
  background(off);
  stroke(on);
 
  for (int i = 0; i <= 13; i++) {
    if (arduino.digitalRead(i) == Arduino.HIGH)
      {
        if(!noteon[i])
        {
          noteon[i] = true;
          play = false;
        }
      fill(on);
     
    }
    else
    {
      if(noteon[i])
        {
          noteon[i] = false;
          play = false;
        }
      fill(off);
    }
   
    rect(420 - i * 30, 30, 20, 20);
  }
 
  findnote();
 
 
  for (int i = 0; i <= 5; i++) {
    analogvalue[i] = arduino.analogRead(i);
    ellipse(280 + i * 30, 240, analogvalue[i] / 16, arduino.analogRead(i) / 16);
   
  }
 
  if(analogvalue[5]>550)
  {
   
    if(!play)
    {
      play = true;
  //////// Here you can put the "note on" of your synthetisor
    }
    else
//////// Here you can put the "change volume function" of your synthetisor
//////// => volume is analogvalue[5]
   
  }
 
  else
  {
    if(play)
    {
      play = false;
//////// Here you can put the "Stop function" of your synth
    }
  }
//////// Here you can put the CC values of your synth (filter frq, resonance, ...)
//////// => values are analogvalue[i]
}

Aucun commentaire:

Enregistrer un commentaire