Arduino_Z.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. const int top_range_pin = 6;
  7. const int bottom_range_pin = 7;
  8. /* Easy Driver pins */
  9. const int driver_dir = 8;
  10. const int driver_stp = 9;
  11. /* Physical buttons */
  12. const int button_up = 2;
  13. const int button_down = 3;
  14. /* GUI buttons */
  15. const int gui_up = 4;
  16. const int gui_down = 5;
  17. void setup()
  18. {
  19. /* Report state of automata: listening or stepping? */
  20. pinMode(signal_pin, OUTPUT);
  21. digitalWrite(signal_pin, LOW);
  22. /* Set range pins as inputs to read range sensors.
  23. Stage will move only if sensors are high */
  24. pinMode(top_range_pin, INPUT);
  25. pinMode(bottom_range_pin, INPUT);
  26. /* Pull up both UP and DOWN range pins */
  27. digitalWrite(top_range_pin, HIGH);
  28. digitalWrite(bottom_range_pin, HIGH);
  29. /* Easy Driver
  30. To control direction. Set as LOW for UP direction and HIGH for DOWN direction */
  31. pinMode(driver_dir, OUTPUT);
  32. /* To step one step */
  33. pinMode(driver_stp, OUTPUT);
  34. digitalWrite(driver_dir, LOW); // Starting direction is UP
  35. digitalWrite(driver_stp, LOW); // No-step
  36. /* Up */
  37. pinMode(button_up, INPUT); // physical button step-up
  38. pinMode(gui_up, INPUT); // GUI button step-up
  39. /* Down */
  40. pinMode(button_down, INPUT); // physical button step-down
  41. pinMode(gui_down, INPUT); // GUI button step-down
  42. }
  43. void loop()
  44. {
  45. /* Read range sensors */
  46. bool value_top = digitalRead(top_range_pin);
  47. bool value_bottom = digitalRead(bottom_range_pin);
  48. if (!value_top || !value_bottom)
  49. {
  50. Serial.println("Stage out of range");
  51. }
  52. /* Check up direction with */
  53. if ((digitalRead(button_up) == LOW || digitalRead(gui_up) == HIGH)
  54. && (Stepping == false && value_top))
  55. {
  56. digitalWrite(driver_dir, LOW);
  57. Stepping = true;
  58. digitalWrite(signal_pin, HIGH);
  59. }
  60. /* Check down direction with */
  61. if ((digitalRead(button_down) == LOW || digitalRead(gui_down) == HIGH)
  62. && (Stepping == false && value_bottom))
  63. {
  64. digitalWrite(driver_dir, HIGH);
  65. Stepping = true;
  66. digitalWrite(signal_pin, HIGH);
  67. }
  68. /* If wished, go ahead and step */
  69. if (Stepping == true)
  70. {
  71. digitalWrite(driver_stp, HIGH);
  72. delay(1);
  73. digitalWrite(driver_stp, LOW);
  74. delay(1);
  75. StepCounter += 1;
  76. if (StepCounter == DISTANCE)
  77. {
  78. StepCounter = 0;
  79. Stepping = false;
  80. digitalWrite(signal_pin, LOW);
  81. }
  82. }
  83. }