Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of multiServoBat by
main.cpp@2:7e5d670ed9aa, 2019-08-01 (annotated)
- Committer:
- aluthe3099
- Date:
- Thu Aug 01 14:40:15 2019 +0000
- Revision:
- 2:7e5d670ed9aa
- Parent:
- 1:829cbbaeaf32
- Child:
- 3:88de581de68d
the code has been updated so that it can move all servos, or only two (so that it can individually run left and right ear)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| aluthe3099 | 0:afc37ce56e1f | 1 | #include "mbed.h" |
| aluthe3099 | 0:afc37ce56e1f | 2 | #include "Servo.h" |
| evangeli | 1:829cbbaeaf32 | 3 | |
| evangeli | 1:829cbbaeaf32 | 4 | #define NUM_SERVOS 4 |
| evangeli | 1:829cbbaeaf32 | 5 | Servo servo[NUM_SERVOS]={p24,p23,p22,p21}; |
| aluthe3099 | 0:afc37ce56e1f | 6 | float range = 0.0005; |
| aluthe3099 | 0:afc37ce56e1f | 7 | float degrees = 45.0; |
| aluthe3099 | 0:afc37ce56e1f | 8 | |
| evangeli | 1:829cbbaeaf32 | 9 | void together(); |
| aluthe3099 | 2:7e5d670ed9aa | 10 | void apart(int side); |
| evangeli | 1:829cbbaeaf32 | 11 | |
| evangeli | 1:829cbbaeaf32 | 12 | int main() { |
| evangeli | 1:829cbbaeaf32 | 13 | printf("Hello bats!\n"); |
| evangeli | 1:829cbbaeaf32 | 14 | |
| evangeli | 1:829cbbaeaf32 | 15 | printf("entering loop\n"); |
| evangeli | 1:829cbbaeaf32 | 16 | while(1) { |
| evangeli | 1:829cbbaeaf32 | 17 | printf("entering together() call\n"); |
| evangeli | 1:829cbbaeaf32 | 18 | together(); |
| aluthe3099 | 2:7e5d670ed9aa | 19 | wait(5); |
| aluthe3099 | 2:7e5d670ed9aa | 20 | apart(0); |
| aluthe3099 | 2:7e5d670ed9aa | 21 | wait(2); |
| aluthe3099 | 2:7e5d670ed9aa | 22 | apart(1); |
| aluthe3099 | 2:7e5d670ed9aa | 23 | wait(2); |
| evangeli | 1:829cbbaeaf32 | 24 | } |
| evangeli | 1:829cbbaeaf32 | 25 | |
| evangeli | 1:829cbbaeaf32 | 26 | } |
| evangeli | 1:829cbbaeaf32 | 27 | |
| evangeli | 1:829cbbaeaf32 | 28 | void together() |
| aluthe3099 | 0:afc37ce56e1f | 29 | { |
| evangeli | 1:829cbbaeaf32 | 30 | float i; |
| evangeli | 1:829cbbaeaf32 | 31 | int j; |
| aluthe3099 | 2:7e5d670ed9aa | 32 | float max = 1.0, min = 0.0, incr = 0.01; |
| evangeli | 1:829cbbaeaf32 | 33 | |
| aluthe3099 | 2:7e5d670ed9aa | 34 | for(i = min; i<max; i+=incr) |
| aluthe3099 | 2:7e5d670ed9aa | 35 | { |
| aluthe3099 | 2:7e5d670ed9aa | 36 | for(j = 0; j<NUM_SERVOS; j++) |
| aluthe3099 | 2:7e5d670ed9aa | 37 | { |
| aluthe3099 | 2:7e5d670ed9aa | 38 | servo[j] = i; |
| aluthe3099 | 2:7e5d670ed9aa | 39 | } |
| aluthe3099 | 2:7e5d670ed9aa | 40 | wait(0.01); |
| aluthe3099 | 2:7e5d670ed9aa | 41 | } |
| aluthe3099 | 2:7e5d670ed9aa | 42 | wait(1); |
| aluthe3099 | 2:7e5d670ed9aa | 43 | for(i = max; i>min; i-=incr) |
| aluthe3099 | 0:afc37ce56e1f | 44 | { |
| evangeli | 1:829cbbaeaf32 | 45 | for(j = 0; j<NUM_SERVOS; j++) |
| aluthe3099 | 0:afc37ce56e1f | 46 | { |
| evangeli | 1:829cbbaeaf32 | 47 | servo[j] = i; |
| aluthe3099 | 0:afc37ce56e1f | 48 | } |
| aluthe3099 | 0:afc37ce56e1f | 49 | wait(0.01); |
| aluthe3099 | 0:afc37ce56e1f | 50 | } |
| aluthe3099 | 2:7e5d670ed9aa | 51 | wait(1); |
| aluthe3099 | 2:7e5d670ed9aa | 52 | printf("finished one of together\n"); |
| aluthe3099 | 2:7e5d670ed9aa | 53 | } |
| aluthe3099 | 2:7e5d670ed9aa | 54 | |
| aluthe3099 | 2:7e5d670ed9aa | 55 | //only moves two of the servo motors, which 2 depends on the parameter side: runs 1 and 3 when side = 1, runs 2 and 4 when side = 0 |
| aluthe3099 | 2:7e5d670ed9aa | 56 | void apart (int side) |
| aluthe3099 | 2:7e5d670ed9aa | 57 | { |
| aluthe3099 | 2:7e5d670ed9aa | 58 | float i; |
| aluthe3099 | 2:7e5d670ed9aa | 59 | int j; |
| aluthe3099 | 2:7e5d670ed9aa | 60 | float max = 1.0, min = 0.0, incr = 0.01; |
| aluthe3099 | 2:7e5d670ed9aa | 61 | for(i = min; i<max; i+=incr) |
| aluthe3099 | 0:afc37ce56e1f | 62 | { |
| evangeli | 1:829cbbaeaf32 | 63 | for(j = 0; j<NUM_SERVOS; j++) |
| aluthe3099 | 0:afc37ce56e1f | 64 | { |
| aluthe3099 | 2:7e5d670ed9aa | 65 | if(j%2 == side) |
| aluthe3099 | 0:afc37ce56e1f | 66 | { |
| aluthe3099 | 2:7e5d670ed9aa | 67 | servo[j] = i; |
| aluthe3099 | 0:afc37ce56e1f | 68 | } |
| aluthe3099 | 0:afc37ce56e1f | 69 | |
| aluthe3099 | 0:afc37ce56e1f | 70 | |
| aluthe3099 | 0:afc37ce56e1f | 71 | } |
| aluthe3099 | 0:afc37ce56e1f | 72 | wait(0.01); |
| aluthe3099 | 0:afc37ce56e1f | 73 | } |
| aluthe3099 | 2:7e5d670ed9aa | 74 | wait(1); |
| aluthe3099 | 2:7e5d670ed9aa | 75 | for(i = max; i>min; i-=incr) |
| aluthe3099 | 0:afc37ce56e1f | 76 | { |
| aluthe3099 | 2:7e5d670ed9aa | 77 | for(j = 0; j<NUM_SERVOS; j++) |
| aluthe3099 | 0:afc37ce56e1f | 78 | { |
| aluthe3099 | 2:7e5d670ed9aa | 79 | if(j%2 == side) |
| aluthe3099 | 2:7e5d670ed9aa | 80 | { |
| aluthe3099 | 2:7e5d670ed9aa | 81 | servo[j] = i; |
| aluthe3099 | 2:7e5d670ed9aa | 82 | } |
| aluthe3099 | 0:afc37ce56e1f | 83 | } |
| aluthe3099 | 0:afc37ce56e1f | 84 | wait(0.01); |
| aluthe3099 | 0:afc37ce56e1f | 85 | } |
| aluthe3099 | 2:7e5d670ed9aa | 86 | wait(1); |
| aluthe3099 | 0:afc37ce56e1f | 87 | printf("finished one of together\n"); |
| aluthe3099 | 2:7e5d670ed9aa | 88 | } |
