Arduino_Z.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #define DISTANCE 1 // Define the discrete length of a step
  2. int StepCounter = 0;
  3. bool Stepping = false;
  4. /* Declare flag pin to send digital output to the RASPBERRY PI */
  5. const int signal_pin = 12;
  6. // Declare pins to set ranges so stage is protected within the Frame
  7. const int top_range_pin = 6;
  8. const int bottom_range_pin = 7;
  9. /* Easy Driver pins */
  10. const int driver_dir = 8;
  11. const int driver_stp = 9;
  12. /* Physical buttons */
  13. const int button_up = 2;
  14. const int button_down = 3;
  15. /* GUI buttons */
  16. // To use for communication from the Raspberry Pi via Arduino's GPIOs
  17. // Note that to stay put the pins should be grounded.
  18. // The Raspberry therefore need to send GND via logical convertors to these pins in the Ardu
  19. // By default the pins might be not grounded so they must be grounded before connecting
  20. const int gui_up = 4;
  21. const int gui_down = 5;
  22. void setup()
  23. {
  24. /* Report state of automata: listening or stepping? */
  25. pinMode(signal_pin, OUTPUT);
  26. digitalWrite(signal_pin, LOW);
  27. /* Set range pins as inputs to read range sensors.
  28. Stage will move only if sensors are high */
  29. pinMode(top_range_pin, INPUT);
  30. pinMode(bottom_range_pin, INPUT);
  31. /* Pull up both UP and DOWN range pins */
  32. digitalWrite(top_range_pin, HIGH);
  33. digitalWrite(bottom_range_pin, HIGH);
  34. /* Easy Driver
  35. To control direction. Set as LOW for UP direction and HIGH for DOWN direction */
  36. pinMode(driver_dir, OUTPUT);
  37. /* To step one step */
  38. pinMode(driver_stp, OUTPUT);
  39. digitalWrite(driver_dir, LOW); // Starting direction is UP
  40. digitalWrite(driver_stp, LOW); // No-step
  41. /* Up */
  42. pinMode(button_up, INPUT); // physical button step-up
  43. pinMode(gui_up, INPUT); // GUI button step-up
  44. /* Down */
  45. pinMode(button_down, INPUT); // physical button step-down
  46. pinMode(gui_down, INPUT); // GUI button step-down
  47. }
  48. void loop()
  49. {
  50. /* Read range sensors */
  51. bool value_top = digitalRead(top_range_pin);
  52. bool value_bottom = digitalRead(bottom_range_pin);
  53. if (!value_top || !value_bottom)
  54. {
  55. Serial.println("Stage out of range");
  56. }
  57. /* Check up direction with */
  58. if ((digitalRead(button_up) == LOW || digitalRead(gui_up) == HIGH)
  59. && (Stepping == false && value_top))
  60. {
  61. digitalWrite(driver_dir, LOW);
  62. Stepping = true;
  63. digitalWrite(signal_pin, HIGH);
  64. }
  65. /* Check down direction with */
  66. if ((digitalRead(button_down) == LOW || digitalRead(gui_down) == HIGH)
  67. && (Stepping == false && value_bottom))
  68. {
  69. digitalWrite(driver_dir, HIGH);
  70. Stepping = true;
  71. digitalWrite(signal_pin, HIGH);
  72. }
  73. /* If wished, go ahead and step */
  74. if (Stepping == true)
  75. {
  76. digitalWrite(driver_stp, HIGH);
  77. delay(1);
  78. digitalWrite(driver_stp, LOW);
  79. delay(1);
  80. StepCounter += 1;
  81. if (StepCounter == DISTANCE)
  82. {
  83. StepCounter = 0;
  84. Stepping = false;
  85. digitalWrite(signal_pin, LOW);
  86. }
  87. }
  88. }