servo_hello_world
Dependencies: mbed Servo MMA7660
liam.cpp@0:1891c8e1bb4a, 2021-02-23 (annotated)
- Committer:
- mistaherd
- Date:
- Tue Feb 23 12:11:13 2021 +0000
- Revision:
- 0:1891c8e1bb4a
servo_hello_world
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mistaherd | 0:1891c8e1bb4a | 1 | #include "mbed.h" |
mistaherd | 0:1891c8e1bb4a | 2 | #include "Servo.h" |
mistaherd | 0:1891c8e1bb4a | 3 | #include <iostream> |
mistaherd | 0:1891c8e1bb4a | 4 | #include "MMA7660.h" |
mistaherd | 0:1891c8e1bb4a | 5 | |
mistaherd | 0:1891c8e1bb4a | 6 | Serial pc(USBTX, USBRX); // tx, rx |
mistaherd | 0:1891c8e1bb4a | 7 | |
mistaherd | 0:1891c8e1bb4a | 8 | |
mistaherd | 0:1891c8e1bb4a | 9 | DigitalIn joy_up(p15); |
mistaherd | 0:1891c8e1bb4a | 10 | DigitalIn joy_down(p12); |
mistaherd | 0:1891c8e1bb4a | 11 | |
mistaherd | 0:1891c8e1bb4a | 12 | Servo myservo1(p21); |
mistaherd | 0:1891c8e1bb4a | 13 | Servo myservo2(p22); |
mistaherd | 0:1891c8e1bb4a | 14 | |
mistaherd | 0:1891c8e1bb4a | 15 | |
mistaherd | 0:1891c8e1bb4a | 16 | AnalogIn p1(p19); |
mistaherd | 0:1891c8e1bb4a | 17 | AnalogIn p2(p20); |
mistaherd | 0:1891c8e1bb4a | 18 | |
mistaherd | 0:1891c8e1bb4a | 19 | MMA7660 MMA(p28, p27); |
mistaherd | 0:1891c8e1bb4a | 20 | DigitalOut connectionLed(LED1);//Accel OK LED |
mistaherd | 0:1891c8e1bb4a | 21 | |
mistaherd | 0:1891c8e1bb4a | 22 | int main (){ |
mistaherd | 0:1891c8e1bb4a | 23 | char option; |
mistaherd | 0:1891c8e1bb4a | 24 | pc.printf("command list 1,2,3,4,5,6,7\n"); |
mistaherd | 0:1891c8e1bb4a | 25 | while(1){ |
mistaherd | 0:1891c8e1bb4a | 26 | |
mistaherd | 0:1891c8e1bb4a | 27 | option = pc.getc(); |
mistaherd | 0:1891c8e1bb4a | 28 | |
mistaherd | 0:1891c8e1bb4a | 29 | if (MMA.testConnection()) |
mistaherd | 0:1891c8e1bb4a | 30 | { |
mistaherd | 0:1891c8e1bb4a | 31 | connectionLed = 1 ; //Accelerometer init OK |
mistaherd | 0:1891c8e1bb4a | 32 | } |
mistaherd | 0:1891c8e1bb4a | 33 | if(option =='1') |
mistaherd | 0:1891c8e1bb4a | 34 | { |
mistaherd | 0:1891c8e1bb4a | 35 | myservo1 = 1 ; |
mistaherd | 0:1891c8e1bb4a | 36 | myservo2 = 1 ; |
mistaherd | 0:1891c8e1bb4a | 37 | } |
mistaherd | 0:1891c8e1bb4a | 38 | else if(option == '2') |
mistaherd | 0:1891c8e1bb4a | 39 | { |
mistaherd | 0:1891c8e1bb4a | 40 | myservo1 = - 0.5 ; |
mistaherd | 0:1891c8e1bb4a | 41 | myservo2 = - 0.5 ; |
mistaherd | 0:1891c8e1bb4a | 42 | } |
mistaherd | 0:1891c8e1bb4a | 43 | else if(option =='3') |
mistaherd | 0:1891c8e1bb4a | 44 | { |
mistaherd | 0:1891c8e1bb4a | 45 | myservo1 = 0; |
mistaherd | 0:1891c8e1bb4a | 46 | myservo2 = 0; |
mistaherd | 0:1891c8e1bb4a | 47 | } |
mistaherd | 0:1891c8e1bb4a | 48 | else if (option =='4') |
mistaherd | 0:1891c8e1bb4a | 49 | { |
mistaherd | 0:1891c8e1bb4a | 50 | float x ; |
mistaherd | 0:1891c8e1bb4a | 51 | cout << "please enter in the degree you want the servo to be at go (limit is 180 degrees be sure to hit enter after command \n\r" ; |
mistaherd | 0:1891c8e1bb4a | 52 | cin >> x; // waits for the user to type something and hit enter to finsh |
mistaherd | 0:1891c8e1bb4a | 53 | wait(0.01); |
mistaherd | 0:1891c8e1bb4a | 54 | myservo1 = x/180; |
mistaherd | 0:1891c8e1bb4a | 55 | myservo2 = x/180 ; |
mistaherd | 0:1891c8e1bb4a | 56 | |
mistaherd | 0:1891c8e1bb4a | 57 | } |
mistaherd | 0:1891c8e1bb4a | 58 | else if (option == '5') |
mistaherd | 0:1891c8e1bb4a | 59 | { |
mistaherd | 0:1891c8e1bb4a | 60 | float x=0 ,y=0; |
mistaherd | 0:1891c8e1bb4a | 61 | x = (x + MMA.x() ); |
mistaherd | 0:1891c8e1bb4a | 62 | y = (y -(MMA.y() )); |
mistaherd | 0:1891c8e1bb4a | 63 | myservo1 = x; |
mistaherd | 0:1891c8e1bb4a | 64 | myservo2 =y ; |
mistaherd | 0:1891c8e1bb4a | 65 | //pc.printf("mma.x is = %f ,mma.y is = %f \n \r",MMA.x() ,MMA.y()); use this to see vaules the servo uses |
mistaherd | 0:1891c8e1bb4a | 66 | |
mistaherd | 0:1891c8e1bb4a | 67 | } |
mistaherd | 0:1891c8e1bb4a | 68 | else if (option == '6') |
mistaherd | 0:1891c8e1bb4a | 69 | { |
mistaherd | 0:1891c8e1bb4a | 70 | if (joy_up) |
mistaherd | 0:1891c8e1bb4a | 71 | { |
mistaherd | 0:1891c8e1bb4a | 72 | |
mistaherd | 0:1891c8e1bb4a | 73 | myservo1= myservo1+ 0.1; |
mistaherd | 0:1891c8e1bb4a | 74 | myservo2 = myservo2 + 0.1; |
mistaherd | 0:1891c8e1bb4a | 75 | wait(0.1); |
mistaherd | 0:1891c8e1bb4a | 76 | } |
mistaherd | 0:1891c8e1bb4a | 77 | else if(joy_down) |
mistaherd | 0:1891c8e1bb4a | 78 | { |
mistaherd | 0:1891c8e1bb4a | 79 | myservo1= myservo1- 0.1; |
mistaherd | 0:1891c8e1bb4a | 80 | myservo2 = myservo2 - 0.1; |
mistaherd | 0:1891c8e1bb4a | 81 | wait(0.1); |
mistaherd | 0:1891c8e1bb4a | 82 | } |
mistaherd | 0:1891c8e1bb4a | 83 | } |
mistaherd | 0:1891c8e1bb4a | 84 | else if (option == '7') |
mistaherd | 0:1891c8e1bb4a | 85 | { |
mistaherd | 0:1891c8e1bb4a | 86 | myservo1=p1; |
mistaherd | 0:1891c8e1bb4a | 87 | myservo2=p2; |
mistaherd | 0:1891c8e1bb4a | 88 | |
mistaherd | 0:1891c8e1bb4a | 89 | } |
mistaherd | 0:1891c8e1bb4a | 90 | } |
mistaherd | 0:1891c8e1bb4a | 91 | } |
mistaherd | 0:1891c8e1bb4a | 92 | |
mistaherd | 0:1891c8e1bb4a | 93 | |
mistaherd | 0:1891c8e1bb4a | 94 | |
mistaherd | 0:1891c8e1bb4a | 95 | |
mistaherd | 0:1891c8e1bb4a | 96 | |
mistaherd | 0:1891c8e1bb4a | 97 | |
mistaherd | 0:1891c8e1bb4a | 98 | |
mistaherd | 0:1891c8e1bb4a | 99 |