Difference between revisions of "TouchDesigner & Arduino"
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
== Using Serial Communication== | == Using Serial Communication== | ||
− | Serial communication is a method for transferring data between devices. It is able to send data sequentially over a wire. In this case the USB cable that connects your Arduino with the computer running TouchDesigner. | + | Serial communication is a method for transferring data between devices. It is able to send data sequentially over a wire. In this case the USB cable that connects your Arduino with the computer running TouchDesigner. <br> |
+ | For this example we are getting sensor data from a LDR and a Ultrasonic sensor, sensing light values and distance. | ||
+ | |||
+ | ===Arduino wiring and code=== | ||
+ | <syntaxhighlight lang="c"> | ||
+ | //LDR PIN and VARIABLE | ||
+ | int sensorPin = A0; // select the input pin for LDR | ||
+ | int sensorValue = 0; // variable to store the value coming from the sensor | ||
+ | //ULTRASONIC SENSOR PIN and VARIABLEs | ||
+ | const int trigPin = 2; | ||
+ | const int echoPin = 3; | ||
+ | long duration; | ||
+ | int distance; | ||
+ | |||
+ | void setup() { | ||
+ | //start serial communication | ||
+ | Serial.begin(9600); //sets serial port for communication | ||
+ | |||
+ | //define ultrasonic sensor pin as in or out | ||
+ | pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output | ||
+ | pinMode(echoPin, INPUT); // Sets the echoPin as an Input | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | //getting sensor data from LDR | ||
+ | sensorValue = analogRead(sensorPin); // read the value from the sensor | ||
+ | |||
+ | //getting sensor data from ultrasonic sensor | ||
+ | // Clears the trigPin | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | delayMicroseconds(2); | ||
+ | // Sets the trigPin on HIGH state for 10 micro seconds | ||
+ | digitalWrite(trigPin, HIGH); | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | // Reads the echoPin, returns the sound wave travel time in microseconds | ||
+ | duration = pulseIn(echoPin, HIGH); | ||
+ | // Calculating the distance | ||
+ | distance = duration * 0.034 / 2; | ||
+ | |||
+ | // Prints and sends the LDR and distance sensor values on the serial monitor on two separate lines | ||
+ | Serial.println(sensorValue); | ||
+ | Serial.println(distance); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
===Arduino to TouchDesigner=== | ===Arduino to TouchDesigner=== | ||
===TouchDesigner to Arduino=== | ===TouchDesigner to Arduino=== |
Revision as of 11:52, 22 March 2024
You can use Arduino to send (sensor) data to TouchDesigner and vice versa.
For example, you might want to measure the light condition of a room and use this data to trigger something in TouchDesigner. Or you might want to be able to have a light bulb react to a sound file, or a motor spin in sync with a projection.
Using Serial Communication
Serial communication is a method for transferring data between devices. It is able to send data sequentially over a wire. In this case the USB cable that connects your Arduino with the computer running TouchDesigner.
For this example we are getting sensor data from a LDR and a Ultrasonic sensor, sensing light values and distance.
Arduino wiring and code
//LDR PIN and VARIABLE
int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
//ULTRASONIC SENSOR PIN and VARIABLEs
const int trigPin = 2;
const int echoPin = 3;
long duration;
int distance;
void setup() {
//start serial communication
Serial.begin(9600); //sets serial port for communication
//define ultrasonic sensor pin as in or out
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
//getting sensor data from LDR
sensorValue = analogRead(sensorPin); // read the value from the sensor
//getting sensor data from ultrasonic sensor
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints and sends the LDR and distance sensor values on the serial monitor on two separate lines
Serial.println(sensorValue);
Serial.println(distance);
}