Arduino_XY.ino 3.6 KB

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