0% found this document useful (0 votes)
12 views3 pages

Noterparrddddddddhead

Uploaded by

Trường Quang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Noterparrddddddddhead

Uploaded by

Trường Quang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <TimerOne.

h>

// Define pin numbers


const int enablePin = 22;
const int dirPin = 24;
const int pulPin = 26;
const int lmswPin1 = 28; // Limit switch pin 1
const int lmswPin2 = 30; // Limit switch pin 2

// Define motor steps per revolution


const int stepsPerRevolution = 6400;

// Variables to keep track of the motor state


bool isRunning = false;

// Variables to store the current position of the motor


volatile int currentPosition = 0;

// Variable to store the number of steps to send


int stepsToSend = 0;

// Variable to check if limit switch was pressed


volatile bool limitSwitchPressed = false;
volatile bool limitSwitchDirection = true;

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

// Enable the motor


digitalWrite(enablePin, LOW);

// Set initial direction to clockwise


digitalWrite(dirPin, HIGH); // Set dirPin HIGH for clockwise rotation

// Initialize serial communication


Serial.begin(115200);

// Initialize Timer1 with a 100Hz frequency


Timer1.initialize(1000); // 1kHz
Timer1.attachInterrupt(stepMotor);
}

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);
}

// Send acknowledgment back to Python


Serial.println("1");
delay(10);
}

// Check the limit switch state


if (digitalRead(lmswPin1) == LOW && !limitSwitchPressed) {
// When the limit switch 1 is triggered
limitSwitchPressed = true;
limitSwitchDirection = true; // Set direction to clockwise
isRunning = false; // Stop the motor
stepsToSend = 3200; // Set steps to 3200
Timer1.start(); // Start the timer
}

if (digitalRead(lmswPin2) == LOW && !limitSwitchPressed) {


// When the limit switch 2 is triggered
limitSwitchPressed = true;
limitSwitchDirection = false; // Set direction to counterclockwise
isRunning = false; // Stop the motor
stepsToSend = 3200; // Set steps to 3200
Timer1.start(); // Start the timer
}
}

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);

stepCount++; // Increment the step counter


currentPosition += (digitalRead(dirPin) == HIGH) ? 1 : -1; // Update current
position

if (stepCount >= stepsToSend) {


// Stop the motor after sending the required steps
isRunning = false;
limitSwitchPressed = false; // Reset limit switch flag
Timer1.stop();
stepCount = 0; // Reset the step counter
}
}
}

// Function to rotate the motor by a certain angle


void rotateMotor(int angle, bool clockwise) {
int steps = angleToSteps(angle);
isRunning = true;
digitalWrite(dirPin, clockwise ? HIGH : LOW); // Set direction pin
stepsToSend = steps;
Timer1.start();
}

// Function to convert steps to angle


int stepsToAngle(int steps) {
int angle = map(steps, 0, stepsPerRevolution, 0, 360);
return angle;
}

// Function to convert angle to steps


int angleToSteps(int angle) {
float steps = (float)angle / 360.0 * stepsPerRevolution;
return (int)steps;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy