Advertisement
Guest User

SWH - snack2go code

a guest
Sep 11th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. // ConstantSpeed.pde
  2. // -*- mode: C++ -*-
  3. //
  4. // Shows how to run AccelStepper in the simplest,
  5. // fixed speed mode with no accelerations
  6. // MultiStepper.pde
  7. // -*- mode: C++ -*-
  8. // Use MultiStepper class to manage multiple steppers and make them all move to
  9. // the same position at the same time for linear 2d (or 3d) motion.
  10.  
  11. #include <AccelStepper.h>
  12. #include <MultiStepper.h>
  13. #include <NewPing.h> //Sensor einbinden
  14.  
  15. #define X_STEP_PIN (byte)26
  16. #define X_DIR_PIN (byte)28
  17. #define X_ENABLE_PIN (byte)24
  18.  
  19. #define Y_STEP_PIN (byte)38
  20. #define Y_DIR_PIN (byte)40
  21. #define Y_ENABLE_PIN (byte)36
  22.  
  23. #define Z_STEP_PIN (byte)44
  24. #define Z_DIR_PIN (byte)46
  25. #define Z_ENABLE_PIN (byte)42
  26.  
  27. // defines pins numbers
  28. const int trigPin = 23;
  29. const int echoPin = 53;
  30.  
  31.  
  32. // defines variables
  33. long duration;
  34.  
  35. // EG X-Y position bed driven by 2 steppers
  36. // Alas its not possible to build an array of these with different pins for each :-(
  37. AccelStepper stepper1(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN);
  38. AccelStepper stepper2(AccelStepper::DRIVER, Z_STEP_PIN, Z_DIR_PIN);
  39.  
  40. // Up to 10 steppers can be handled as a group by MultiStepper
  41. MultiStepper steppers;
  42.  
  43. //-----------------------------
  44. void setup() {
  45.  
  46. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  47. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  48.  
  49. Serial.begin(9600); // Starts the serial communication
  50.  
  51.  
  52. // Configure each stepper
  53. pinMode(X_ENABLE_PIN , OUTPUT);
  54. pinMode(Z_ENABLE_PIN , OUTPUT);
  55.  
  56. digitalWrite(X_ENABLE_PIN , HIGH);
  57. digitalWrite(Z_ENABLE_PIN , HIGH);
  58.  
  59.  
  60. stepper1.setMaxSpeed(400);
  61. stepper2.setMaxSpeed(100);
  62. stepper1.setAcceleration(10);
  63. stepper2.setAcceleration(10);
  64.  
  65. // Then give them to MultiStepper to manage
  66. steppers.addStepper(stepper1);
  67. steppers.addStepper(stepper2);
  68. }
  69. //--------------------------------------
  70. void moveMotors(long x, long y) {
  71.  
  72. long positions[2]; // Array of desired stepper positions
  73.  
  74. positions[0] = x + y;
  75. positions[1] = x - y;
  76.  
  77. // Enable!
  78. digitalWrite(X_ENABLE_PIN , LOW);
  79. digitalWrite(Z_ENABLE_PIN , LOW);
  80.  
  81. steppers.moveTo(positions);
  82. steppers.runSpeedToPosition(); // Blocks until all are in position
  83.  
  84. // Disable!
  85. digitalWrite(X_ENABLE_PIN , HIGH);
  86. digitalWrite(Z_ENABLE_PIN , HIGH);
  87.  
  88. }
  89. //---------------------------------
  90. void loop() {
  91.  
  92. int d;
  93. d = getDist();
  94.  
  95. // Prints the distance on the Serial Monitor
  96. Serial.print("Distance: ");
  97. Serial.println(d);
  98.  
  99. if (d < 20) {
  100. moveMotors(300, -400); // digitalWrite(ledPin1, HIGH);
  101. Serial.println("Motor backwards"); // Then the Motor turns on
  102. delay(1000);
  103.  
  104. } else {
  105.  
  106. moveMotors(-100, 100); //digitalWrite(ledPin1, LOW);
  107. Serial.println("Motor forwards"); // Prints Motor Off
  108. delay(1000);
  109. }
  110. }
  111. //-----------------------------------
  112. int getDist() {
  113.  
  114. int distance;
  115.  
  116. // Clears the trigPin
  117. digitalWrite(trigPin, LOW);
  118. delayMicroseconds(2);
  119.  
  120. // Sets the trigPin on HIGH state for 10 micro seconds
  121. digitalWrite(trigPin, HIGH);
  122. delayMicroseconds(10);
  123. digitalWrite(trigPin, LOW);
  124.  
  125. // Reads the echoPin, returns the sound wave travel time in microseconds
  126. duration = pulseIn(echoPin, HIGH);
  127.  
  128. // Calculating the distance
  129. distance = duration * 0.034 / 2;
  130. return distance;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement