Devices & Components
40 colored female-female jumper wires
Arduino Uno Rev3
[HB] Micro Servo MG90
Joystick 2 axes MAKERMIND
Sensor Shield V5 Arduino Uno
Software & Tools
Arduino IDE
Project description
Code
Two-Axis Pan-and-Tilt LED Projekt
cpp
1#include <Servo.h> 2 3// Create the Servo objects 4Servo servoX; 5Servo servoY; 6 7// Pin definitions for the servos 8const int servoXPin = 8; 9const int servoYPin = 9; 10 11// Pin definitions for the joystick 12const int joystickXPin = A0; // Connect the joystick's VRX to A0 13const int joystickYPin = A1; // Connect the joystick's VRY to A1 14 15void setup() { 16 // Attach and initialize the servos 17 servoX.attach(servoXPin); 18 servoY.attach(servoYPin); 19 servoX.write(90); // Set servos to the center position 20 servoY.write(90); 21 22 Serial.begin(9600); // Start serial communication for debugging 23} 24 25void loop() { 26 // Read the analog values from the joystick 27 int joystickXValue = analogRead(joystickXPin); 28 int joystickYValue = analogRead(joystickYPin); 29 30 // Map the joystick values (0-1023) to the servo range (0-180) 31 int servoXPos = map(joystickXValue, 0, 1023, 180, 0); 32 int servoYPos = map(joystickYValue, 0, 1023, 180, 0); 33 34 // Write the new position to the servos 35 servoX.write(servoXPos); 36 servoY.write(servoYPos); 37 38 // Debugging output 39 Serial.print("Joystick X: "); 40 Serial.print(joystickXValue); 41 Serial.print(" -> ServoX Pos: "); 42 Serial.print(servoXPos); 43 Serial.print(" | Joystick Y: "); 44 Serial.print(joystickYValue); 45 Serial.print(" -> ServoY Pos: "); 46 Serial.println(servoYPos); 47 48 delay(10); // Small delay for stabilization 49}
Downloadable files
Two-Axis Pan-and-Tilt LED Projekt
sketch_sep17b.ino
Comments
Only logged in users can leave comments