Stepper motor with 5 states. State machine is used for switching between states.
Dependencies: mbed
Fork of NucleoF103-stepper_motor_with_button by
Diff: main.cpp
- Revision:
- 1:d30c9dbba3f2
- Parent:
- 0:a8de23e49ba2
--- a/main.cpp Sun Mar 22 14:01:50 2015 +0000 +++ b/main.cpp Sun Apr 26 17:14:24 2015 +0000 @@ -25,7 +25,11 @@ int state = 1; float stepFreq = 5; int accelerationMode = 0; //0 - no acceleration, 1 - accelerating, 2 - decelerating -int position = 1; +int position = 0; +int posArray[4][8] = {1,1,0,0,0,0,0,1, + 0,1,1,1,0,0,0,0, + 0,0,0,1,1,1,0,0, + 0,0,0,0,0,1,1,1}; //mbed - initialization of peripherals DigitalOut my_led(LED1); @@ -166,57 +170,11 @@ * Output : none. * Return : none. *******************************************************************************/ -void halfStep(int position) { //Alternating between one powered coil and two adjacent powered coils. - switch(position){ - case 1 : - out1.write(1); - out2.write(0); - out3.write(0); - out4.write(0); - break; - case 2 : - out1.write(1); - out2.write(1); - out3.write(0); - out4.write(0); - break; - case 3 : - out1.write(0); - out2.write(1); - out3.write(0); - out4.write(0); - break; - case 4 : - out1.write(0); - out2.write(1); - out3.write(1); - out4.write(0); - break; - case 5 : - out1.write(0); - out2.write(0); - out3.write(1); - out4.write(0); - break; - case 6 : - out1.write(0); - out2.write(0); - out3.write(1); - out4.write(1); - break; - case 7 : - out1.write(0); - out2.write(0); - out3.write(0); - out4.write(1); - break; - case 8 : - out1.write(1); - out2.write(0); - out3.write(0); - out4.write(1); - break; - } +void halfStep(int position) { //Alternating between one powered coil and two adjacent coils. + out1.write(posArray[0][position]); + out2.write(posArray[1][position]); + out3.write(posArray[2][position]); + out4.write(posArray[3][position]); } /******************************************************************************* @@ -234,8 +192,8 @@ while (1) { //Infinite while loop if(running == true) { halfStep(position); - position = increment(position , direction, 8); - my_led = !my_led; + position = increment(position , direction, 7); + my_led = !my_led; //Signaliyation of stepping. } if(accelerationMode == 1 && stepFreq < 500) { wait(0.05); //For better control over acceleration