mkebab 1 год назад
Родитель
Сommit
8e4662860f
1 измененных файлов с 60 добавлено и 0 удалено
  1. 60 0
      Stepper_Rotation.ino

+ 60 - 0
Stepper_Rotation.ino

@@ -0,0 +1,60 @@
+#include <AccelStepper.h>
+
+// Define the pinss
+#define STEP_PIN1 5
+#define DIR_PIN1 4
+#define ENABLE_PIN1 6
+
+// Define the steps per revolution of your stepper motor
+const float STEPS_PER_REVOLUTION = 200.0;
+float i = 20;  //Gear Transmission
+
+// Create an instance of the AccelStepper class
+AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN1, DIR_PIN1);
+
+void setup() {
+
+  Serial.begin(9600);  // Set the baud rate to the same value used in Python
+
+  // Set the maximum speed and acceleration (you can adjust these values)
+  stepper.setMaxSpeed(400);
+  stepper.setAcceleration(100.0);
+
+  // Set the enable pin as an output and disable the motor by default
+  pinMode(ENABLE_PIN1, OUTPUT);
+  digitalWrite(ENABLE_PIN1, HIGH);
+
+  // Set the motor direction (0 for clockwise, 1 for counter-clockwise)
+  stepper.setPinsInverted(false, false, true);  // Adjust the last parameter if needed
+}
+
+void loop() {
+  if (Serial.available() > 0) {
+    // Read the incoming value from Python
+    int value = Serial.parseInt();
+    if ((value > -125) && (value < 125)) {
+        rotateStepper(value * i);
+        Serial.println(value);
+      }
+  }
+  delay(1000);
+}
+
+void rotateStepper(float degrees) {
+  // Calculate the number of steps based on the desired angle
+  float steps = degrees * (STEPS_PER_REVOLUTION / 360.0);
+
+  // Move the stepper to the specified number of steps
+  stepper.move(steps);
+
+  // Enable the motor
+  digitalWrite(ENABLE_PIN1, LOW);
+
+  // Run the stepper until it reaches the target position
+  while (stepper.distanceToGo() != 0) {
+    stepper.run();
+  }
+
+  // Disable the motor
+  digitalWrite(ENABLE_PIN1, HIGH);
+}