Library to control 4 phase stepper motors using ULN2003. Default values for motor 28BYJ-48.

Dependents:   Lift_arm_control

Committer:
fbcosentino
Date:
Tue Mar 05 13:18:24 2019 +0000
Revision:
0:8a14924886c4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fbcosentino 0:8a14924886c4 1 #include "ULN2003.h"
fbcosentino 0:8a14924886c4 2
fbcosentino 0:8a14924886c4 3 ULN2003::ULN2003(PinName pinOut1, PinName pinOut2, PinName pinOut3, PinName pinOut4, int full_turn_steps):
fbcosentino 0:8a14924886c4 4 _out1(pinOut1), _out2(pinOut2), _out3(pinOut3), _out4(pinOut4) {
fbcosentino 0:8a14924886c4 5 _pos = 0;
fbcosentino 0:8a14924886c4 6 _full_turn_steps = full_turn_steps;
fbcosentino 0:8a14924886c4 7 relax();
fbcosentino 0:8a14924886c4 8 }
fbcosentino 0:8a14924886c4 9
fbcosentino 0:8a14924886c4 10 void ULN2003::_set_output() {
fbcosentino 0:8a14924886c4 11 switch (_pos) {
fbcosentino 0:8a14924886c4 12 case 0: {
fbcosentino 0:8a14924886c4 13 _out1=1;
fbcosentino 0:8a14924886c4 14 _out2=0;
fbcosentino 0:8a14924886c4 15 _out3=0;
fbcosentino 0:8a14924886c4 16 _out4=0;
fbcosentino 0:8a14924886c4 17 break;
fbcosentino 0:8a14924886c4 18 }
fbcosentino 0:8a14924886c4 19 case 1: {
fbcosentino 0:8a14924886c4 20 _out1=1;
fbcosentino 0:8a14924886c4 21 _out2=1;
fbcosentino 0:8a14924886c4 22 _out3=0;
fbcosentino 0:8a14924886c4 23 _out4=0;
fbcosentino 0:8a14924886c4 24 break;
fbcosentino 0:8a14924886c4 25 }
fbcosentino 0:8a14924886c4 26 case 2: {
fbcosentino 0:8a14924886c4 27 _out1=0;
fbcosentino 0:8a14924886c4 28 _out2=1;
fbcosentino 0:8a14924886c4 29 _out3=0;
fbcosentino 0:8a14924886c4 30 _out4=0;
fbcosentino 0:8a14924886c4 31 break;
fbcosentino 0:8a14924886c4 32 }
fbcosentino 0:8a14924886c4 33 case 3: {
fbcosentino 0:8a14924886c4 34 _out1=0;
fbcosentino 0:8a14924886c4 35 _out2=1;
fbcosentino 0:8a14924886c4 36 _out3=1;
fbcosentino 0:8a14924886c4 37 _out4=0;
fbcosentino 0:8a14924886c4 38 break;
fbcosentino 0:8a14924886c4 39 }
fbcosentino 0:8a14924886c4 40 case 4: {
fbcosentino 0:8a14924886c4 41 _out1=0;
fbcosentino 0:8a14924886c4 42 _out2=0;
fbcosentino 0:8a14924886c4 43 _out3=1;
fbcosentino 0:8a14924886c4 44 _out4=0;
fbcosentino 0:8a14924886c4 45 break;
fbcosentino 0:8a14924886c4 46 }
fbcosentino 0:8a14924886c4 47 case 5: {
fbcosentino 0:8a14924886c4 48 _out1=0;
fbcosentino 0:8a14924886c4 49 _out2=0;
fbcosentino 0:8a14924886c4 50 _out3=1;
fbcosentino 0:8a14924886c4 51 _out4=1;
fbcosentino 0:8a14924886c4 52 break;
fbcosentino 0:8a14924886c4 53 }
fbcosentino 0:8a14924886c4 54 case 6: {
fbcosentino 0:8a14924886c4 55 _out1=0;
fbcosentino 0:8a14924886c4 56 _out2=0;
fbcosentino 0:8a14924886c4 57 _out3=0;
fbcosentino 0:8a14924886c4 58 _out4=1;
fbcosentino 0:8a14924886c4 59 break;
fbcosentino 0:8a14924886c4 60 }
fbcosentino 0:8a14924886c4 61 case 7: {
fbcosentino 0:8a14924886c4 62 _out1=1;
fbcosentino 0:8a14924886c4 63 _out2=0;
fbcosentino 0:8a14924886c4 64 _out3=0;
fbcosentino 0:8a14924886c4 65 _out4=1;
fbcosentino 0:8a14924886c4 66 break;
fbcosentino 0:8a14924886c4 67 }
fbcosentino 0:8a14924886c4 68 }
fbcosentino 0:8a14924886c4 69 }
fbcosentino 0:8a14924886c4 70
fbcosentino 0:8a14924886c4 71 void ULN2003::stepForward() {
fbcosentino 0:8a14924886c4 72 _pos++;
fbcosentino 0:8a14924886c4 73 if (_pos > 7) _pos = 0;
fbcosentino 0:8a14924886c4 74 _set_output();
fbcosentino 0:8a14924886c4 75 }
fbcosentino 0:8a14924886c4 76
fbcosentino 0:8a14924886c4 77 void ULN2003::stepReverse() {
fbcosentino 0:8a14924886c4 78 _pos--;
fbcosentino 0:8a14924886c4 79 if (_pos < 0) _pos = 7;
fbcosentino 0:8a14924886c4 80 _set_output();
fbcosentino 0:8a14924886c4 81 }
fbcosentino 0:8a14924886c4 82
fbcosentino 0:8a14924886c4 83 void ULN2003::moveForward(int steps, float speed) {
fbcosentino 0:8a14924886c4 84 int delay_us_value = (1.0/speed)*1000000; // speed is in Hz = steps/sec
fbcosentino 0:8a14924886c4 85
fbcosentino 0:8a14924886c4 86 for (int i=0; i<steps; i++) {
fbcosentino 0:8a14924886c4 87 stepForward();
fbcosentino 0:8a14924886c4 88 wait_us(delay_us_value);
fbcosentino 0:8a14924886c4 89 }
fbcosentino 0:8a14924886c4 90 relax();
fbcosentino 0:8a14924886c4 91 }
fbcosentino 0:8a14924886c4 92
fbcosentino 0:8a14924886c4 93 void ULN2003::moveReverse(int steps, float speed) {
fbcosentino 0:8a14924886c4 94 int delay_us_value = (1.0/speed)*1000000; // speed is in Hz = steps/sec
fbcosentino 0:8a14924886c4 95
fbcosentino 0:8a14924886c4 96 for (int i=0; i<steps; i++) {
fbcosentino 0:8a14924886c4 97 stepForward();
fbcosentino 0:8a14924886c4 98 wait_us(delay_us_value);
fbcosentino 0:8a14924886c4 99 }
fbcosentino 0:8a14924886c4 100 relax();
fbcosentino 0:8a14924886c4 101 }
fbcosentino 0:8a14924886c4 102
fbcosentino 0:8a14924886c4 103 void ULN2003::turnForward(float turns, float speed) {
fbcosentino 0:8a14924886c4 104 moveForward(_full_turn_steps*turns, _full_turn_steps*speed);
fbcosentino 0:8a14924886c4 105 }
fbcosentino 0:8a14924886c4 106
fbcosentino 0:8a14924886c4 107 void ULN2003::turnReverse(float turns, float speed) {
fbcosentino 0:8a14924886c4 108 moveForward(_full_turn_steps*turns, _full_turn_steps*speed);
fbcosentino 0:8a14924886c4 109 }
fbcosentino 0:8a14924886c4 110
fbcosentino 0:8a14924886c4 111
fbcosentino 0:8a14924886c4 112 void ULN2003::relax() {
fbcosentino 0:8a14924886c4 113 _out1 = 0;
fbcosentino 0:8a14924886c4 114 _out2 = 0;
fbcosentino 0:8a14924886c4 115 _out3 = 0;
fbcosentino 0:8a14924886c4 116 _out4 = 0;
fbcosentino 0:8a14924886c4 117 }