Noterparrddddddddhead
Noterparrddddddddhead
h>
void setup() {
// Set pin modes
pinMode(enablePin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(pulPin, OUTPUT);
pinMode(lmswPin1, INPUT_PULLUP); // Use internal pull-up resistor for limit
switch pin 1
pinMode(lmswPin2, INPUT_PULLUP); // Use internal pull-up resistor for limit
switch pin 2
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n'); // Read the command from serial
if (command == "a") {
// Rotate the motor clockwise by 90 degrees
rotateMotor(180, true);
} else if (command == "b") {
// Rotate the motor counterclockwise by 90 degrees
rotateMotor(180, false);
} else if (command == "c" || command == "None") {
// Stop the motor
isRunning = false;
Timer1.stop(); // Stop the timer interrupt
currentPosition = 0; // Reset the current position
} else if (command == "getAngle") {
// Get the current angle and send it back to Python
int currentAngle = stepsToAngle(currentPosition);
Serial.println(currentAngle);
}
void stepMotor() {
static int stepCount = 0; // Counter to track the number of steps
if (isRunning || limitSwitchPressed) {
// Check limit switch state
if (limitSwitchPressed) {
// Set direction pin based on limit switch direction
digitalWrite(dirPin, limitSwitchDirection ? HIGH : LOW);
}
// Generate a pulse
digitalWrite(pulPin, HIGH);
delayMicroseconds(5);
digitalWrite(pulPin, LOW);
delayMicroseconds(5);