0% found this document useful (0 votes)
6 views2 pages

Control 2 Servos With A Joystick - Code, Please Used On

The document describes an Arduino project involving a joystick, two servos, and a buzzer. The joystick controls the servos' angles based on its position, and pressing the joystick switch activates the buzzer for one second. The code includes setup and loop functions to read joystick inputs, map them to servo angles, and manage the buzzer sound.

Uploaded by

akniemdam323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Control 2 Servos With A Joystick - Code, Please Used On

The document describes an Arduino project involving a joystick, two servos, and a buzzer. The joystick controls the servos' angles based on its position, and pressing the joystick switch activates the buzzer for one second. The code includes setup and loop functions to read joystick inputs, map them to servo angles, and manage the buzzer sound.

Uploaded by

akniemdam323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

Name: Oliver Wang


Date: Apr. 11
Assignment: 1 Joystick, 2 Servo, 1 Buzzer Connected Together
Brief Description: When move the joystick left and right, it will control one servos from 0-180
degrees. When move the joystick up and down, it will control the other servos from 0-180
degrees. If mount one servo on top of the other servo, will have a super laser pointer. When
press the switch on the joystick, it will sound the buzzer for 1 second.
*/

#include <Servo.h> // Set this code included the “Servo library”, this library allows you to control
servo by using the command such as attach(); and write();. The library is the code other people
already wrote it and well tested, and #include <> is just call and using these code.

int Xpin = A0; // Assign the analog pin A0 for the integer value of joystick X-axis
int Ypin = A1; // Assign the analog pin A1 for the integer value of joystick Y-axis
int Spin = 13; // Assign the digital pin 13 for the integer value of joystick switch
int Xval; // Declare a integer value Xval, will define later
int Yval; // Declare a integer value Yval, will define later
int Sval; // Declare a integer value Sval, will define later
int DLay = 25; // Create a common integer delay time of 25ms
const int servoXPin = 3; // Constantly assign the pin 3 for the integer value of servo motor
const int servoYPin = 5; // Constantly assign the integer value for digital pin 5 for the second
servo motor
Servo servoX; // Create a Servo object to control the servo motor
Servo servoY; // Create another Servo object to control the second servo motor
const int buzzerPin = 7; // Constantly assign the digital pin 7 for the buzzer

void setup() {
Serial.begin(9600); // Let the baud rate in serial monitor use the port 9600(select 9600 baud
when running)

pinMode(Xpin, INPUT); // Set Xpin(A0) as input


pinMode(Ypin, INPUT); // Set Ypin(A1) as input
pinMode(Spin, INPUT_PULLUP); // Set Spin(pin 13) as input, also set as pullup which
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
// digitalWrite(buzzerPin, LOW); // Make sure the buzzer starts off

servoX.attach(servoXPin); // Attach the servo motor to pin 3


servoY.attach(servoYPin); // Attach the second servo motor to pin 5
}

void loop() {
Xval = analogRead(Xpin); // Let the value from X-axis joystick equals to analog input from A0
Yval = analogRead(Ypin); // Let the value from Y-axis joystick equals to analog input from A1
Sval = digitalRead(Spin); // Read the value from joystick switch
delay(DLay);
int angleX = map(Xval, 0, 1023, 0, 180); // Convert the sensor value(0-1023) to servo angle
range(0-180)
int angleY = map(Yval, 0, 1023, 0, 180); // Convert the sensor value(0-1023) to servo angle
range(0-180)
servoX.write(angleX); // Move the servo motor to the position(0-180), position is base on
angleX
servoY.write(angleY); // Move the second servo motor to the position(0-180)

Serial.print("XValue: "); // Display message "XValue:" in Serial Monitor


Serial.print(Xval); //Display the value of X value
Serial.print("\tYValue: "); // Display message "YValue:" in Serial Monitor, use \t to line up
Serial.print(Yval); //Display the value of Y value
Serial.print("\tSwitchS: "); // Display message "SwitchS:" in Serial Monitor, use \t to line up
Serial.print(Sval); //Display the value of Sval
Serial.print("\tAngleofX: "); // Display message "AngelofX:" in Serial Monitor, use \t to line up
Serial.print(angleX); //Display the value of the angle of X "maintenant"(now)
Serial.print("\tAngleofY: "); // Display message "AngelofY:" in Serial Monitor, use \t to line up
Serial.println(angleY); //Display the value of the angle of Y "maintenant"(now)

if (Sval == LOW) {
tone(buzzerPin, 3000); // Piezo play a 3000Hz tone
delay(1000); //Wait for 1000ms until the tone over
noTone(buzzerPin); //Stop the piezo keep play the tone
}

delay(DLay); // Wait for the time value defined in DLay


}

You might also like