Arduino_XY_no_GUI_no_LCD.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Arduino XY code version with NO Graphical User Interface (GUI) running on the Raspberry Pi
  3. As well as no LCD connected to report coordinates
  4. */
  5. #include <Servo.h>
  6. /* Declare pins for servo output */
  7. const int servo_x_Pin = 9;
  8. const int servo_y_Pin = 10;
  9. /* Analog input pins for Joystick XY values */
  10. const int JoyStick_X = 0; // pin number for x
  11. const int JoyStick_Y = 1; // pin number for y
  12. /* Declare flag pin to send digital output to the RASPBERRY PI */
  13. const int signal_pin = 12;
  14. /* Declare 2 servo objects to control de degrees of freedom of the XY Stage */
  15. Servo servo_x;
  16. Servo servo_y;
  17. /* Set up the default position of the servos: in the middle */
  18. int servo_x_Angle = 90;
  19. int servo_y_Angle = 90;
  20. /* Actuation flag: not moving */
  21. bool actuating = false;
  22. /* Joystick sensitivity */
  23. const int threshold = 64;
  24. void setup()
  25. {
  26. /* Communicate to the RASPBERRY PI if the Arduino is
  27. actuating (the stage is moving) or listening */
  28. pinMode(signal_pin, OUTPUT); // to send 1 bit message to pi
  29. digitalWrite(signal_pin, LOW); // not actuating
  30. /* Prepare the servos */
  31. servo_x.attach(servo_x_Pin);
  32. servo_y.attach(servo_y_Pin);
  33. /* Position at the center of the scanning area
  34. (x,y) = (90,90) */
  35. servo_x.write(servo_x_Angle);
  36. servo_y.write(servo_y_Angle);
  37. delay(50);
  38. }
  39. void loop()
  40. {
  41. /* X movement (signal detection) */
  42. /* X.UP */
  43. if ((analogRead(JoyStick_X) > (512 + threshold)) && actuating == false)
  44. {
  45. if(servo_x_Angle < 180) servo_x_Angle++;
  46. actuating = true;
  47. }
  48. /* X.DOWN */
  49. if ((analogRead(JoyStick_X) < (512 - threshold)) && actuating==false)
  50. {
  51. if(servo_x_Angle > 0)servo_x_Angle--;
  52. actuating = true;
  53. }
  54. /* Y movement (signal detection) */
  55. /* Y.UP */
  56. if ((analogRead(JoyStick_Y) > (512 + threshold)) && actuating==false)
  57. {
  58. if (servo_y_Angle < 180 ) {servo_y_Angle++;}
  59. actuating= true;
  60. }
  61. /* Y.DOWN */
  62. if ((analogRead(JoyStick_Y) < (512 - threshold)) && actuating==false)
  63. {
  64. if (servo_y_Angle > 0) {servo_y_Angle--;}
  65. actuating = true;
  66. }
  67. /* Move */
  68. if (actuating == true)
  69. {
  70. digitalWrite(signal_pin, HIGH); // Actuating
  71. servo_x.write(servo_x_Angle);
  72. servo_y.write(servo_y_Angle);
  73. delay(500);
  74. actuating = false;
  75. digitalWrite(signal_pin, LOW); // Not actuating
  76. }
  77. }