forked from m4k3r-org/simple-stepper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleStepper.cpp
104 lines (87 loc) · 2.35 KB
/
SimpleStepper.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* vim: set ft=c : */
/**
* Library to handle 28byj-48 motor steppers.
*
* https://github.com/minirobots/simple-stepper
*
* Gear Reduction ratio: 1 / 64 (Not really exact: probably 63.68395.:1 )
* Step Angle (Full Step sequence: Internal Motor alone): 11.25° (32 steps per revolution)
* Step Angle (Half Step sequence: Internal Motor alone): 5.625° (64 steps per revolution)
* SO: it takes 32*64 = 2048 steps per output shaft revolution.. In Full Step sequence.
* SO: it takes 64*64 = 4096 steps per output shaft revolution.. In Half Step sequence.
*
* Useful links:
* - 28BYJ-48 Stepper Motor and ULN2003 Driver Intro
* https://youtu.be/B86nqDRskVU
* - 28BYJ-48: Small Stepper Motor and Driver Board
* https://arduino-info.wikispaces.com/SmallSteppers
*
*
* Author: Leo Vidarte <http://nerdlabs.com.ar>
*
* This is free software:
* you can redistribute it and/or modify it
* under the terms of the GPL version 3
* as published by the Free Software Foundation.
*/
#include <SimpleStepper.h>
SimpleStepper::SimpleStepper (uint8_t sequence, bool clockwise,
uint8_t pin1, uint8_t pin2,
uint8_t pin3, uint8_t pin4)
{
_sequence = sequence;
_clockwise = clockwise;
_pins[0] = pin1;
_pins[1] = pin2;
_pins[2] = pin3;
_pins[3] = pin4;
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
}
void SimpleStepper::setStep (int steps)
{
_positive = steps > 0;
_steps = abs(steps);
}
void SimpleStepper::step ()
{
if (_steps)
{
_writeMotor(_actualStep);
_actualStep = _clockwise == _positive
? (_actualStep < 7 ? _actualStep + 1 : 0)
: (_actualStep > 0 ? _actualStep - 1 : 7);
_steps--;
}
}
bool SimpleStepper::ready()
{
return _steps == 0;
}
int SimpleStepper::getFullRotationSteps ()
{
switch (_sequence)
{
case SimpleStepper::WAKEDRIVE:
case SimpleStepper::FULLSTEP :
return STEPS_PER_OUTPUT_REVOLUTION_FS;
case SimpleStepper::HALFSTEP:
return STEPS_PER_OUTPUT_REVOLUTION_HS;
}
return 0;
}
void SimpleStepper::offCoils()
{
for (uint8_t i = 0; i < 4; i++)
digitalWrite(_pins[i], LOW);
}
/** private **/
void SimpleStepper::_writeMotor (int step)
{
for (uint8_t i = 0; i < 4; i++)
{
digitalWrite(_pins[i], bitRead(_motorSequences[_sequence][step], 3-i));
}
}