Arduino_Z_no_GUI.ino 2.4 KB

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