Thursday, 15 May 2014

Arduino + 3 softpots code to Processing ::

Arduino code:
 
#include <MIDI.h>
int softpotPin0 = A0; //analog pin 0
int softpotPin1 = A1; //analog pin 1
int softpotPin2 = A2; //analog pin 2
void setup() {
//MIDI.begin();
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, 0, 127);
 int val2 = map(softpotReading1, 0, 1023, 0, 127);
 int val3 = map(softpotReading2, 0, 1023, 0, 127);

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




















Processing code:

import processing.serial.*;

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

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
}

void draw() {
  background(255);
  noStroke();
  fill(255,0,0,90);
  rect(30,0,30,x);
  fill(0);
  textAlign(CENTER, BOTTOM);
  text("softpot",45,160);
  text(x, 45, 180);
  noStroke();
  fill(0,255,0,90);
  rect(100,0,30,y);
  fill(0);
  textAlign(CENTER, BOTTOM);
  text("softpot2",115,160);
  text(y,115,180);
  noStroke();
  fill(0,0,255,90);
  rect(180,0,30,z);
  fill(0);
  textAlign(CENTER, BOTTOM);
  text("softpot3",195,160);
  text(z,195,180);
 
}

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]);
     
      print(x);
      print("\t");
      print(y);
      print("\t");
      println(z);
    }
  }
  catch (Exception e){
    println("Error");
  }
}


 




Visualization of 3 softpots on Processing


No comments:

Post a Comment