this first post. if i'm in wrong place tell me post.
the following code runs cw 2 turns , ccw 2 turns forever.
i'm trying add , down buttons simple elevator. i'm sure simple if know code.
i know electronics don't know programming. have commented out attempt @ adding code buttons. can make them work not should. on issue great.
#include <accelstepper.h>
#define halfstep 8
#define motorpin1 8 // in1 on uln2003 ==> blue on 28byj-48
#define motorpin2 9 // in2 on uln2004 ==> pink on 28byj-48
#define motorpin3 10 // in3 on uln2003 ==> yellow on 28byj-48
#define motorpin4 11 // in4 on uln2003 ==> orange on 28byj-48
const int down_button = 2; //set our button constants
const int up_button = 3; //set our button constants
const int up_limit = 4;
const int down_limit = 5;
const int downled = 12;
const int upled = 13;
int endpoint = 8192; // move many steps -
//1024 = approx 1/4 turn
//8192 = 2 turns
// note: sequence 1-3-2-4 required proper sequencing of 28byj-48
accelstepper stepper1(halfstep, motorpin1, motorpin3, motorpin2, motorpin4);
void setup()
{
serial.begin(9600);
serial.println(stepper1.currentposition());
delay(5000);
pinmode(down_button, input); //set i/o
pinmode(up_button, input); //set i/o
pinmode(downled, output); //setup our down led
pinmode(upled, output); //setup our led
stepper1.setmaxspeed(1000.0);
stepper1.setacceleration(100.0);
stepper1.setspeed(200);
}
void loop()
{
// if (digitalread(down_button) == low) // lets go down
stepper1.moveto(endpoint);
// if (stepper1.distancetogo() == 0 && digitalread(up_button) == low) // lets go up
if (stepper1.distancetogo() == 0)
{
serial.println(stepper1.currentposition());
stepper1.setcurrentposition(0);
endpoint = -endpoint;
stepper1.moveto(endpoint);
serial.println(stepper1.currentposition());
}
stepper1.run();
}
the following code runs cw 2 turns , ccw 2 turns forever.
i'm trying add , down buttons simple elevator. i'm sure simple if know code.
i know electronics don't know programming. have commented out attempt @ adding code buttons. can make them work not should. on issue great.
#include <accelstepper.h>
#define halfstep 8
#define motorpin1 8 // in1 on uln2003 ==> blue on 28byj-48
#define motorpin2 9 // in2 on uln2004 ==> pink on 28byj-48
#define motorpin3 10 // in3 on uln2003 ==> yellow on 28byj-48
#define motorpin4 11 // in4 on uln2003 ==> orange on 28byj-48
const int down_button = 2; //set our button constants
const int up_button = 3; //set our button constants
const int up_limit = 4;
const int down_limit = 5;
const int downled = 12;
const int upled = 13;
int endpoint = 8192; // move many steps -
//1024 = approx 1/4 turn
//8192 = 2 turns
// note: sequence 1-3-2-4 required proper sequencing of 28byj-48
accelstepper stepper1(halfstep, motorpin1, motorpin3, motorpin2, motorpin4);
void setup()
{
serial.begin(9600);
serial.println(stepper1.currentposition());
delay(5000);
pinmode(down_button, input); //set i/o
pinmode(up_button, input); //set i/o
pinmode(downled, output); //setup our down led
pinmode(upled, output); //setup our led
stepper1.setmaxspeed(1000.0);
stepper1.setacceleration(100.0);
stepper1.setspeed(200);
}
void loop()
{
// if (digitalread(down_button) == low) // lets go down
stepper1.moveto(endpoint);
// if (stepper1.distancetogo() == 0 && digitalread(up_button) == low) // lets go up
if (stepper1.distancetogo() == 0)
{
serial.println(stepper1.currentposition());
stepper1.setcurrentposition(0);
endpoint = -endpoint;
stepper1.moveto(endpoint);
serial.println(stepper1.currentposition());
}
stepper1.run();
}
here same code in code tags - easy read , copy
you should reorganize code loop() this
and put of code in movemotor() function. move end-point. code in function updateendpoint() change destination motor when appropriate - i.e. after appropriate button has been pressed.
see planning , implementing program
...r
code: [select]
#include <accelstepper.h>
#define halfstep 8
#define motorpin1 8 // in1 on uln2003 ==> blue on 28byj-48
#define motorpin2 9 // in2 on uln2004 ==> pink on 28byj-48
#define motorpin3 10 // in3 on uln2003 ==> yellow on 28byj-48
#define motorpin4 11 // in4 on uln2003 ==> orange on 28byj-48
const int down_button = 2; //set our button constants
const int up_button = 3; //set our button constants
const int up_limit = 4;
const int down_limit = 5;
const int downled = 12;
const int upled = 13;
int endpoint = 8192; // move many steps -
//1024 = approx 1/4 turn
//8192 = 2 turns
// note: sequence 1-3-2-4 required proper sequencing of 28byj-48
accelstepper stepper1(halfstep, motorpin1, motorpin3, motorpin2, motorpin4);
void setup()
{
serial.begin(9600);
serial.println(stepper1.currentposition());
delay(5000);
pinmode(down_button, input); //set i/o
pinmode(up_button, input); //set i/o
pinmode(downled, output); //setup our down led
pinmode(upled, output); //setup our led
stepper1.setmaxspeed(1000.0);
stepper1.setacceleration(100.0);
stepper1.setspeed(200);
}
void loop()
{
// if (digitalread(down_button) == low) // lets go down
stepper1.moveto(endpoint);
// if (stepper1.distancetogo() == 0 && digitalread(up_button) == low) // lets go up
if (stepper1.distancetogo() == 0)
{
serial.println(stepper1.currentposition());
stepper1.setcurrentposition(0);
endpoint = -endpoint;
stepper1.moveto(endpoint);
serial.println(stepper1.currentposition());
}
stepper1.run();
}
quote
code runs cw 2 turns , ccw 2 turns forever.that's program tells do.
you should reorganize code loop() this
code: [select]
void loop() {
readbuttons();
updateendpoint();
movemotor();
}
and put of code in movemotor() function. move end-point. code in function updateendpoint() change destination motor when appropriate - i.e. after appropriate button has been pressed.
see planning , implementing program
...r
Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > Need help with AccelStepper.h code. Simple elevator with up and down buttons
arduino
Comments
Post a Comment