You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.5KB

  1. // Include the AccelStepper library:
  2. #include <AccelStepper.h>
  3. // Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
  4. // Set stepper 1 pins
  5. #define m1LimitNegPin 2
  6. #define m1LimitPosPin 3
  7. #define m1DirPin 4
  8. #define m1StepPin 5
  9. #define m1PowerPin 6
  10. // Set stepper 2 pins
  11. #define m2LimitNegPin 9
  12. #define m2LimitPosPin 10
  13. #define m2DirPin 11
  14. #define m2StepPin 12
  15. #define m2PowerPin 13
  16. #define motorInterfaceType 1
  17. // Create a new instance of the AccelStepper class:
  18. AccelStepper m1Stepper = AccelStepper(motorInterfaceType, m1StepPin, m1DirPin);
  19. AccelStepper m2Stepper = AccelStepper(motorInterfaceType, m2StepPin, m2DirPin);
  20. unsigned long previousMillis = 0;
  21. unsigned long currentMillis = 0;
  22. void setup() {
  23. pinMode(m1PowerPin, OUTPUT);
  24. pinMode(m1LimitNegPin, INPUT);
  25. pinMode(m1LimitPosPin, INPUT);
  26. pinMode(m2PowerPin, OUTPUT);
  27. pinMode(m2LimitNegPin, INPUT);
  28. pinMode(m2LimitPosPin, INPUT);
  29. Serial.begin(115200);
  30. // Set the maximum speed in steps per second:
  31. m1Stepper.setMaxSpeed(200);
  32. m1Stepper.setAcceleration(100);
  33. m1Stepper.setCurrentPosition(0);
  34. m2Stepper.setMaxSpeed(200);
  35. m2Stepper.setAcceleration(100);
  36. m2Stepper.setCurrentPosition(0);
  37. }
  38. int integerValue=0;
  39. bool negativeNumber = false; // track if number is negative
  40. char incomingByte;
  41. void loop() {
  42. currentMillis = millis();
  43. int m1EorNeg = digitalRead(m1LimitNegPin);
  44. int m1EorPos = digitalRead(m1LimitPosPin);
  45. int m2EorNeg = digitalRead(m2LimitNegPin);
  46. int m2EorPos = digitalRead(m2LimitPosPin);
  47. if (currentMillis - previousMillis >= 1000 == true ) {
  48. Serial.println("------Stepper 1------");
  49. Serial.print("m1EorPos:");
  50. Serial.println(m1EorNeg);
  51. Serial.print("m1EorNeg: ");
  52. Serial.println(m1EorPos);
  53. Serial.print("m1CurPos: ");
  54. Serial.println(m1Stepper.currentPosition() * -1);
  55. Serial.print("m1TarPos: ");
  56. Serial.println(m1Stepper.targetPosition() * -1);
  57. Serial.println("");
  58. Serial.println("------Stepper 2------");
  59. Serial.print("m2EorPos: ");
  60. Serial.println(m2EorNeg);
  61. Serial.print("m2EorNeg: ");
  62. Serial.println(m2EorPos);
  63. Serial.print("m2CurPos: ");
  64. Serial.println(m2Stepper.currentPosition() * -1);
  65. Serial.print("m2TarPos: ");
  66. Serial.println(m2Stepper.targetPosition() * -1);
  67. Serial.println("");
  68. previousMillis = currentMillis;
  69. }
  70. // limit switch logic for stepper 1
  71. if ((m1EorNeg < m1EorPos) && (m1Stepper.targetPosition() > m1Stepper.currentPosition())) {
  72. m1Stepper.setSpeed(0);
  73. m1Stepper.moveTo(m1Stepper.currentPosition());
  74. digitalWrite(m1PowerPin, HIGH);
  75. } else if ((m1EorNeg > m1EorPos) && (m1Stepper.targetPosition() < m1Stepper.currentPosition())) {
  76. m1Stepper.setSpeed(0);
  77. m1Stepper.moveTo(m1Stepper.currentPosition());
  78. digitalWrite(m1PowerPin, HIGH);
  79. } else if (m1Stepper.targetPosition() == m1Stepper.currentPosition()) {
  80. digitalWrite(m1PowerPin, HIGH);
  81. } else {
  82. digitalWrite(m1PowerPin, LOW);
  83. m1Stepper.run();
  84. }
  85. // limit switch logic for stepper 2
  86. if ((m2EorNeg < m2EorPos) && (m2Stepper.targetPosition() > m2Stepper.currentPosition())) {
  87. m2Stepper.setSpeed(0);
  88. m2Stepper.moveTo(m2Stepper.currentPosition());
  89. digitalWrite(m2PowerPin, HIGH);
  90. } else if ((m2EorNeg > m2EorPos) && (m2Stepper.targetPosition() < m2Stepper.currentPosition())) {
  91. m2Stepper.setSpeed(0);
  92. m2Stepper.moveTo(m1Stepper.currentPosition());
  93. digitalWrite(m2PowerPin, HIGH);
  94. } else if (m2Stepper.targetPosition() == m2Stepper.currentPosition()) {
  95. digitalWrite(m2PowerPin, HIGH);
  96. } else {
  97. digitalWrite(m2PowerPin, LOW);
  98. m2Stepper.run();
  99. }
  100. if (Serial.available() > 0) { // something came across serial
  101. integerValue = 0; // throw away previous integerValue
  102. negativeNumber = false; // reset for negative
  103. while(1) { // force into a loop until 'n' is received
  104. incomingByte = Serial.read();
  105. if (incomingByte == ' ') break; // exit the while(1), we're done receiving
  106. if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
  107. if (incomingByte == '-') {
  108. negativeNumber = true;
  109. continue;
  110. }
  111. integerValue *= 10; // shift left 1 decimal place
  112. integerValue = ((incomingByte - 48) + integerValue); // convert ASCII to integer, add, and shift left 1 decimal place
  113. }
  114. if (negativeNumber)
  115. integerValue = -integerValue;
  116. integerValue = -integerValue; // this makes up for the fact that things are backwards
  117. m1Stepper.moveTo(integerValue);
  118. integerValue = 0; // throw away previous integerValue
  119. negativeNumber = false; // reset for negative
  120. while(1) { // force into a loop until 'n' is received
  121. incomingByte = Serial.read();
  122. if (incomingByte == '\n') break; // exit the while(1), we're done receiving
  123. if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
  124. if (incomingByte == '-') {
  125. negativeNumber = true;
  126. continue;
  127. }
  128. integerValue *= 10; // shift left 1 decimal place
  129. integerValue = ((incomingByte - 48) + integerValue); // convert ASCII to integer, add, and shift left 1 decimal place
  130. }
  131. if (negativeNumber)
  132. integerValue = -integerValue;
  133. integerValue = -integerValue; // this makes up for the fact that things are backwards
  134. m2Stepper.moveTo(integerValue);
  135. }
  136. //delay(100);
  137. }