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
Revision 5:90f7d451910e, committed 2019-08-14
- Comitter:
- aluthe3099
- Date:
- Wed Aug 14 13:28:50 2019 +0000
- Parent:
- 4:5bf4d3c6ec3b
- Commit message:
- Contains timer so ears can move based on time interval rather than in relation solely to the other movements of the ear.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 5bf4d3c6ec3b -r 90f7d451910e main.cpp --- a/main.cpp Tue Aug 06 15:50:39 2019 +0000 +++ b/main.cpp Wed Aug 14 13:28:50 2019 +0000 @@ -3,21 +3,52 @@ #define NUM_SERVOS 4 Servo servo[NUM_SERVOS]={p24,p23,p22,p21}; -float range = 0.0005; -float degrees = 45.0; void bothForwardOrBack(float min, float max, int direction, int startVEnd); //1 is forward, 2 is backwards void oneSideForwardOrBack(float min, float max, int direction, int side, int startVEnd);//1 is forward 2 is back, 1 is left 0 is right -void movementCombo(float randoNum, int forwardVBack); -int main() { - while(1) { - int rando = rand(); - // float randoNum = rando/32767; - float randoNum = 0.56; - movementCombo(randoNum, 1); - wait(3); - movementCombo(randoNum, 2); - wait(3); +void movementCombo(float firstDisplace, float secondDisplace, int forwardVBack, int whichSideFirst);//1 is left, zero is right + +Timer t; +float interval1 = 10; +float alreadyPast = 0; +int main() { + t.start(); + float previousSeconds = 0; + while(1) { + float currentSeconds = t.read(); + + float disp1 = 0.5; + float disp2 = disp1 + 0.5; + //THIS PART OF THE FUNCTION RUNS BASED ON A TIME LOOP + if(currentSeconds-previousSeconds>interval1) + { + previousSeconds = currentSeconds; + disp2 = disp1 + 0.2; + movementCombo(disp1, disp2, 1, 1); + wait(0.5); + movementCombo(disp1, disp2, 2, 1); + wait(1); + + disp1 = 0.9; + disp2 = 1.0; + movementCombo(disp1, disp2, 1, 0); + wait(0.5); + movementCombo(disp1, disp2, 2, 1); + wait(1); + + disp1 = 0.1; + disp2 = disp1 + 0.5; + movementCombo(disp1, disp2, 1, 0); + wait(0.5); + movementCombo(disp1, disp2, 2, 0); + wait(1); + } + + //THIS PART OF THE FUNCTION CONSTANTLY RUNS + movementCombo(disp1, disp2, 1, 1); + wait(0.5); + movementCombo(disp1, disp2, 2, 1); + wait(1); } } @@ -63,19 +94,29 @@ } printf("finished one run of bothForwardOrBack\n"); } -void movementCombo(float randoNum, int forwardVBack) +void movementCombo(float firstDisplace, float secondDisplace, int forwardVBack, int whichSideFirst) { //forward is 1, back is 2 + //1 is left first, 0 is right first - bothForwardOrBack(0.0, randoNum, forwardVBack, 0); //moves both ears partway + bothForwardOrBack(0.0, firstDisplace, forwardVBack, 0); //moves both ears partway wait(1); - oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 1, 0); //moves left ear rest of way - oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 1, 1); //moves left ear back to position - - oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 0, 0); //moves right ear rest of way - oneSideForwardOrBack(randoNum, 1.0, forwardVBack, 0, 1); //moves right ear back to position + if(whichSideFirst == 1) + { + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 1, 0); //moves left ear rest of way + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 1, 1); //moves left ear back to position + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 0, 0); //moves right ear rest of way + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 0, 1); //moves right ear back to position + } + else + { + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 0, 0); //moves right ear rest of way + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 0, 1); //moves right ear back to position + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 1, 0); //moves left ear rest of way + oneSideForwardOrBack(firstDisplace, secondDisplace, forwardVBack, 1, 1); //moves left ear back to position + } wait(1); - bothForwardOrBack(0.0, randoNum, forwardVBack, 1); //moves both ears to position 0 + bothForwardOrBack(0.0, firstDisplace, forwardVBack, 1); //moves both ears to position 0 wait(1); }