Hello guys,
I am here to ask for advice after day of struggling and searching all over the internet. Let me just say on the start that I am quite unexperienced in Arduino and coding, even I do some projects from time to time on it.
Now to my project. The main function of my project is stepper motor which goes clockwise and counter-clockwise according the last endstop pressed. To reel spool of filament (from 2pcs of 1kg filament making 1pc of 2kg filament) and to preserve the spacing between filament.
Functionality:
- Start on the Arduino, stepper is turned off
- Press zero button, stepper will go counter-clockwise till it hits the endstop1.
- Press start button, stepper will go clockwise till it hits the endstop2, then go counter-clockwise and so long back and forth until the start button is pressed again and the motor stops.
(Not a topic right now: 4. The speed of stepper motor is controled by potentiometer)
My connection:
stepper + driver: https://www.instructables.com/id/Controll-a-Stepper-Motor-With-the-DRV8825/
note: except the gnd cable from 12v to 5v, all the same
buttons: https://www.instructables.com/id/5-Simple-Button-and-Led-Projects-with-Arduino/
note: except the LED
The only 2 things I could do were that I started Arduino and when one endstop was pressed then the rotation went until the limit of rotation was reached no matter if the other endstop was pressed. Or to just simply set number of degrees to rotate until it stopped. So I could not figure out how to stop the stepper by endstop.
As I said before, I am not much experienced with Arduino, but I would like to ask for advice here with code. Please could anybody help me to edit my code to functionality as written above? I would be very gratefull.
Many thanks, Tom
This is the farest I get today and I am struggling with it :(.
#include <Arduino.h>
#include "A4988.h"
// using a 200-step motor (most common)
#define MOTOR_STEPS 200
#define DIRECTION 8
#define STEP 9
#define MS1 10
#define MS2 11
#define MS3 12
A4988 stepper(MOTOR_STEPS, DIRECTION, STEP, MS1, MS2, MS3);
int ENDSTOP1 = 2;
int ENDSTOP2 = 4;
int ZEROBUTTON = 5;
int STARTSTOP = 6;
void setup() {
// Set target motor RPM to 1RPM and microstepping to 8 (1/8 of full step mode)
stepper.begin(1, 8);
pinMode(DIRECTION, OUTPUT);
digitalWrite(DIRECTION, LOW);
pinMode(ENDSTOP1,INPUT);
pinMode(ENDSTOP2,INPUT);
}
void loop() {
// If button ZEROBUTTON pressed, start stepper counter-clockwise till it hits ENDSTOP1, then stop.
// If STARTSTOP pressed, stepper goes clockwise till hits ENDSTOP2, then switch direction
// to clockwise till hits ENDSTOP1, back and forth till the STARTSTOP is pressed again.
if(digitalRead(ENDSTOP1) == HIGH){
digitalWrite(DIRECTION, HIGH);
stepper.rotate(28.8);
delay(50);
}
if(digitalRead(ENDSTOP2) == HIGH){
digitalWrite(DIRECTION, LOW);
stepper.rotate(-28.8);
delay(50);
}
}