Code
The Arduino and Processing code for development was largely referenced from the Adrianino input boards.
I modified them slightly to take into account handing off data for two different touch buttons, but in development left the second button as a flat returned value of 10000.
long result;
int analog_pin = 2;
int tx_pin = 1;
void setup() {
pinMode(tx_pin,OUTPUT);
Serial.begin(115200);
}
long tx_rx(){
int read_high;
int read_low;
int diff;
long int sum;
int N_samples = 100;
sum = 0;
for (int i = 0; i < N_samples; i++){
digitalWrite(tx_pin,HIGH);
read_high = analogRead(analog_pin);
delayMicroseconds(100);
digitalWrite(tx_pin,LOW);
read_low = analogRead(analog_pin);
diff = read_high - read_low;
sum += diff;
}
return sum;
}
void loop() {
result = tx_rx();
Serial.print(result);
Serial.print(",");
Serial.println(10000);
delay(100);
}
import processing.serial.*;
float sensorValue;
float sensorValue2;
Serial myPort;
String teststring;
void setup() {
size(1024, 500);
myPort = new Serial(this, "COM6", 115200);
background(255);
}
void draw() {
noStroke();
fill(255,0,0,20);
rect(0, 0, sensorValue, 200);
rect(0, 300, sensorValue, 200);
fill(255,70);
rect(sensorValue, 0, width-sensorValue, 200);
rect(sensorValue, 300, width-sensorValue, 200);
println(sensorValue + " - " + sensorValue2);
fill(0,0,0);
text(sensorValue + " " , sensorValue, 100);
text(sensorValue + " " , sensorValue, 400);
textSize(32);
}
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
teststring = inString;
if (inString != null) {
inString = trim(inString);
float[] values = float(split(inString, ","));
if (values.length >=1) {
sensorValue = values[0];
sensorValue2 = values[1];
}
}
}