void movement_clockwise() {
digitalWrite(DIR_PIN, HIGH); // SET DIRECTION
for (int i = 0; i < 10000; i++) {
digitalWrite(PUL_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(PUL_PIN, LOW);
delayMicroseconds(500);
// Check the limit switch during movement
if (digitalRead(LIMIT_SWITCH_1) == LOW) {
delay(WAITING_TIME);
break; // Exit loop if the limit switch is triggered
}
if (digitalRead(SWITCH_1) == HIGH) {
delay(WAITING_TIME);
}
}
}
void movement_counterclockwise() {
digitalWrite(DIR_PIN, LOW); // SET DIRECTION
for (int i = 0; i < 10000; i++) {
digitalWrite(PUL_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(PUL_PIN, LOW);
delayMicroseconds(500);
// Check the limit switch during movement
if (digitalRead(LIMIT_SWITCH_2) == LOW) {
delay(WAITING_TIME);
break; // Exit loop if the limit switch is triggered
}
if (digitalRead(SWITCH_2) == HIGH) {
delay(WAITING_TIME);
}
}
}
void movement_handler() {
if (digitalRead(LIMIT_SWITCH_1) == LOW) { movement_counterclockwise();}
else if (digitalRead(LIMIT_SWITCH_2) == LOW) { movement_clockwise();}
else {movement_clockwise();} // Default movement (e.g., clockwise)
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
Please post your full sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
We need to see a circuit diagram and I don't mean a 'photo of a mess of wires
Please draw the circuit, using pencil and paper is good enough, showing how all components are connected, pin numbers and how the project is powered. Upload a photo of the drawing