Advertisement
Guest User

Final Code for Project

a guest
Nov 17th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #include <Servo.h>
  2. #define DETECT1 2 // pin 2 for sensor
  3. #define ACTION1 8 // pin 8 for action to do someting
  4. #define DETECT2 3
  5. #define ACTION2 13
  6. #define DETECT3 4
  7. #define ACTION3 12
  8. #define DETECT4 5
  9. #define ACTION4 16
  10. #define DETECT5 11
  11. #define ACTION5 17
  12.  
  13. int detected;
  14.  
  15. Servo xservo;
  16. Servo yservo;
  17. int pos = 0;
  18. const int XServoPin = 9;
  19. const int YServoPin = 10; // Servo pin that will control Y motion
  20. int xposPin = A0; // select the input pin for the potentiometer
  21. int yposPin = A1; // select the input pin for the potentiometer
  22. int Xpos = 0;
  23. int Ypos = 0;
  24. int buttonPin = 7;
  25. int buttonPress = 0;
  26. int laserPin = 6;
  27.  
  28. #include <Wire.h>
  29. #include <LiquidCrystal_I2C.h>
  30.  
  31. // Set the LCD address to 0x27 for a 16 chars and 2 line display
  32. LiquidCrystal_I2C lcd(0x27, 16, 2);
  33.  
  34. void setup() {
  35. Serial.begin(9600);
  36. Serial.println("Robojax.com Laser Module Test");
  37. pinMode(DETECT1, INPUT);//define detect input pin
  38. pinMode(ACTION1, OUTPUT);//define ACTION output pin
  39. pinMode(DETECT2, INPUT);//define detect input pin
  40. pinMode(ACTION2, OUTPUT);//define ACTION output pin
  41. pinMode(DETECT3, INPUT);//define detect input pin
  42. pinMode(ACTION3, OUTPUT);//define ACTION output pin
  43. pinMode(DETECT4, INPUT);//define detect input pin
  44. pinMode(ACTION4, OUTPUT);//define ACTION output pin
  45. pinMode(DETECT5, INPUT);//define detect input pin
  46. pinMode(ACTION5, OUTPUT);//define ACTION output pin
  47.  
  48.  
  49. pinMode(laserPin,OUTPUT); //The laser will be an output
  50. pinMode(buttonPin,INPUT); //The buttonpress pin is an input
  51. digitalWrite(buttonPin,HIGH); //This enables the Arduino pullup for this pin
  52.  
  53. xservo.attach(XServoPin); // attaches the servo on pin 9 to the servo object
  54. yservo.attach(YServoPin); // attaches the servo on pin 8 to the servo object
  55. // initialize the LCD
  56. lcd.begin();
  57.  
  58. lcd.backlight();
  59. lcd.print ("shoot the sensor");
  60.  
  61. }
  62.  
  63.  
  64. void loop() {
  65. buttonPress = digitalRead(buttonPin); //Read the sate of the button
  66. if(buttonPress == LOW) // The button press is active low , so if it is pressed we will turn the laser on
  67. {
  68. digitalWrite(laserPin, HIGH); //Turn laser on
  69. detected = digitalRead(DETECT1);// read Laser sensor
  70.  
  71. if( detected == HIGH)
  72. {
  73. digitalWrite(ACTION1,HIGH);// set the buzzer ON
  74. Serial.println("Detected!");
  75.  
  76. }else{
  77. digitalWrite(ACTION1,LOW); // Set the buzzer OFF
  78. Serial.println("No laser");
  79. // Laser Sensor code for Robojax.com
  80.  
  81. }
  82. detected = digitalRead(DETECT2);// read Laser sensor
  83.  
  84. if( detected == HIGH)
  85. {
  86. digitalWrite(ACTION2,HIGH);// set the buzzer ON
  87. Serial.println("Detected!");
  88.  
  89. }else{
  90. digitalWrite(ACTION2,LOW); // Set the buzzer OFF
  91. Serial.println("No laser");
  92. // Laser Sensor code for Robojax.com
  93. }
  94. detected = digitalRead(DETECT3);// read Laser sensor
  95.  
  96. if( detected == HIGH)
  97. {
  98. digitalWrite(ACTION3,HIGH);// set the buzzer ON
  99. Serial.println("Detected!");
  100.  
  101. }else{
  102. digitalWrite(ACTION3,LOW); // Set the buzzer OFF
  103. Serial.println("No laser");
  104. // Laser Sensor code for Robojax.com
  105. }
  106. detected = digitalRead(DETECT4);// read Laser sensor
  107.  
  108. if( detected == HIGH)
  109. {
  110. digitalWrite(ACTION4,HIGH);// set the buzzer ON
  111. Serial.println("Detected!");
  112.  
  113. }else{
  114. digitalWrite(ACTION4,LOW); // Set the buzzer OFF
  115. Serial.println("No laser");
  116. // Laser Sensor code for Robojax.com
  117. }
  118. detected = digitalRead(DETECT5);// read Laser sensor
  119.  
  120. if( detected == HIGH)
  121. {
  122. digitalWrite(ACTION5,HIGH);// set the buzzer ON
  123. Serial.println("Detected!");
  124.  
  125. }else{
  126. digitalWrite(ACTION5,LOW); // Set the buzzer OFF
  127. Serial.println("No laser");
  128. // Laser Sensor code for Robojax.com
  129. }
  130. delay(200);
  131. } else
  132. {
  133. digitalWrite(laserPin, LOW); // If it is not pressed, keep laser off
  134. }
  135.  
  136.  
  137.  
  138. Xpos = analogRead(xposPin); // read and store the x position location of the joystick
  139. Xpos = map(Xpos,0,1023,0,180); //map the analog read x values to the 0-180 servo values
  140. Ypos = analogRead(yposPin); // read and store the y position of the joystick
  141. Ypos = map(Ypos,0,1023,0,180); //map the analog read y values to the 0-180 servo values
  142. xservo.write(Xpos); // move the X Location servo to the x position the joystick is at
  143. yservo.write(Ypos);
  144. delay(50);
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement