Arduino_XY_no_GUI.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <Servo.h>
  2. // Note that as we are using the Servo.h library
  3. // We now need a LiquidCrystal I2C libbrary which is compatible
  4. #include <LiquidCrystal_I2C_Hangul.h>
  5. // therefore we use Hangul and not de Brabander
  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 the LCD address to 0x27 for a 16 chars and 2 lines display */
  18. LiquidCrystal_I2C_Hangul lcd(0x27, 16, 2);
  19. /* Set up the default position of the servos: in the middle */
  20. int servo_x_Angle = 90;
  21. int servo_y_Angle = 90;
  22. /* Actuation flag: not moving */
  23. bool actuating = false;
  24. /* Joystick sensitivity */
  25. const int threshold = 64;
  26. void setup()
  27. {
  28. /* Communicate to the RASPBERRY PI if the Arduino is
  29. actuating (the stage is moving) or listening */
  30. pinMode(signal_pin, OUTPUT); // to send 1 bit message to pi
  31. digitalWrite(signal_pin, LOW); // not actuating
  32. /* Prepare the servos */
  33. servo_x.attach(servo_x_Pin);
  34. servo_y.attach(servo_y_Pin);
  35. /* Prepare the LCD */
  36. lcd.init();
  37. /* Turn the blacklight on and print a message */
  38. lcd.backlight();
  39. lcd.setCursor(0,0);
  40. lcd.print("HomeScope");
  41. lcd.setCursor(0,1);
  42. lcd.print(" by Biotexturas");delay(5000);
  43. /* Position at the center of the scanning area
  44. (x,y) = (90,90) */
  45. servo_x.write(servo_x_Angle);
  46. servo_y.write(servo_y_Angle);
  47. delay(50);
  48. }
  49. void loop()
  50. {
  51. /* X movement (signal detection) */
  52. /* X.UP */
  53. if ((analogRead(JoyStick_X) > (512 + threshold)) && actuating == false)
  54. {
  55. if(servo_x_Angle < 180) servo_x_Angle++;
  56. actuating = true;
  57. }
  58. /* X.DOWN */
  59. if ((analogRead(JoyStick_X) < (512 - threshold)) && actuating==false)
  60. {
  61. if(servo_x_Angle > 0)servo_x_Angle--;
  62. actuating = true;
  63. }
  64. /* Y movement (signal detection) */
  65. /* Y.UP */
  66. if ((analogRead(JoyStick_Y) > (512 + threshold)) && actuating==false)
  67. {
  68. if (servo_y_Angle < 180 ) {servo_y_Angle++;}
  69. actuating= true;
  70. }
  71. /* Y.DOWN */
  72. if ((analogRead(JoyStick_Y) < (512 - threshold)) && actuating==false)
  73. {
  74. if (servo_y_Angle > 0) {servo_y_Angle--;}
  75. actuating = true;
  76. }
  77. /* Move */
  78. if (actuating == true)
  79. {
  80. digitalWrite(signal_pin, HIGH); // Actuating
  81. servo_x.write(servo_x_Angle);
  82. servo_y.write(servo_y_Angle);
  83. delay(500);
  84. actuating = false;
  85. digitalWrite(signal_pin, LOW); // Not actuating
  86. }
  87. /* Display position info */
  88. lcd.setCursor(0,1);
  89. lcd.print("position:");
  90. lcd.print(servo_x_Angle);
  91. lcd.print(",");
  92. lcd.print(servo_y_Angle);
  93. lcd.print(" ");
  94. delay(50);
  95. }