-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton_Controlled_AccelStepper.ino
74 lines (50 loc) · 1.3 KB
/
Button_Controlled_AccelStepper.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Arduino code to control a stepper motor with pushbuttons using a Raspberry Pi Pico or any other microcontroller.
*
* By KushagraK7: https://www.instructables.com/member/KushagraK7/
*
* Feel free to modify the code and tinker around with it and please attribute before publising.
*/
#include<AccelStepper.h>
AccelStepper stepper(AccelStepper :: DRIVER, 16, 17);
#define B1 A2
#define B2 A1
#define B3 A0
int p = 0, pos;
int i = 0;
void setup() {
// put your setup code here, to run once:
pinMode(B1, INPUT_PULLUP);
pinMode(B2, INPUT_PULLUP);
pinMode(B3, INPUT_PULLUP);
stepper.setSpeed(150);
stepper.setAcceleration(5000);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(B1) == LOW) p++;
if(digitalRead(B2) == LOW) p--;
if(digitalRead(B3) == LOW)
{
while(digitalRead(B3) == LOW)
{
i++;
if(i >= 2000) break;
delay(1);
}
if(i >= 2000)
{
i = 0;
stepper.setCurrentPosition(0);
stepper.setSpeed(150);
stepper.runToNewPosition(40);
stepper.runToNewPosition(0);
}
p = 0;
while(digitalRead(B3) == LOW);
}
pos = p/20;
stepper.moveTo(pos);
stepper.run();
Serial.println(pos);
}