xylcd.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <Servo.h>
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. // declare pins for digital input from the RASPBERRY PI
  5. /*/int x_up_pin = 2;
  6. int x_down_pin = 3 ;
  7. int y_up_pin = 4;
  8. int y_down_pin = 5 ;/*/
  9. // declare pins for servo output
  10. int servo_x_Pin = 9;
  11. int servo_y_Pin = 10;
  12. // declare flag pin to send digital output to the RASPBERRY PI
  13. //int signal_pin = 12;
  14. // declare 2 servos to control the 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 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. int actuating = false;
  24. int moving_x = 0;
  25. int moving_y = 0;
  26. // Joystick sensitivity
  27. int threshold = 64;
  28. // Analog input pins for Joystick XY values
  29. int JoyStick_X =0;// pin number for x
  30. int JoyStick_Y =1;// pin number for y
  31. void setup()
  32. {
  33. // communicate to RASPBERRY if actuating a stage move or listening
  34. //pinMode(signal_pin, OUTPUT); // to send 1 bit message to pi
  35. //digitalWrite(signal_pin, LOW);// not actuating
  36. // prepare the servos
  37. Serial.begin(9600);// 9600 bps
  38. servo_x.attach(servo_x_Pin);
  39. servo_y.attach(servo_y_Pin);
  40. // prepare the LCD
  41. lcd.init();
  42. // Turn on the blacklight and print a message.
  43. lcd.backlight();
  44. lcd.setCursor(0,0);
  45. lcd.print("OpenStage");
  46. // position at the center of the scanning area (x,y)=(90,90)
  47. servo_x.write(servo_x_Angle);
  48. servo_y.write(servo_y_Angle);
  49. // RASPBERRY communication pins (4 bits)
  50. /*/pinMode(x_up_pin, INPUT); // GUI button x.step-up
  51. pinMode(x_down_pin, INPUT); // GUI button x.step-down
  52. pinMode(y_up_pin, INPUT); // GUI button y.step-up
  53. pinMode(y_down_pin, INPUT); // GUI button y.step-down
  54. delay(50);/*/
  55. }
  56. void loop()
  57. {
  58. // X movement (signal detection)
  59. // x.up
  60. if(analogRead(JoyStick_X) > (512 + threshold) && actuating == false)
  61. //|| (digitalRead(x_up_pin)== HIGH && actuating == false))
  62. {
  63. if(servo_x_Angle < 180) servo_x_Angle++;
  64. actuating = true;
  65. }
  66. //x.down
  67. if(analogRead(JoyStick_X) < (512 - threshold) && actuating==false)
  68. //||(digitalRead(x_down_pin)== HIGH && actuating == false))
  69. {
  70. if(servo_x_Angle > 0)servo_x_Angle--;
  71. actuating = true;
  72. }
  73. // Y movement (signal detection)
  74. //y.up
  75. if(analogRead(JoyStick_Y) > (512 + threshold)&& actuating==false)
  76. //||(digitalRead(y_up_pin)== HIGH && actuating==false))
  77. {
  78. if(servo_y_Angle < 180) servo_y_Angle++;
  79. actuating= true;
  80. }
  81. //y.down
  82. if(analogRead(JoyStick_Y) < (512 - threshold)&& actuating==false)
  83. //||(digitalRead(y_down_pin)== HIGH && actuating==false))
  84. {
  85. if(servo_y_Angle > 0) servo_y_Angle--;
  86. actuating = true;
  87. }
  88. // Move
  89. if(actuating == true )
  90. {
  91. //digitalWrite(signal_pin, HIGH); // actuating
  92. servo_x.write(servo_x_Angle);
  93. servo_y.write(servo_y_Angle);
  94. delay(50);
  95. actuating = false;
  96. //digitalWrite(signal_pin, LOW); // not actuating
  97. }
  98. lcd.setCursor(0,1);
  99. lcd.print("position:");
  100. lcd.print(servo_x_Angle);
  101. lcd.print(",");
  102. lcd.print(servo_y_Angle);
  103. lcd.print(" ");
  104. delay(50);
  105. }