Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. fh //#include // Include the Servo Library to control the servos
  2. #include <Servo.h>
  3. Servo xservo; // create servo object to control the x direction servo
  4. Servo yservo; // create servo object to control the y direction servo
  5.  
  6. //Servo Variables
  7. int pos = 0; // variable to store the servo position
  8. int XServoPin = 9; // Servo pin that will control X motion
  9. int YServoPin = 8; // Servo pin that will control Y motion
  10.  
  11. //Joystick variables
  12. int xposPin = A0; // select the input pin for the potentiometer
  13. int yposPin = A1; // select the input pin for the potentiometer
  14. int Xpos = 0;
  15. int Ypos = 0;
  16. int buttonPin = 7;
  17. int buttonPress = 0;
  18.  
  19. //Lazer Variables
  20. int lazerPin = 6;
  21. void setup() {
  22. pinMode(lazerPin,OUTPUT); //The lazer will be an output
  23. pinMode(buttonPin,INPUT); //The buttonpress pin is an input
  24. digitalWrite(buttonPin,HIGH); //This enables the Arduino pullup for this pin
  25.  
  26. xservo.attach(XServoPin); // attaches the servo on pin 9 to the servo object
  27. yservo.attach(YServoPin); // attaches the servo on pin 8 to the servo object
  28. }
  29.  
  30. void loop() {
  31.  
  32. buttonPress = digitalRead(buttonPin); //Read the sate of the button
  33. if(buttonPress == LOW) // The button press is active low , so if it is pressed we will turn the lazer on
  34. {
  35. digitalWrite(lazerPin, HIGH); //Turn Lazer on
  36. }
  37. else
  38. {
  39. digitalWrite(lazerPin, LOW); // If it is not pressed, keep lazer off
  40. }
  41.  
  42. Xpos = analogRead(xposPin); // read and store the x position location of the joystick
  43. Xpos = map(Xpos,0,1023,0,180); //map the analog read x values to the 0-180 servo values
  44. Ypos = analogRead(yposPin); // read and store the y position of the joystick
  45. Ypos = map(Ypos,0,1023,0,180); //map the analog read y values to the 0-180 servo values
  46. xservo.write(Xpos); // move the X Location servo to the x position the joystick is at
  47. yservo.write(Ypos); // move the Y Location servo to the y position the joystick is at
  48. delay(50); // add in a slight delay to help with "jittering"
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement