23-04-2022, 10:52
Problem polega na tym że są dwie krańcówki jedna odpowiedzialna za min a druga za max i tu pojawia się problem nieważne którą krańcówkę użyje natychmiast zmienia się kierunek ruchu silnik obracając się do krańcówki max reaguje na krańcówke min i zmienia kierunek obrotów silnika na przeciwny w przypadku naruszenia krańcówki max zmienia się kierunek na przeciwny ale następne naruszenie krańcówki max znów zmienia ruch na przeciwny kod w załączniku.
/*
Silnik krokowy z krancowkami
*/
#include <ezButton.h>
#include <AccelStepper.h>
int stepamount = 750;
int steps = stepamount;
int i = 0;
#define DIRECTION_CCW -1
#define DIRECTION_CW 1
#define STATE_CHANGE_DIR 1
#define STATE_MOVE 2
#define STATE_MOVING 3
#define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type)
ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0;
ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1;
AccelStepper stepper(AccelStepper::FULL4WIRE, 7, 6, 5, 4);
int stepperState = STATE_MOVE;
int direction = DIRECTION_CW;
long targetPos = 0;
void setup() {
Serial.begin(9600);
limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds
stepper.setMaxSpeed(5000.0); // set the maximum speed
stepper.setAcceleration(1000); // set acceleration
stepper.setSpeed(1000); // set initial speed
stepper.setCurrentPosition(0); // set position
delay(1000);
}
void loop() {
limitSwitch_1.loop(); // MUST call the loop() function first
limitSwitch_2.loop(); // MUST call the loop() function first
if (limitSwitch_1.isPressed()) {
stepperState = STATE_CHANGE_DIR;
Serial.println(F("The limit switch 1: TOUCHED"));
delay(180000);
}
if (limitSwitch_2.isPressed()) {
stepperState = STATE_CHANGE_DIR;
Serial.println(F("The limit switch 2: TOUCHED"));
//delay(180000);
}
switch (stepperState) {
case STATE_CHANGE_DIR:
direction *= -1; // change direction
Serial.print(F("The direction -> "));
if (direction == DIRECTION_CW)
Serial.println(F("CLOCKWISE"));
else
Serial.println(F("ANTI-CLOCKWISE"));
stepperState = STATE_MOVE; // after changing direction, go to the next state to move the motor
break;
stepper.moveTo(stepamount);
stepper.run();
if (stepper.distanceToGo() == 0) {
if (i % 2) {
stepamount += steps;
}
else {
stepamount -= steps;
}
delay(5000);
i++;
}
loop();
case STATE_MOVE:
targetPos = direction * MAX_POSITION;
stepper.setCurrentPosition(0); // set position
stepper.moveTo(targetPos);
stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity
break;
case STATE_MOVING: // without this state, the move will stop after reaching maximum position
if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position
stepper.setCurrentPosition(0); // reset position to 0
stepper.moveTo(targetPos); // move the motor to maximum position again
}
break;
}
stepper.run(); // MUST be called in loop() function
}
/*
Silnik krokowy z krancowkami
*/
#include <ezButton.h>
#include <AccelStepper.h>
int stepamount = 750;
int steps = stepamount;
int i = 0;
#define DIRECTION_CCW -1
#define DIRECTION_CW 1
#define STATE_CHANGE_DIR 1
#define STATE_MOVE 2
#define STATE_MOVING 3
#define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type)
ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0;
ezButton limitSwitch_2(A1); // create ezButton object that attach to pin A1;
AccelStepper stepper(AccelStepper::FULL4WIRE, 7, 6, 5, 4);
int stepperState = STATE_MOVE;
int direction = DIRECTION_CW;
long targetPos = 0;
void setup() {
Serial.begin(9600);
limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds
stepper.setMaxSpeed(5000.0); // set the maximum speed
stepper.setAcceleration(1000); // set acceleration
stepper.setSpeed(1000); // set initial speed
stepper.setCurrentPosition(0); // set position
delay(1000);
}
void loop() {
limitSwitch_1.loop(); // MUST call the loop() function first
limitSwitch_2.loop(); // MUST call the loop() function first
if (limitSwitch_1.isPressed()) {
stepperState = STATE_CHANGE_DIR;
Serial.println(F("The limit switch 1: TOUCHED"));
delay(180000);
}
if (limitSwitch_2.isPressed()) {
stepperState = STATE_CHANGE_DIR;
Serial.println(F("The limit switch 2: TOUCHED"));
//delay(180000);
}
switch (stepperState) {
case STATE_CHANGE_DIR:
direction *= -1; // change direction
Serial.print(F("The direction -> "));
if (direction == DIRECTION_CW)
Serial.println(F("CLOCKWISE"));
else
Serial.println(F("ANTI-CLOCKWISE"));
stepperState = STATE_MOVE; // after changing direction, go to the next state to move the motor
break;
stepper.moveTo(stepamount);
stepper.run();
if (stepper.distanceToGo() == 0) {
if (i % 2) {
stepamount += steps;
}
else {
stepamount -= steps;
}
delay(5000);
i++;
}
loop();
case STATE_MOVE:
targetPos = direction * MAX_POSITION;
stepper.setCurrentPosition(0); // set position
stepper.moveTo(targetPos);
stepperState = STATE_MOVING; // after moving, go to the next state to keep the motor moving infinity
break;
case STATE_MOVING: // without this state, the move will stop after reaching maximum position
if (stepper.distanceToGo() == 0) { // if motor moved to the maximum position
stepper.setCurrentPosition(0); // reset position to 0
stepper.moveTo(targetPos); // move the motor to maximum position again
}
break;
}
stepper.run(); // MUST be called in loop() function
}