Tuesday, 3 June 2014

Final exhibition codes

ARDUINO:
int softpotPin0 = A0; //analog pin 0
int softpotPin1 = A1; //analog pin 1
int softpotPin2 = A2; //analog pin 2
void setup() {

Serial.begin(115200);
digitalWrite(softpotPin0, HIGH); //enable pullup resistor
digitalWrite(softpotPin1, HIGH); //enable pullup resistor
digitalWrite(softpotPin2, HIGH); //enable pullup resistor
}
void loop(){

 int softpotReading0 = analogRead(softpotPin0);
 int softpotReading1 = analogRead(softpotPin1);
 int softpotReading2 = analogRead(softpotPin2);

 int val1 = map(softpotReading0, 0, 1023, 127, 0);
 int val2 = map(softpotReading1, 0, 1023, 127, 0);
 int val3 = map(softpotReading2, 0, 1023, 127, 0);

 Serial.print(val1);
Serial.print(";");
Serial.print(val2);
Serial.print(";");
Serial.print(val3);
Serial.println(">");
}

PROCESSING (+max):
import oscP5.*;
import netP5.*;
import processing.serial.*;
import nl.tue.id.oocsi.*;

OscP5 oscP5;
NetAddress myRemoteLocation;
NetAddress myRemoteLocation2;
NetAddress myRemoteLocation3;
NetAddress myRemoteLocation4;
OOCSI oocsi;

Serial myPort; // we're calling the port "myPort"
String dataReading = "";
int x; // this is val1 on the Arduino
int y;
int z;
int lastz;

void setup() {
 size(500,500); // size of the window
 myPort = new Serial(this, Serial.list()[0], 115200); // reading it right
 myPort.bufferUntil('\n'); // goes on until manual stop
 oocsi = new OOCSI(this, "thanos", "diserver.id.tue.nl");

 oscP5 = new OscP5(this,5001);
 myRemoteLocation = new NetAddress("127.0.0.1", 5004);
 myRemoteLocation2 = new NetAddress("127.0.0.1", 5002);
 myRemoteLocation3 = new NetAddress("127.0.0.1", 5003);
}

void draw() {
 background(255);
 noStroke();
 fill(255,0,0,90);
 rect(30,0,30,x/4);
 fill(0);
 textAlign(CENTER, BOTTOM);
 text("softpot1",45,400);
 text(x, 45, 420);
 noStroke();
 fill(0,255,0,90);
 rect(100,0,30,y/4);
 fill(0);
 textAlign(CENTER, BOTTOM);
 text("softpot2",115,400);
 text(y,115,420);
 noStroke();
 fill(0,0,255,90);
 rect(180,0,30,z/4);
 fill(0);
 textAlign(CENTER, BOTTOM);
 text("softpot3",195,400);
 text(z,195,420);

 OscMessage myMessage = new OscMessage("");
 myMessage.add(x);
 oscP5.send(myMessage, myRemoteLocation);

 OscMessage myMessage2 = new OscMessage("");
 myMessage2.add(y);
 oscP5.send(myMessage2, myRemoteLocation2);

 OscMessage myMessage3 = new OscMessage("");
 myMessage3.add(z);
 oscP5.send(myMessage3, myRemoteLocation3);



    oocsi
 .channel("thanothijs")
 .data("softpot", x)
 .data("softpot2", y)
 .send();

}

void serialEvent(Serial myPort) {
 try {
   dataReading = myPort.readString();
   if(dataReading!=null) {
     String[] trim = split(dataReading,'>');
     String[] pieces = split(trim[0], ';');

     x = parseInt(pieces[0]);
     y = parseInt(pieces[1]);
     z = parseInt(pieces[2]);

   }
 }
 catch (Exception e){
   println("Error");
 }
}

No comments:

Post a Comment