Good morning. I have used the search function here but have not yet found a good final solution. I hope that you can give me a few tips on how to implement this setup.
I would like to create the following test environment:
- Arduino Mega2560
- one stepper motor C17HD2024-01N
- one TMC2130 stepper driver
- Two spring-loaded micro switches
- One 10k potentiometer (analogue)
- trapezoidal threaded rod
- Slide for trapezoidal thread
This test setup should satisfy this condition:
The carriage should travel a distance "x" (variable) forwards and backwards. The carriage is driven by the stepper motor, which is connected to the trapezoidal threaded rod.
When the carriage hits the micro switch 2, the stepper motor should stop with a delay and rotate in the opposite direction with a further delay. When the carriage hits micro switch 1, the same happens as above.
I would like to use the MoBa Tools database for the Arduino code. Unfortunately, with my currently limited Arduino knowledge, I do not yet know how to integrate the micro button without always having to press it.
I have used this code as "inspiration":
(not yet completely changed)
#define MAX8BUTTONS // spart Speicher, da nur 4 Taster benötigt werden (saves RAM)
#include <MobaTools.h>
const byte dirPin = 5;
const byte stepPin = 6;
const byte enaPin = 7;
const byte button1Pin = A1;
const byte button2Pin = A2;
const byte potPin = A0;
const int STEPS_REVOLUTION = 3200;
MoToStepper myStepper( STEPS_REVOLUTION, STEPDIR );
enum { Button1=0, Button2 } ;
const byte buttonPins[] = { button1Pin, button2Pin };
MoToButtons button( buttonPins, sizeof(buttonPins), 20, 500 );
MoToTimebase speedIntervall;
int vspeed = 0; //Steppergeschwindigkeit in U/min*10
void setup()
{
myStepper.attach( stepPin, dirPin );
myStepper.attachEnable( enaPin, 10, LOW );
myStepper.setSpeed( 200 );
myStepper.setRampLen( 100 );
speedIntervall.setBasetime( 100 );
}
void loop() {
button.processButtons();
if ( speedIntervall.tick() ) {
vspeed = map((analogRead(potPin)), 0, 1023, 20, 1800);
myStepper.setSpeed( vspeed );
}
if (button.pressed(Button1) ) {
myStepper.rotate( 1 ); // Stepper dreht vorwärts
}
if ( button.released(Button1) ) {
//Taster1 losgelassen
myStepper.rotate(0); // Stepper stoppt
}
if (button.pressed(Button2) ) {
//Taster2 gedrĂĽckt
myStepper.rotate( -1 ); // Stepper dreht rückwärts
}
if ( button.released(Button2) ) {
//Taster2 losgelassen
myStepper.rotate(0); // Stepper stoppt
}
}