Dependencies:   mbed Servo

Fork of multiServoBat by Angeline Luther

Committer:
aluthe3099
Date:
Tue Aug 06 15:50:39 2019 +0000
Revision:
4:5bf4d3c6ec3b
Parent:
3:88de581de68d
Child:
5:90f7d451910e
This version is able to combine the movements

Who changed what in which revision?

UserRevisionLine numberNew 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
aluthe3099 4:5bf4d3c6ec3b 9 void bothForwardOrBack(float min, float max, int direction, int startVEnd); //1 is forward, 2 is backwards
aluthe3099 4:5bf4d3c6ec3b 10 void oneSideForwardOrBack(float min, float max, int direction, int side, int startVEnd);//1 is forward 2 is back, 1 is left 0 is right
aluthe3099 4:5bf4d3c6ec3b 11 void movementCombo(float randoNum, int forwardVBack);
evangeli 1:829cbbaeaf32 12 int main() {
evangeli 1:829cbbaeaf32 13 while(1) {
aluthe3099 4:5bf4d3c6ec3b 14 int rando = rand();
aluthe3099 4:5bf4d3c6ec3b 15 // float randoNum = rando/32767;
aluthe3099 4:5bf4d3c6ec3b 16 float randoNum = 0.56;
aluthe3099 4:5bf4d3c6ec3b 17 movementCombo(randoNum, 1);
aluthe3099 4:5bf4d3c6ec3b 18 wait(3);
aluthe3099 4:5bf4d3c6ec3b 19 movementCombo(randoNum, 2);
aluthe3099 4:5bf4d3c6ec3b 20 wait(3);
evangeli 1:829cbbaeaf32 21 }
evangeli 1:829cbbaeaf32 22
evangeli 1:829cbbaeaf32 23 }
aluthe3099 4:5bf4d3c6ec3b 24 void bothForwardOrBack(float min, float max, int direction, int startVEnd)
aluthe3099 0:afc37ce56e1f 25 {
aluthe3099 4:5bf4d3c6ec3b 26 //1 is forward 2 is backwards
aluthe3099 3:88de581de68d 27 float minServo, maxServo;
aluthe3099 3:88de581de68d 28 if (direction == 1)
aluthe3099 3:88de581de68d 29 {
aluthe3099 3:88de581de68d 30 minServo = NUM_SERVOS/2;
aluthe3099 3:88de581de68d 31 maxServo = NUM_SERVOS;
aluthe3099 3:88de581de68d 32 }
aluthe3099 4:5bf4d3c6ec3b 33 else if (direction == 2)
aluthe3099 3:88de581de68d 34 {
aluthe3099 3:88de581de68d 35 minServo = 0;
aluthe3099 3:88de581de68d 36 maxServo = NUM_SERVOS/2;
aluthe3099 3:88de581de68d 37 }
evangeli 1:829cbbaeaf32 38 float i;
evangeli 1:829cbbaeaf32 39 int j;
aluthe3099 4:5bf4d3c6ec3b 40
aluthe3099 4:5bf4d3c6ec3b 41 float incr = 0.01;
aluthe3099 4:5bf4d3c6ec3b 42 if(startVEnd == 0)
aluthe3099 2:7e5d670ed9aa 43 {
aluthe3099 4:5bf4d3c6ec3b 44 for(i = min; i<max; i+=incr)
aluthe3099 2:7e5d670ed9aa 45 {
aluthe3099 4:5bf4d3c6ec3b 46 for(j = minServo; j<maxServo; j++)
aluthe3099 4:5bf4d3c6ec3b 47 {
aluthe3099 4:5bf4d3c6ec3b 48 servo[j] = i;
aluthe3099 4:5bf4d3c6ec3b 49 }
aluthe3099 4:5bf4d3c6ec3b 50 wait(0.005);
aluthe3099 2:7e5d670ed9aa 51 }
aluthe3099 2:7e5d670ed9aa 52 }
aluthe3099 4:5bf4d3c6ec3b 53 else if(startVEnd == 1)
aluthe3099 0:afc37ce56e1f 54 {
aluthe3099 4:5bf4d3c6ec3b 55 for(i = max; i>min; i-=incr)
aluthe3099 0:afc37ce56e1f 56 {
aluthe3099 4:5bf4d3c6ec3b 57 for(j = minServo; j<maxServo; j++)
aluthe3099 4:5bf4d3c6ec3b 58 {
aluthe3099 4:5bf4d3c6ec3b 59 servo[j] = i;
aluthe3099 4:5bf4d3c6ec3b 60 }
aluthe3099 4:5bf4d3c6ec3b 61 wait(0.005);
aluthe3099 0:afc37ce56e1f 62 }
aluthe3099 0:afc37ce56e1f 63 }
aluthe3099 3:88de581de68d 64 printf("finished one run of bothForwardOrBack\n");
aluthe3099 2:7e5d670ed9aa 65 }
aluthe3099 4:5bf4d3c6ec3b 66 void movementCombo(float randoNum, int forwardVBack)
aluthe3099 4:5bf4d3c6ec3b 67 {
aluthe3099 4:5bf4d3c6ec3b 68 //forward is 1, back is 2
aluthe3099 4:5bf4d3c6ec3b 69
aluthe3099 4:5bf4d3c6ec3b 70 bothForwardOrBack(0.0, randoNum, forwardVBack, 0); //moves both ears partway
aluthe3099 4:5bf4d3c6ec3b 71 wait(1);
aluthe3099 4:5bf4d3c6ec3b 72 oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 1, 0); //moves left ear rest of way
aluthe3099 4:5bf4d3c6ec3b 73 oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 1, 1); //moves left ear back to position
aluthe3099 4:5bf4d3c6ec3b 74
aluthe3099 4:5bf4d3c6ec3b 75 oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 0, 0); //moves right ear rest of way
aluthe3099 4:5bf4d3c6ec3b 76 oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 0, 1); //moves right ear back to position
aluthe3099 4:5bf4d3c6ec3b 77 wait(1);
aluthe3099 4:5bf4d3c6ec3b 78 bothForwardOrBack(0.0, randoNum, forwardVBack, 1); //moves both ears to position 0
aluthe3099 4:5bf4d3c6ec3b 79
aluthe3099 4:5bf4d3c6ec3b 80 wait(1);
aluthe3099 4:5bf4d3c6ec3b 81 }
aluthe3099 4:5bf4d3c6ec3b 82 void oneSideForwardOrBack(float min, float max, int direction, int side, int startVEnd)
aluthe3099 2:7e5d670ed9aa 83 {
aluthe3099 3:88de581de68d 84 //direction: one is forwards two is backwards
aluthe3099 4:5bf4d3c6ec3b 85 //0 is right, 1 is left
aluthe3099 3:88de581de68d 86 int whichServo = 0;
aluthe3099 4:5bf4d3c6ec3b 87 if(direction == 1)
aluthe3099 3:88de581de68d 88 {
aluthe3099 3:88de581de68d 89 whichServo = whichServo + NUM_SERVOS/2;
aluthe3099 3:88de581de68d 90 }
aluthe3099 3:88de581de68d 91 if(side == 1)
aluthe3099 3:88de581de68d 92 {
aluthe3099 3:88de581de68d 93 whichServo = whichServo + 1;
aluthe3099 3:88de581de68d 94 }
aluthe3099 4:5bf4d3c6ec3b 95
aluthe3099 4:5bf4d3c6ec3b 96 float incr = 0.01, i;
aluthe3099 4:5bf4d3c6ec3b 97 if(startVEnd == 0)
aluthe3099 0:afc37ce56e1f 98 {
aluthe3099 4:5bf4d3c6ec3b 99 for(i = min; i<max; i+=incr)
aluthe3099 4:5bf4d3c6ec3b 100 {
aluthe3099 4:5bf4d3c6ec3b 101 servo[whichServo] = i;
aluthe3099 4:5bf4d3c6ec3b 102 wait(0.002);
aluthe3099 4:5bf4d3c6ec3b 103 }
aluthe3099 4:5bf4d3c6ec3b 104 wait(0.1);
aluthe3099 0:afc37ce56e1f 105 }
aluthe3099 4:5bf4d3c6ec3b 106 else if(startVEnd == 1)
aluthe3099 0:afc37ce56e1f 107 {
aluthe3099 4:5bf4d3c6ec3b 108 for(i = max; i>min; i-=incr)
aluthe3099 4:5bf4d3c6ec3b 109 {
aluthe3099 4:5bf4d3c6ec3b 110 servo[whichServo] = i;
aluthe3099 4:5bf4d3c6ec3b 111 wait(0.002);
aluthe3099 4:5bf4d3c6ec3b 112 }
aluthe3099 4:5bf4d3c6ec3b 113 wait(0.1);
aluthe3099 0:afc37ce56e1f 114 }
aluthe3099 3:88de581de68d 115 printf("finished one run of oneSideForwardOrBack\n");
aluthe3099 2:7e5d670ed9aa 116 }