👁 1048 | Tristan Schulze lately updated this on April 3, 2023
0
// needs both libs installed!
import processing.serial.*;
import osteele.processing.SerialRecord.*;
Serial serialPort;
SerialRecord serialRecord;
int num_of_values_attended = 9;
void setup() {
size(500, 500);
noStroke();
String serialPortName = SerialUtils.findArduinoPort();
serialPort = new Serial(this, serialPortName, 115200);
serialRecord = new SerialRecord(this, serialPort, num_of_values_attended);
}
void draw() {
background(0);
serialRecord.read(); // read serial data
int t1 = serialRecord.values[0];
int t2 = serialRecord.values[1];
int t3 = serialRecord.values[2];
translate(10,10);
fill(255);
rect( 0,0,100,t1*5);
rect( 100,0,100,t2*5);
rect( 200,0,100,t3*5);
}
#include <Arduino.h>
#include <touch.h>
aTouch t1(T6);
aTouch t2(T7);
aTouch t3(T8);
#define tpin_count 3
aTouch touches[3] = { t1,t2,t3};
void setup() {
touches[0] = t1;
touches[1] = t2;
touches[2] = t3;
Serial.begin(115200); // initialize serial communication at 9600 baud rate
}
void loop() {
// read all touch input
for (int i = 0; i < tpin_count; i++) {
touches[i].readAndProcessInput();
}
// send current smoothed touch cap value
for (int i = 0; i < tpin_count; i++) {
Serial.print(touches[i].smoothed_val);
Serial.print(" "); // add a space between each data point for readability
}
// send if the touchpad is pressed
for (int i = 0; i < tpin_count; i++) {
Serial.print(touches[i].is_triggered);
Serial.print(" "); // add a space between each data point for readability
}
// send if the touchpad is holded
for (int i = 0; i < tpin_count; i++) {
Serial.print(touches[i].is_holded);
Serial.print(" "); // add a space between each data point for readability
}
Serial.println(); // add a newline character at the end of the data array
delay(40); // wait for a second before sending the data again
}
// ----------------------------------
// ------- TRXYS TOUCH HELPERS ------
// ----------------------------------
// ( put this code PRIOR to your mainloop code or you will get nice errors :) )
// simple lerp helper function
float return_lerp(float _s, int _target,int _time){
_s = _s + (( float(_target) - _s)/float(_time));
return _s;
}
class aTouch {
private:
bool prev_touch_state = false;
byte pin;
int smooth_time = 4;
int trigger_threshold = 20;
long ts = 0;
public:
int current_val = 0;
int smoothed_val = 0;
int diff_val = 0;
bool is_triggered = false;
bool on_pressed = false;
bool on_released = false;
bool is_holded = false;
aTouch(byte pin) {
this->pin = pin;
}
void readAndProcessInput() {
// reset interaction states
on_pressed = false;
on_released = false;
// directly read out values TWICE = BUGFIX for debouncing
current_val = touchRead(pin);
delayMicroseconds(10);
current_val = touchRead(pin);
//calculate smoothed input values
smoothed_val = return_lerp(smoothed_val,current_val,smooth_time);
// calc current differential sum of button
diff_val = smoothed_val - current_val;
// check if there is a noticable difference input values
if( diff_val > trigger_threshold){
if(prev_touch_state == false){
is_triggered = true;
prev_touch_state = is_triggered;
on_pressed = true;
ts = millis(); // set timestamp for hold routine
}
}else if( diff_val < trigger_threshold*-.7){
if(prev_touch_state == true){
is_triggered = false;
prev_touch_state = is_triggered;
on_released = true;
}
}
// calculate timed holding function
if( ts + 1500 < millis() && is_triggered){
is_holded = true;
}else{
is_holded = false;
}
delayMicroseconds(2);
}
};