and it is working fine, How ever I want to advance it with first off all when motor is running from UPpos to DOWNpos and button is pressed while motor is running the motor should stop, and also next time I press it I want it to go back to latest position.
When this is done I would like to add some learningfunctions like saving positions to program when holding down button etc. Not so relevant atm.
What approach should I go with here and since it is going to be on for days and days shold I use something like deepsleep and waking it up everytime?
Thanks
maker-20_:
How ever I want to advance it with first off all when motor is running from UPpos to DOWNpos and button is pressed while motor is running the motor should stop, and also next time I press it I want it to go back to latest position.
I'm not sure what you mean by "latest position" - but that is just a detail.
I think you need to change the style of the program. At the moment loop() calls two functions for stepper movement in each of which the button state is checked.
I reckon you need a function that checks the buttonState and decides what should happen. It should set a variable the identifies the state of the system - let's call it stepperPosition. The possible states might be 'T' (moving To Top), 'B' (moving To Bottom), 'S' (stopped), 'U' (up at Top), 'D' (down at bottom) and 'M' somewhere in the middle.
This might do the job, or at least give you some ideas.
void loop() {
checkButton();
moveMotor();
}
void checkButton() {
prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW and prevButtonState == HIGH) {
if (stepperPosition == 'B' or stepperPosition == 'T') { // going to top or to bottom
myStepper.stop();
systemState = 'S';
}
else if (stepperPosition == 'U' or stepperPosition == 'M') {
myStepper.moveTo(DOWNpos);
stepperPosition = 'B';
}
else if (stepperPosition == 'D') {
myStepper.moveTo(UPpos);
stepperPosition = 'T';
}
}
}
void moveMotor() {
myStepper.run();
if (myStepper.distanceToGo() == 0) {
if (myStepper.currentPosition() == DOWNpos) {
stepperPosition = 'D';
}
else if (myStepper.currentPosition() == UPpos) {
stepperPosition = 'U';
}
else {
stepperPosition = 'M'; // somewhere in the middle
}
}
}
Robin2:
I'm not sure what you mean by "latest position" - but that is just a detail.
I think you need to change the style of the program. At the moment loop() calls two functions for stepper movement in each of which the button state is checked.
I reckon you need a function that checks the buttonState and decides what should happen. It should set a variable the identifies the state of the system - let's call it stepperPosition. The possible states might be 'T' (moving To Top), 'B' (moving To Bottom), 'S' (stopped), 'U' (up at Top), 'D' (down at bottom) and 'M' somewhere in the middle.
This might do the job, or at least give you some ideas.
void loop() {
checkButton();
moveMotor();
}
void checkButton() {
prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW and prevButtonState == HIGH) {
if (stepperPosition == 'B' or stepperPosition == 'T') { // going to top or to bottom
myStepper.stop();
systemState = 'S';
}
else if (stepperPosition == 'U' or stepperPosition == 'M') {
myStepper.moveTo(DOWNpos);
stepperPosition = 'B';
}
else if (stepperPosition == 'D') {
myStepper.moveTo(UPpos);
stepperPosition = 'T';
}
}
}
void moveMotor() {
myStepper.run();
if (myStepper.distanceToGo() == 0) {
if (myStepper.currentPosition() == DOWNpos) {
stepperPosition = 'D';
}
else if (myStepper.currentPosition() == UPpos) {
stepperPosition = 'U';
}
else {
stepperPosition = 'M'; // somewhere in the middle
}
}
}
...R
Thank you Robin! It was something like this I was thinking!
Okey so I used @Robin2 s approach and now got this
#include <AccelStepper.h>
#define UPpos 0
#define DOWNpos 5000
#define FULLSTEP 4
int systemState;
int stepperPosition;
const int buttonPin = 2;
byte buttonState;
byte prevButtonState;
//const int interval = 100;
int latest;
// IN1-IN3-IN2-IN4
AccelStepper myStepper(FULLSTEP, 8, 10, 9, 11);
void setup() {
myStepper.setMaxSpeed(800.0);
myStepper.setAcceleration(300.0);
myStepper.setSpeed(200);
myStepper.setCurrentPosition(0);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
checkButton();
moveMotor();
}
void checkButton() {
prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW and prevButtonState == HIGH) {
if (stepperPosition == 'B' or stepperPosition == 'T') { // going to top or to bottom
myStepper.stop();
systemState = 'S';
}
else if (stepperPosition == 'U') {
myStepper.moveTo(DOWNpos);
stepperPosition = 'B';
latest = UPpos;
}
else if (stepperPosition == 'D') {
myStepper.moveTo(UPpos);
stepperPosition = 'T';
latest = DOWNpos;
}
else if (stepperPosition == 'M'){
myStepper.moveTo(latest);
}
}
}
void moveMotor() {
myStepper.run();
if (myStepper.distanceToGo() == UPpos) {
if (myStepper.currentPosition() == DOWNpos) {
stepperPosition = 'D';
myStepper.disableOutputs();
}
else if (myStepper.currentPosition() == UPpos) {
stepperPosition = 'U';
myStepper.disableOutputs();
}
else {
stepperPosition = 'M'; // somewhere in the middle
myStepper.disableOutputs();
}
}
}
This code is working great, I would like to add a learning program for it where I can hold down the button 5 sec and it goes into programming/learning mode. Assuming the rollerblind is at the highest point and you hold down the button until it reaches the DOWNpos. Maybe saving this to the arduinos memory so that the device can be turned off and still know the latest pos and also the range that it should be. Is this possible?
maker-20_:
This code is working great, I would like to add a learning program for it where I can hold down the button 5 sec and it goes into programming/learning mode. Assuming the rollerblind is at the highest point and you hold down the button until it reaches the DOWNpos. Maybe saving this to the arduinos memory so that the device can be turned off and still know the latest pos and also the range that it should be. Is this possible?
I would strongly recommend a separate button (or better still a switch) to select programming mode.
You could save values to the Arduino's EEPROM memory.
You must also keep in mind that the Arduino has no idea where the stepper motor is when the program starts. The usual thing is to have a limit switch and move the motor until it triggers the limit switch to identify the ZERO or HOME position. The simplest thing might be to have limit switches at the top and the bottom.