Advertisement
Guest User

Code for Laser

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