#include <Servo.h>
Servo servo1; // Create servo object for servo 1
Servo servo2; // Create servo object for servo 2
const int ledPin = 11; // Pin for LED/Laser
const int servo1Pin = 9; // Pin for Servo 1
const int servo2Pin = 10; // Pin for Servo 2
void setup() {
// Attach the servos to their respective pins
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
// Initialize LED/Laser pin
pinMode(ledPin, OUTPUT);
}
void loop() {
// Move servo 1
for (int pos = 0; pos <= 180; pos += 1) {
servo1.write(pos); // Move to position
delay(15); // Wait for servo to reach position
}
for (int pos = 180; pos >= 0; pos -= 1) {
servo1.write(pos); // Move back to position
delay(15); // Wait for servo to reach position
}
// Move servo 2
for (int pos = 0; pos <= 180; pos += 1) {
servo2.write(pos); // Move to position
delay(15); // Wait for servo to reach position
}
for (int pos = 180; pos >= 0; pos -= 1) {
servo2.write(pos); // Move back to position
delay(15); // Wait for servo to reach position
}
// Blink the LED/Laser
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin, HIGH); // Turn on LED/Laser
delay(500); // Wait for half a second
digitalWrite(ledPin, LOW); // Turn off LED/Laser
delay(500); // Wait for half a second
}
}
// #include <Servo.h>
// Servo verticalServo;
// Servo horizontalServo;
// // Define pin numbers for the servos
// #define VERTICAL_PIN 10 // Pin for the vertical servo
// #define HORIZONTAL_PIN 9 // Pin for the horizontal servo
// #define LedPin 13
// // Timing variables
// unsigned long previousMillis = 0; // Store last time LED was updated
// const long interval = 200; // Interval at which to blink (milliseconds)
// int ledState = LOW; // Current state of the LED
// bool movingServos = false; // Flag to indicate if servos are moving
// void setup() {
// pinMode(LedPin, OUTPUT); // Set LED pin as output
// verticalServo.attach(VERTICAL_PIN);
// horizontalServo.attach(HORIZONTAL_PIN);
// // Move both servos to 0 degrees initially
// verticalServo.write(0);
// horizontalServo.write(0);
// delay(1000); // Wait for a second to stabilize
// }
// void loop() {
// unsigned long currentMillis = millis(); // Get current time
// // Blink the LED every interval
// if (currentMillis - previousMillis >= interval) {
// previousMillis = currentMillis; // Save the last time LED was updated
// ledState = (ledState == LOW) ? HIGH : LOW; // Toggle LED state
// digitalWrite(LedPin, ledState); // Update the LED
// }
// // Move both servos to 180 degrees if not already moving
// if (!movingServos) {
// movingServos = true; // Set flag to indicate servos are moving
// moveServosTo(180); // Move servos to 180 degrees
// delay(1000); // Wait for 1 second at this position
// moveServosTo(0); // Move servos back to 0 degrees
// delay(1000); // Wait for 1 second at this position
// movingServos = false; // Reset flag after movement is complete
// }
// }
// // Function to move both servos to a target angle slowly
// void moveServosTo(int targetAngle) {
// int currentAngleVertical = verticalServo.read(); // Get current angle of vertical servo
// int currentAngleHorizontal = horizontalServo.read(); // Get current angle of horizontal servo
// int stepVertical = (targetAngle > currentAngleVertical) ? 1 : -1; // Determine direction for vertical servo
// int stepHorizontal = (targetAngle > currentAngleHorizontal) ? 1 : -1; // Determine direction for horizontal servo
// while (currentAngleVertical != targetAngle || currentAngleHorizontal != targetAngle) {
// if (currentAngleVertical != targetAngle) {
// currentAngleVertical += stepVertical; // Increment or decrement angle by step
// verticalServo.write(currentAngleVertical); // Set vertical servo position
// }
// if (currentAngleHorizontal != targetAngle) {
// currentAngleHorizontal += stepHorizontal; // Increment or decrement angle by step
// horizontalServo.write(currentAngleHorizontal); // Set horizontal servo position
// }
// delay(20); // Adjust this value to change speed (higher = slower)
// }
// }
// #include <Servo.h>
// Servo verticalServo;
// Servo horizontalServo;
// // Define pin numbers for the servos
// #define VERTICAL_PIN 9 // Pin for the vertical servo
// #define HORIZONTAL_PIN 10 // Pin for the horizontal servo
// #define LedPin 13
// void setup() {
// pinMode(LedPin, OUTPUT); // Set LED pin as output
// verticalServo.attach(VERTICAL_PIN);
// horizontalServo.attach(HORIZONTAL_PIN);
// // Move both servos to 0 degrees initially
// verticalServo.write(0);
// horizontalServo.write(0);
// delay(1000); // Wait for a second to stabilize
// }
// void loop() {
// // Blink the LED
// digitalWrite(LedPin, HIGH);
// delay(200);
// digitalWrite(LedPin, LOW);
// delay(200);
// // Move both servos to 180 degrees (max for standard servos)
// moveServo(verticalServo, 180);
// moveServo(horizontalServo, 180);
// delay(1000); // Wait for 1 seconds at this position
// // Move both servos back to 0 degrees
// moveServo(verticalServo, 0);
// moveServo(horizontalServo, 0);
// delay(1000); // Wait for 1 seconds at this position
// }
// // Function to move servo to a target angle slowly
// void moveServo(Servo &servo, int targetAngle) {
// int currentAngle = servo.read(); // Get current angle
// int step = (targetAngle > currentAngle) ? 1 : -1; // Determine direction
// // Move in steps towards the target angle
// while (currentAngle != targetAngle) {
// currentAngle += step; // Increment or decrement angle by step
// servo.write(currentAngle); // Set servo position
// delay(5); // Adjust this value to change speed (higher = slower)
// }
// }
// #include <Servo.h>
// Servo verticalServo;
// Servo horizontalServo;
// // Define pin numbers for the servos
// #define VERTICAL_PIN 10 // Pin for the vertical servo
// #define HORIZONTAL_PIN 9 // Pin for the horizontal servo
// #define LedPin 13
// void setup() {
// pinMode (LedPin, OUTPUT);// put your setup code here, to run once:
// verticalServo.attach(VERTICAL_PIN);
// horizontalServo.attach(HORIZONTAL_PIN);
// // Move both servos to 0 degrees initially
// verticalServo.write(0);
// horizontalServo.write(0);
// delay(1000); // Wait for a second to stabilize
// }
// void loop() {
// digitalWrite(LedPin, HIGH);
// delay(200);
// // digitalWrite(LedPin, LOW);// put your main code here, to run repeatedly:
// // delay(200);
// verticalServo.write(360);
// horizontalServo.write(360);
// delay(2000); // Wait for 2 seconds at this position
// // Move both servos back to 0 degrees
// verticalServo.write(0);
// horizontalServo.write(0);
// delay(2000); // Wait for 2 seconds at this position
// }
// // Function to move servo to a target angle slowly
// void moveServo(Servo &servo, int targetAngle) {
// int currentAngle = servo.read(); // Get current angle
// int step = (targetAngle > currentAngle) ? 1 : -1; // Determine direction
// // Move in steps towards the target angle
// for (int angle = currentAngle; angle != targetAngle; angle += step) {
// servo.write(angle); // Set servo position
// delay(15); // Adjust this value to change speed (higher = slower)
// }
// }