0% found this document useful (0 votes)
9 views

Codearrduinohead

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)
9 views

Codearrduinohead

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 = 48;
const int pulPin = 50;
const int lmswPin1 = 44; // Limit switch pin 1
const int lmswPin2 = 46; // Limit switch pin 2

// Define motor steps per revolution


const int stepsPerRevolution = 6400; // Replace 6400 with the actual steps per
revolution of your motor

// Variables to keep track of the motor state


volatile 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


volatile int stepsToSend = 0;

// Variable to check if limit switch was pressed


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

// Variables to store the previous state of the limit switches


bool previousLmswState1 = HIGH;
bool previousLmswState2 = HIGH;

// Flag to indicate if handleLimitSwitch is being executed


volatile String cocheck = "off";

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

Serial.begin(115200);

Timer1.initialize(10000);
Timer1.attachInterrupt(stepMotor);

rotateMotor(true);
}
String previousCocheck = "off";
void loop() {

String cocheckCopy = String(cocheck.c_str());


if (cocheckCopy != previousCocheck) {

Serial.println(cocheckCopy);

previousCocheck = cocheckCopy;
}
if (cocheckCopy == "on" && Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
Serial.println("ACK");
if (command == "a") {

rotateMotor(true);
} else if (command == "b") {

rotateMotor(false);
} else if (command == "c" || command == "None") {
// Stop the motor
isRunning = false;
Timer1.stop(); // Stop the timer interrupt
}

delay(10);
}

// Check the limit switch state in the main loop


bool currentLmswState1 = digitalRead(lmswPin1);
if (currentLmswState1 == LOW && previousLmswState1 == HIGH) {
handleLimitSwitch(true);
}
previousLmswState1 = currentLmswState1;

bool currentLmswState2 = digitalRead(lmswPin2);


if (currentLmswState2 == LOW && previousLmswState2 == HIGH) {
handleLimitSwitch(false);
}
previousLmswState2 = currentLmswState2;
}

void stepMotor() {
// Check the limit switch state
bool currentLmswState1 = digitalRead(lmswPin1);
if (currentLmswState1 == LOW && previousLmswState1 == HIGH) {
handleLimitSwitch(true);
cocheck = "on";
}
previousLmswState1 = currentLmswState1;
bool currentLmswState2 = digitalRead(lmswPin2);
if (currentLmswState2 == LOW && previousLmswState2 == HIGH) {
handleLimitSwitch(false);
cocheck = "on";
}
previousLmswState2 = currentLmswState2;

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

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


position

if (--stepsToSend <= 0) {
// Stop the motor after sending the required steps
isRunning = false;
limitSwitchPressed = false; // Reset limit switch flag
Timer1.stop();
cocheck = "on";
}
}
}

// Function to handle limit switch


void handleLimitSwitch(bool direction) {
limitSwitchPressed = true;
limitSwitchDirection = direction;
currentPosition = direction ? 0 : stepsPerRevolution; // Reset currentPosition
based on direction

// Set the new number of steps here, for example:


stepsToSend = 3200; // Change this value to the new number of steps you want to
move when hitting the limit

isRunning = false; // Stop the motor


Timer1.start(); // Start the timer
cocheck = "off";
// After finishing the limit switch handling, reset cocheck
}

// Function to rotate the motor


void rotateMotor(bool clockwise) {
isRunning = true;
digitalWrite(dirPin, clockwise ? HIGH : LOW); // Set direction pin
stepsToSend = stepsPerRevolution; // Set steps to full revolution
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