Taller de Audio del Centro Multimedeia del CNA

Arduino-SC3 Puerto Serial


Conexión Arduino-SuperColider3 usando el Puerto Serial
Aqui mostramos un ejemplo de cómo conectar 6 potenciómetros y 12 botones al Arduino para mandar mensajes a SuperCollider.

Arduino-SC3 Puerto Serial


                                   (En la foto solo estan conectados 3 potenciómatros y tres botones)



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

En Arduino escribir el código siguiente


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

    for(int i=2; i<=13; i++) {
     pinMode(i, INPUT);
       }
}


void loop () {

  Serial.write(255);
  delay(10);

  Serial.write((analogRead(0)/4)*0.99);
  delay(10);
  Serial.write((analogRead(1)/4)*0.99);
  delay(10);
  Serial.write((analogRead(2)/4)*0.99);
  delay(10);
  Serial.write((analogRead(3)/4)*0.99);
  delay(10);
  Serial.write((analogRead(4)/4)*0.99);
  delay(10);
  Serial.write((analogRead(5)/4)*0.99);
  delay(10);
 
  Serial.write(digitalRead(2));
  delay(10);
  Serial.write(digitalRead(3));
  delay(10);
  Serial.write(digitalRead(4));
  delay(10);
  Serial.write(digitalRead(5));
  delay(10);
 
  Serial.write(digitalRead(6));
  delay(10);
  Serial.write(digitalRead(7));
  delay(10);
  Serial.write(digitalRead(8));
  delay(10);
  Serial.write(digitalRead(9));
  delay(10);

  Serial.write(digitalRead(10));
  delay(10);
  Serial.write(digitalRead(11));
  delay(10);
  Serial.write(digitalRead(12));
  delay(10);
  Serial.write(digitalRead(13));
  delay(10);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

En SuperCollider escribir el código que sigue:

(
Tdef(\tiempoSerial, {var dato, index=0, array=Array.fill(18, 1);
             var switch1=1, switch1Ant,switch2=1, switch2Ant,switch3=1, switch3Ant,switch4=1, switch4Ant;
             var boton1=1, boton1Ant,boton2=1, boton2Ant,boton3=1, boton3Ant,boton4=1, boton4Ant;
             var boton5=1, boton5Ant,boton6=1, boton6Ant,boton7=1, boton7Ant,boton8=1, boton8Ant;
             var pote1=1, pote1Ant,pote2=1, pote2Ant,pote3=1, pote3Ant,pote4=1, pote4Ant;
             var pote5=1, pote5Ant, pote6=1, pote6Ant;

                        inf.do{
                              dato=p.next;
                                  if(dato!=nil, {
                               
                switch1Ant=switch1;switch2Ant=switch2;switch3Ant=switch3;switch4Ant=switch4;   
                boton1Ant=boton1;boton2Ant=boton2;boton3Ant=boton3;boton4Ant=boton4;   
                boton5Ant=boton5;boton6Ant=boton6;boton7Ant=boton7;boton8Ant=boton8;
                pote1Ant=pote1;pote2Ant=pote2;pote3Ant=pote3;pote4Ant=pote4;   
                pote5Ant=pote5;pote6Ant=pote6;
                                               
                            if(dato==255, {index=0},
                                               {
                                               array[index]=dato;
                                               index=(index+1)%array.size;
                                               });

                                          /////  Analog In 0 - 5   ////                              
                              
                if(pote1Ant!=array[0], {pote1=array[0];array.postln;});
                if(pote2Ant!=array[1], {pote2=array[1];array.postln;});
                if(pote3Ant!=array[2], {pote3=array[2];array.postln;});
                if(pote4Ant!=array[3], {pote4=array[3];array.postln;});
                if(pote5Ant!=array[4], {pote5=array[4];array.postln;});
                if(pote6Ant!=array[5], {pote6=array[5];array.postln;});

                                           /////  Digital In 2 - 5   ////                           
                                                               
                if(boton1Ant!=array[6], {boton1=array[6];array.postln;});
                if(boton2Ant!=array[7], {boton2=array[7];array.postln;});
                if(boton3Ant!=array[8], {boton3=array[8];array.postln;});
                if(boton4Ant!=array[9], {boton4=array[9];array.postln;});
                              
                                             /////  Digital In 6 - 9   ////               
                               
                if(switch1Ant!=array[10], {switch1=array[10];array.postln;});
                if(switch2Ant!=array[11], {switch2=array[11];array.postln;});
                if(switch3Ant!=array[12], {switch3=array[12];array.postln;});
                if(switch4Ant!=array[13], {switch4=array[13];array.postln;});
                                                                                                                               
                                          /////  Digital In 10 - 13   ////                       
                           
                if(boton5Ant!=array[14], {boton5=array[14];array.postln;});
                if(boton6Ant!=array[15], {boton6=array[15];array.postln;});
                if(boton7Ant!=array[16], {boton7=array[16];array.postln;});
                if(boton8Ant!=array[17], {boton8=array[17];array.postln;});

               
                                });
                                   
                0.001.wait;
                                }
                        }).quant_(0);
);


/*
Para saber que escribir en el primer argumneto del SerialPort tomar la información
del menú Tools/Serial Port
*/

(
p=SerialPort("/dev/cu.usbserial", 9600); 
Tdef(\tiempoSerial).play;
);
(
Tdef(\tiempoSerial).stop;
p.close
)


// Si no funciona probar restartear el Arduino o desconectar y conectar de nuevo el cable.



/////////////////////////////////////////////////////////////////////////////////////////////////
}