Arduino_XY.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <Servo.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. /* Declare pins for digital input from the RASPBERRY PI */
  5. const int x_up_pin = 2;
  6. const int x_down_pin = 3 ;
  7. const int y_up_pin = 4;
  8. const int y_down_pin = 5 ;
  9. /* Declare pins for servo output */
  10. const int servo_x_Pin = 9;
  11. const int servo_y_Pin = 10;
  12. /* Analog input pins for Joystick XY values */
  13. const int JoyStick_X = 0; // pin number for x
  14. const int JoyStick_Y = 1; // pin number for y
  15. /* Declare flag pin to send digital output to the RASPBERRY PI */
  16. const int signal_pin = 12;
  17. /* Declare 2 servo objects to control de degrees of freedom of the XY Stage */
  18. Servo servo_x;
  19. Servo servo_y;
  20. /* Set the LCD address to 0x27 for a 16 chars and 2 lines display */
  21. LiquidCrystal_I2C lcd(0x27, 16, 2);
  22. /* Set up the default position of the servos: in the middle */
  23. int servo_x_Angle = 90;
  24. int servo_y_Angle = 90;
  25. /* Actuation flag: not moving */
  26. bool actuating = false;
  27. int moving_x = 0;
  28. int moving_y = 0;
  29. /* Joystick sensitivity */
  30. const int threshold = 64;
  31. void setup()
  32. {
  33. /* Communicate to the RASPBERRY PI if the Arduino is
  34. actuating (the stage is moving) or listening */
  35. pinMode(signal_pin, OUTPUT); // to send 1 bit message to pi
  36. digitalWrite(signal_pin, LOW); // not actuating
  37. /* Prepare the servos */
  38. Serial.begin(9600); // 9600 bps
  39. servo_x.attach(servo_x_Pin);
  40. servo_y.attach(servo_y_Pin);
  41. /* Prepare the LCD */
  42. lcd.begin();
  43. /* Turn the blacklight on and print a message */
  44. lcd.backlight();
  45. lcd.setCursor(0,0);
  46. lcd.print("HomeScope");
  47. lcd.setCursor(0,1);
  48. lcd.print(" by Biotexturas");delay(5000);
  49. /* Position at the center of the scanning area
  50. (x,y) = (90,90) */
  51. servo_x.write(servo_x_Angle);
  52. servo_y.write(servo_y_Angle);
  53. /* Set up the RASPBERRY PI communication pins (4 bits) */
  54. pinMode(x_up_pin, INPUT); // GUI button x.step-up
  55. pinMode(x_down_pin, INPUT); // GUI button x.step-down
  56. pinMode(y_up_pin, INPUT); // GUI button y.step-up
  57. pinMode(y_down_pin, INPUT); // GUI button y.step-down
  58. delay(50);
  59. }
  60. void loop()
  61. {
  62. /* X movement (signal detection) */
  63. /* X.UP */
  64. if ((analogRead(JoyStick_X) > (512 + threshold) && actuating == false)
  65. || (digitalRead(x_up_pin) == HIGH && actuating == false))
  66. {
  67. if(servo_x_Angle < 180) servo_x_Angle++;
  68. actuating = true;
  69. }
  70. /* X.DOWN */
  71. if ((analogRead(JoyStick_X) < (512 - threshold) && actuating==false)
  72. || (digitalRead(x_down_pin) == HIGH && actuating == false))
  73. {
  74. if(servo_x_Angle > 0)servo_x_Angle--;
  75. actuating = true;
  76. }
  77. /* Y movement (signal detection) */
  78. /* Y.UP */
  79. if ((analogRead(JoyStick_Y) > (512 + threshold) && actuating==false)
  80. || (digitalRead(y_up_pin) == HIGH && actuating==false))
  81. {
  82. if (servo_y_Angle < 180 ) {servo_y_Angle++;}
  83. actuating= true;
  84. }
  85. /* Y.DOWN */
  86. if ((analogRead(JoyStick_Y) < (512 - threshold) && actuating==false)
  87. || (digitalRead(y_down_pin) == HIGH && actuating==false))
  88. {
  89. if (servo_y_Angle > 0) {servo_y_Angle--;}
  90. actuating = true;
  91. }
  92. /* Move */
  93. if (actuating == true)
  94. {
  95. digitalWrite(signal_pin, HIGH); // Actuating
  96. servo_x.write(servo_x_Angle);
  97. servo_y.write(servo_y_Angle);
  98. delay(500);
  99. actuating = false;
  100. digitalWrite(signal_pin, LOW); // Not actuating
  101. }
  102. /* Display position info */
  103. lcd.setCursor(0,1);
  104. lcd.print("position:");
  105. lcd.print(servo_x_Angle);
  106. lcd.print(",");
  107. lcd.print(servo_y_Angle);
  108. lcd.print(" ");
  109. delay(50);
  110. }