tim004 tim004
/
LV09_Zad02_Tim004
Ediba Žubor Sumejja Porča
main.cpp@0:03eb4b0feb7e, 2014-05-12 (annotated)
- Committer:
- tim004
- Date:
- Mon May 12 18:02:37 2014 +0000
- Revision:
- 0:03eb4b0feb7e
LV09 Zad02;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim004 | 0:03eb4b0feb7e | 1 | #include "mbed.h" |
tim004 | 0:03eb4b0feb7e | 2 | #include "sMotor.h" |
tim004 | 0:03eb4b0feb7e | 3 | |
tim004 | 0:03eb4b0feb7e | 4 | |
tim004 | 0:03eb4b0feb7e | 5 | Serial pc(USBTX, USBRX); |
tim004 | 0:03eb4b0feb7e | 6 | sMotor motor(dp9, dp10, dp11, dp13); // creates new stepper motor: IN1, IN2, IN3, IN4 |
tim004 | 0:03eb4b0feb7e | 7 | |
tim004 | 0:03eb4b0feb7e | 8 | int step_speed = 1200 ; // set default motor speed |
tim004 | 0:03eb4b0feb7e | 9 | int numstep = 512 ; // defines full turn of 360 degree |
tim004 | 0:03eb4b0feb7e | 10 | int direction = 0; //0 for right, 1 for left |
tim004 | 0:03eb4b0feb7e | 11 | |
tim004 | 0:03eb4b0feb7e | 12 | void screenMenu() |
tim004 | 0:03eb4b0feb7e | 13 | { |
tim004 | 0:03eb4b0feb7e | 14 | // Screen Menu |
tim004 | 0:03eb4b0feb7e | 15 | pc.printf("Default Speed: %d, press: \n\r",step_speed); |
tim004 | 0:03eb4b0feb7e | 16 | pc.printf("1- to choose random angle to set new motor state\n\r"); |
tim004 | 0:03eb4b0feb7e | 17 | pc.printf("2- to change the motor direction\n\r"); |
tim004 | 0:03eb4b0feb7e | 18 | pc.printf("3- to change the rotation speed\n\r"); |
tim004 | 0:03eb4b0feb7e | 19 | pc.printf("4- to start/stop the motor\n\r"); |
tim004 | 0:03eb4b0feb7e | 20 | } |
tim004 | 0:03eb4b0feb7e | 21 | int main() { |
tim004 | 0:03eb4b0feb7e | 22 | |
tim004 | 0:03eb4b0feb7e | 23 | screenMenu(); |
tim004 | 0:03eb4b0feb7e | 24 | |
tim004 | 0:03eb4b0feb7e | 25 | while (1) { |
tim004 | 0:03eb4b0feb7e | 26 | |
tim004 | 0:03eb4b0feb7e | 27 | if (pc.readable()) { // checks for serial |
tim004 | 0:03eb4b0feb7e | 28 | char c (pc.getc()); |
tim004 | 0:03eb4b0feb7e | 29 | if (c == '1') |
tim004 | 0:03eb4b0feb7e | 30 | { |
tim004 | 0:03eb4b0feb7e | 31 | |
tim004 | 0:03eb4b0feb7e | 32 | int angle; |
tim004 | 0:03eb4b0feb7e | 33 | pc.printf("\nSpecify the angle to move the motor from current position: \n\r"); |
tim004 | 0:03eb4b0feb7e | 34 | pc.scanf("%d", &angle); |
tim004 | 0:03eb4b0feb7e | 35 | if ( angle > 0 ) |
tim004 | 0:03eb4b0feb7e | 36 | motor.step(numstep / 360 * (angle % 360), direction, step_speed); // number of steps, direction, speed |
tim004 | 0:03eb4b0feb7e | 37 | else |
tim004 | 0:03eb4b0feb7e | 38 | motor.step(numstep / 360 * (angle % 360), 1 - direction, step_speed); // number of steps, direction, speed |
tim004 | 0:03eb4b0feb7e | 39 | |
tim004 | 0:03eb4b0feb7e | 40 | screenMenu(); |
tim004 | 0:03eb4b0feb7e | 41 | } |
tim004 | 0:03eb4b0feb7e | 42 | |
tim004 | 0:03eb4b0feb7e | 43 | if (c =='2') |
tim004 | 0:03eb4b0feb7e | 44 | { |
tim004 | 0:03eb4b0feb7e | 45 | direction = 1 - direction; |
tim004 | 0:03eb4b0feb7e | 46 | pc.printf("\nDirection is changed.\n"); |
tim004 | 0:03eb4b0feb7e | 47 | screenMenu(); |
tim004 | 0:03eb4b0feb7e | 48 | } |
tim004 | 0:03eb4b0feb7e | 49 | if ( c =='3') |
tim004 | 0:03eb4b0feb7e | 50 | { |
tim004 | 0:03eb4b0feb7e | 51 | pc.printf("\nCurrent Speed: %d\n\r", step_speed); |
tim004 | 0:03eb4b0feb7e | 52 | pc.printf("\nNew speed: \n\r"); |
tim004 | 0:03eb4b0feb7e | 53 | pc.scanf("%d",&step_speed); // sets new speed |
tim004 | 0:03eb4b0feb7e | 54 | pc.printf("\nThe speed has been changed."); |
tim004 | 0:03eb4b0feb7e | 55 | screenMenu(); |
tim004 | 0:03eb4b0feb7e | 56 | } |
tim004 | 0:03eb4b0feb7e | 57 | |
tim004 | 0:03eb4b0feb7e | 58 | if (c =='4') |
tim004 | 0:03eb4b0feb7e | 59 | { |
tim004 | 0:03eb4b0feb7e | 60 | pc.printf("\nPress 0 to stop the motor: \n\r"); |
tim004 | 0:03eb4b0feb7e | 61 | int korak=10; |
tim004 | 0:03eb4b0feb7e | 62 | while (!pc.readable() || pc.getc() != '0'){ |
tim004 | 0:03eb4b0feb7e | 63 | motor.step(korak, direction, step_speed); |
tim004 | 0:03eb4b0feb7e | 64 | korak+=10;} |
tim004 | 0:03eb4b0feb7e | 65 | wait(2); |
tim004 | 0:03eb4b0feb7e | 66 | screenMenu(); |
tim004 | 0:03eb4b0feb7e | 67 | } |
tim004 | 0:03eb4b0feb7e | 68 | |
tim004 | 0:03eb4b0feb7e | 69 | } |
tim004 | 0:03eb4b0feb7e | 70 | } |
tim004 | 0:03eb4b0feb7e | 71 | } |