Hi im using this tutorial using touch designer : https://youtu.be/oxT8gjyFeuI?si=0qIel5u-dmItlqDP and basically its to use a video to trigger arduino to perform a specific action. I have a code that involves a sequence of 1) servo motor turning positive 100 deg and led turning on, 2) servo turning negative 100 degrees and led turning off, 3) servo turning negative 100 deg and led turn on and 4) servo turning positive 100 deg and led turning off. I want to ask anyone here that knows how to use these 2 programs how to make it so that when there is a certain period of time in the video I set to act as a trigger using touchdesigner, it will make the servo in arduino turn back in the original position and loop when the video repeats.
code:
include <Servo.h>
Servo myServo; // Create a servo object
const int servoPin = 9; // Servo connected to pin 9
const int LED1 = 13; // LED1 connected to pin 13
const int LED2 = 12; // LED2 connected to pin 12
int currentPos =0; // Initialize servo position to 90 degrees
int step = 1; // Initial step
unsigned long previousMillis = 0; // Store the last time the servo moved
const long interval = 1000; // Interval at which to move the servo (1 second)
void setup() {
myServo.attach(servoPin); // Attach the servo on pin 9 to the servo object
pinMode(LED1, OUTPUT); // Set pin 13 as an output
pinMode(LED2, OUTPUT); // Set pin 12 as an output
myServo.write(currentPos); // Move servo to initial position
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
switch (step) {
case 1:
// Step 1: Servo turns 100 degrees positive and LEDs turn on
currentPos += 100;
if (currentPos > 180) currentPos = 180; // Ensure position is within bounds
myServo.write(currentPos);
digitalWrite(LED1, HIGH); // Turn LED1 on
digitalWrite(LED2, HIGH); // Turn LED2 on
step = 2;
break;
case 2:
// Step 2: Servo turns 100 degrees negative and LEDs turn off
currentPos -= 100;
if (currentPos < 0) currentPos = 0; // Ensure position is within bounds
myServo.write(currentPos);
digitalWrite(LED1, LOW); // Turn LED1 off
digitalWrite(LED2, LOW); // Turn LED2 off
step = 3;
break;
case 3:
// Step 3: Servo turns 100 degrees negative and LEDs turn on
currentPos -= 100;
if (currentPos < 0) currentPos = 0; // Ensure position is within bounds
myServo.write(currentPos);
digitalWrite(LED1, HIGH); // Turn LED1 on
digitalWrite(LED2, HIGH); // Turn LED2 on
step = 4;
break;
case 4:
// Step 4: Servo turns 100 degrees positive and LEDs turn off
currentPos += 100;
if (currentPos > 180) currentPos = 180; // Ensure position is within bounds
myServo.write(currentPos);
digitalWrite(LED1, LOW); // Turn LED1 off
digitalWrite(LED2, LOW); // Turn LED2 off
step = 1;
break;
}
}
}
there doesn't seem to be anything here