mardi 22 février 2011

Codes - 3

The fixed Arduino board is connected to a PC and sends serially the time-of-flight values of the ultrasonic pulses. A Processing code uses these tof values to display on a screen the trajectory of rectangles which is a bijection of the red box movements inside the white circle. There is a main PDE that calculates from the two TOFs avta and avtb the mobile coordinates xo and yo using simple geometry. The two US receivers being placed respectively at the (0,0) and (0, L) corners. The more critical part of the computation is the Moving Average Filter or MAF which allows to get mean values using always the "nu" (in our case: 20), more recent data.

           import fullscreen.*;
FullScreen fs;
import krister.Ess.*;
   AudioStream myStream;
SineWave myWave1;
TriangleWave myWave2;
PinkNoise myWave3;

FadeOut myFadeOut;
FadeIn myFadeIn;
Reverb myReverb;

int oldFrequency=0;
boolean doFadeIn=false;
AudioChannel myChannel;
AudioFile myFile=new AudioFile();
                   PImage a; 
                   float r, x, y, xo, yo, xp, yp;
                   int taille = 10;
    // importing the processing serial class
  import processing.serial.*;
  int num = 100;
  int nu = 20;
  int nus = 10;
  float mx[] = new float[num];
  float my[] = new float[num];
// variables for serial connection, portname and baudrate have to be set
  Serial port; 
  int value = 0;
// buffer and value variable for each value and boolean to switch between values
  String bufA="", bufB="", bufC="", bufD=""; 
  boolean buf, buff; // boolean switch to distinguish values
  int value1, value2, value3, value4, n; 
// variables for drawing function
 
// lets user control DisplayItems properties, value output and drawing attributes
float c = 0.34;
  float L = 1280;
  float x1, x2,avta,avtb, avstop, stop, stopp, d1, d2, x1p, x2p;;
  import processing.video.*;

Movie myMovie;

void setup(){
   // start up Ess
  Ess.start(this);
  myChannel=new AudioChannel("son_folle1.wav");
  // create a new AudioStream
  myStream=new AudioStream();
  myStream.smoothPan=true;

  // our waves
  myWave1=new SineWave(0,.33);
  myWave2=new TriangleWave(0,.66);
  myWave3=new PinkNoise(.50);

  // our effects
  myFadeOut=new FadeOut();
  myFadeIn=new FadeIn();
  myReverb=new Reverb();

  // start
  myStream.start();

  frameRate(30);
 // establish serial port connection     
  port = new Serial(this, Serial.list()[1], 9600);
  smooth();
  noStroke();
 size(800, 800);
  noCursor();
   background(255);
   a = loadImage("tableau1 rond1.bmp");
   myMovie = new Movie(this, "folle_entier.mov");
  myMovie.loop();
   // Create the fullscreen object
  fs = new FullScreen(this);
 
  // enter fullscreen mode
  fs.enter();
}
void movieEvent(Movie myMovie) {
  myMovie.read();
}

void draw(){
  while(port.available() > 0){
        value = port.read();
        serialEvent(value);
   }
   if(value2 < 0)
   {
    background(0);
  image(myMovie, 0,0,width,height);
  myChannel.play(Ess.FOREVER);
       myChannel.volume(1.);
   }
   else if(value2 > 0)
   {
     background(255);
     myChannel.stop();
  // adjust the pan
  myStream.pan((2*x-width)/width);

    fill(0);
   ellipse(width/2+5, width/2, width-90, width-40);
// listen to serial port and trigger serial event    
 
     if(value1 < 17000 && value1 > 8000){
  avta = value1;
  }
    if(value3 < 17000 && value3 > 8000){
  avtb = value3;
    }
    d1 = c * (avta-9550);
    d2 = c * (avtb-9550);
    x1 = (d1 + nu * x1p) / (nu + 1);
    x2 = (d2 + nu * x2p) / (nu + 1);
    x1p = x1;
    x2p = x2;
  xo = (sq(x1) - sq(x2) + sq(L))/(2*L);
  yo = sqrt(abs(sq(x1) - sq(xo)));
          x = map(xo, 0, 1170, 50, width);
          y = map(yo, 0, 1050, 50, height);
            n++;
         // draw the curve
  stroke(255);
    strokeWeight(10);
  // draw waveform
  int interp=(int)(((millis()-myStream.bufferStartTime)/(float)myStream.duration)*myStream.size);

  for (int i=0;i<width;i++) {
    float left=y;
    float right=y;

    if (i+interp+1<myStream.buffer2.length) {
      left-=myStream.buffer2[i+interp]*150.0;
      right-=myStream.buffer2[i+1+interp]*150.0;
    }

    line(i,left,i+1,right);
  }
 if(n > (num )){
    for(int i=1; i<num; i++) {
    mx[i-1] = mx[i];
    my[i-1] = my[i];
  }
          r = sqrt(sq(x - width/2) + sq(y - width/2));
         println(r);
      if(r < width/2)
      {
  // Add the new values to the end of the array
      mx[num-1] = x;
      my[num-1] = y;
      }else{
        mx[num-1] = mx[num-2];
        my[num-1] = my[num-2];
      }
  fill(255);
  for(int i=0; i<num; i++) {
    rect(mx[i]-i, my[i]-i, i, i);
  }
  blend(a,0,0,800,800,0,0,800,800,DARKEST);
            }  
                          println(n);
   }
    println("value2: "+ value2+" - value4: "+ value4);
println("value1: "+ value1+" - value3: "+ value3);
    println("x1: "+ int(x1)+" - x2: "+ int(x2)+" - x: "+ int(x)+" - y: "+ int(y));
   println("xo: "+ int(xo)+" - yo: "+ int(yo));
   
}
 

A serial subroutine added as a tab to the main PDE monitor the serial communication between Arduino and Processing:

void serialEvent(int serial){
  if(serial!=10) {     
      if (serial=='A') buf = true;
      if (serial=='B') buf = false;
      if (serial=='C') buff = true;
      if (serial=='D') buff = false;
     
     
      if (buf == true){ if (serial!='A') bufA += char(serial);
                }else if (buf == false) {
                if (serial!='B') bufB += char(serial);
                }
      if (buff == true){ if (serial!='C') bufC += char(serial);
                }else if (buff == false) {
                if (serial!='D') bufD += char(serial);
                }
     
     
      } else {
                if (buf){  value1 = int(bufA); bufA="";
                } else {   value2 = int(bufB); bufB="";
                }
                if (buff){  value3 = int(bufC); bufC="";
                } else {   value4 = int(bufD); bufD="";
                }              
      }     
}


The "value2" linked to 'B' is indicative of the state of motion of the automata, if it is moving the value of "value2" is the +1 corresponding to the value of "frontiere" of the previous code. Depending on "value2" value either a movie is playing on the screen or transparent squares discover a picture hidden behind a black circle while the synthesizer sound is both played and represented by its amplitude waveform.
There is another tab for the sound subroutine:

void audioStreamWrite(AudioStream theStream) {
        if(value2 < 0)
   { 
      myStream.start();
  myStream.fadeTo(0,500);
   }
   else if(value2 > 0)
   {
      myStream.start();
     myStream.fadeTo(1,500);
   }
   int frequency=int(y) / 3;
  myWave1.generate(myStream);
  myWave2.generate(myStream,Ess.ADD);
     
  // adjust our phases
  myWave1.phase+=myStream.size;
  myWave1.phase%=myStream.sampleRate;
  myWave2.phase=myWave1.phase;

  if (doFadeIn) {
    myFadeIn.filter(myStream);
    doFadeIn=false;
  }
  if (frequency!=oldFrequency) {
    // we have a new frequency   
    myWave1.frequency=frequency;

    // non integer frequencies can cause timing issues with our simple timing code
    myWave2.frequency=(int)(frequency*2.33);

    myWave1.phase=myWave2.phase=0;
    // out with the old
    // fade out the old sound to create a
    myFadeOut.filter(myStream);
    doFadeIn=true; 
    oldFrequency=frequency;
  }

  // reverb
  myReverb.filter(myStream,.5);
                          xp = x;
                          yp = y;
}

// we are done, clean up Ess

public void stop() {
  Ess.stop();
  super.stop();
}

1 commentaire:

  1. Si vous avez des problèmes financiers, il est temps pour vous de sourire. Il vous suffit de contacter M. Benjamin avec le montant que vous souhaitez emprunter au faible taux de retour sur investissement de 2% et le délai de paiement qui vous convient et vous aurez votre prêt sous trois jours ouvrés. Je viens de bénéficier pour la sixième fois d'un prêt de 700 mille dollars pour une durée de 180 mois avec possibilité de payer avant la date d'expiration. M. Benjamin m'a aidé avec le prêt, prenez contact avec lui et vous verrez qu'il est un homme très honnête avec un bon cœur.Son email est 247officedept@gmail.com et son numéro de téléphone WhatApp est + 1-989-394 -3740.

    RépondreSupprimer