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.
Dependencies: Motor Servo22oct mbed
Fork of projecto0003030020 by
main.cpp@6:29245562bba3, 2014-11-02 (annotated)
- Committer:
- KyleSchaff
- Date:
- Sun Nov 02 20:06:01 2014 +0000
- Revision:
- 6:29245562bba3
- Parent:
- 5:a7c0a09a7c58
FINAL CODE COMMENTED;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alphasig | 5:a7c0a09a7c58 | 1 | //Project X |
alphasig | 5:a7c0a09a7c58 | 2 | // Jordan Kangwijaya, Gabe Collins, Kyle Schaff |
alphasig | 5:a7c0a09a7c58 | 3 | |
KyleSchaff | 0:db9e971c8ba6 | 4 | #include "mbed.h" |
KyleSchaff | 0:db9e971c8ba6 | 5 | #include "Servo.h" |
KyleSchaff | 0:db9e971c8ba6 | 6 | #include "Motor.h" |
KyleSchaff | 0:db9e971c8ba6 | 7 | |
KyleSchaff | 6:29245562bba3 | 8 | BusIn Switch(p16,p17,p18,p19); //declares switches |
KyleSchaff | 6:29245562bba3 | 9 | BusOut LED(p5,p6,p7,p8,p11); //declares LEDs |
KyleSchaff | 0:db9e971c8ba6 | 10 | |
KyleSchaff | 6:29245562bba3 | 11 | Motor m(p26,p29,p30); //declares motor |
KyleSchaff | 6:29245562bba3 | 12 | Servo servo1(p21); //top .39 to 1.02 declares servos |
jkangwi | 3:b81a3d326aee | 13 | Servo servo2(p22); //bottom .38 to .93 |
KyleSchaff | 0:db9e971c8ba6 | 14 | |
KyleSchaff | 6:29245562bba3 | 15 | float servopos; //variable will be used to control servo positions |
KyleSchaff | 6:29245562bba3 | 16 | float motorspeed=.35; //variable will be used to control motorspeed |
KyleSchaff | 6:29245562bba3 | 17 | int counter=0; |
KyleSchaff | 6:29245562bba3 | 18 | int counter1=0; //counters will be used later to control LEDs |
KyleSchaff | 0:db9e971c8ba6 | 19 | |
KyleSchaff | 0:db9e971c8ba6 | 20 | int main() |
KyleSchaff | 0:db9e971c8ba6 | 21 | { |
KyleSchaff | 6:29245562bba3 | 22 | while(1) { //so switches always work, and program is constantly checking on switches being on or off |
jkangwi | 3:b81a3d326aee | 23 | |
jkangwi | 3:b81a3d326aee | 24 | |
KyleSchaff | 6:29245562bba3 | 25 | while (Switch==1) { //while switch 1 is on |
jkangwi | 3:b81a3d326aee | 26 | |
jkangwi | 3:b81a3d326aee | 27 | |
alphasig | 4:4b093f9bd41c | 28 | for(servopos=.40; servopos<=.60; servopos+=.03) { |
KyleSchaff | 6:29245562bba3 | 29 | servo1=.7; //sets constant position for servo1 |
KyleSchaff | 6:29245562bba3 | 30 | servo2=servopos; //sets servo2's position equal to the variable in the for loop, moving the servo from position 1 to position 2 |
KyleSchaff | 6:29245562bba3 | 31 | motorspeed=0; //no motor speed for this level |
alphasig | 4:4b093f9bd41c | 32 | m.speed(motorspeed); |
KyleSchaff | 6:29245562bba3 | 33 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 34 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 35 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 36 | LED=rand()%129; //sets LEDs to randomly light up |
KyleSchaff | 0:db9e971c8ba6 | 37 | } |
KyleSchaff | 6:29245562bba3 | 38 | wait (.1); //low wait time for smooth motion |
KyleSchaff | 0:db9e971c8ba6 | 39 | } |
alphasig | 4:4b093f9bd41c | 40 | for(servopos=.60; servopos>=.40; servopos-=.03) { |
KyleSchaff | 6:29245562bba3 | 41 | servo1=.7; //sets constant position for servo1 |
KyleSchaff | 6:29245562bba3 | 42 | servo2=servopos; //sets servo2's position equal to the variable in the for loop, moving the servo from position 2 to position 1 |
KyleSchaff | 6:29245562bba3 | 43 | motorspeed=0; //no motor speed for this level |
alphasig | 4:4b093f9bd41c | 44 | m.speed(motorspeed); |
KyleSchaff | 6:29245562bba3 | 45 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 46 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 47 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 48 | LED=rand()%129; //sets LEDs to randomly light up |
KyleSchaff | 1:33ef833aae0b | 49 | } |
KyleSchaff | 6:29245562bba3 | 50 | wait (.1); //low wait time for smooth motion |
KyleSchaff | 1:33ef833aae0b | 51 | } |
KyleSchaff | 6:29245562bba3 | 52 | } |
jkangwi | 3:b81a3d326aee | 53 | while(Switch==2) { |
jkangwi | 3:b81a3d326aee | 54 | |
jkangwi | 3:b81a3d326aee | 55 | |
alphasig | 4:4b093f9bd41c | 56 | for(servopos=.40; servopos<=.60; servopos+=.03) { |
KyleSchaff | 6:29245562bba3 | 57 | servo1=servopos+.2; //sets servo1's position equal to the variable in the for loop, moving the servo from position 1 to position 2 |
alphasig | 4:4b093f9bd41c | 58 | servo2=.5; |
KyleSchaff | 6:29245562bba3 | 59 | motorspeed=0; //no motor speed for this level |
alphasig | 4:4b093f9bd41c | 60 | m.speed(motorspeed); |
KyleSchaff | 6:29245562bba3 | 61 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 62 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 63 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 64 | LED=rand()%129; //sets LEDs to randomly light up |
KyleSchaff | 0:db9e971c8ba6 | 65 | } |
KyleSchaff | 6:29245562bba3 | 66 | wait (.03); //low wait time for smooth motion |
KyleSchaff | 0:db9e971c8ba6 | 67 | } |
alphasig | 4:4b093f9bd41c | 68 | for(servopos=.60; servopos>=.40; servopos-=.03) { |
KyleSchaff | 6:29245562bba3 | 69 | servo1=servopos+.2; //sets servo1's position equal to the variable in the for loop, moving the servo from position 2 to position 1 |
alphasig | 4:4b093f9bd41c | 70 | servo2=.5; |
KyleSchaff | 6:29245562bba3 | 71 | motorspeed=0; //no motor speed for this level |
alphasig | 4:4b093f9bd41c | 72 | m.speed(motorspeed); |
KyleSchaff | 6:29245562bba3 | 73 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 74 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 75 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 76 | LED=rand()%129; //sets LEDs to randomly light up |
KyleSchaff | 1:33ef833aae0b | 77 | } |
KyleSchaff | 6:29245562bba3 | 78 | wait (.03); //low wait time for smooth motion |
KyleSchaff | 1:33ef833aae0b | 79 | } |
KyleSchaff | 0:db9e971c8ba6 | 80 | } |
jkangwi | 3:b81a3d326aee | 81 | while(Switch==4) { |
jkangwi | 3:b81a3d326aee | 82 | |
alphasig | 4:4b093f9bd41c | 83 | for(servopos=.40; servopos<=.60; servopos+=.03) { |
KyleSchaff | 6:29245562bba3 | 84 | servo1=servopos+.2; //sets servo1's position equal to the variable in the for loop, moving the servo from position 1 to position 2 |
KyleSchaff | 6:29245562bba3 | 85 | servo2=servopos; //sets servo2's position equal to the variable in the for loop, moving the servo from position 1 to position 2 |
KyleSchaff | 6:29245562bba3 | 86 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 87 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 88 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 89 | LED=rand()%129; //sets LEDs to randomly light up |
alphasig | 4:4b093f9bd41c | 90 | } |
KyleSchaff | 6:29245562bba3 | 91 | wait (.02); //low wait time for smooth motion |
alphasig | 4:4b093f9bd41c | 92 | } |
alphasig | 4:4b093f9bd41c | 93 | for(servopos=.60; servopos>=.40; servopos-=.03) { |
KyleSchaff | 6:29245562bba3 | 94 | servo1=servopos+.2; //sets servo1's position equal to the variable in the for loop, moving the servo from position 2 to position 1 |
KyleSchaff | 6:29245562bba3 | 95 | servo2=servopos; //sets servo2's position equal to the variable in the for loop, moving the servo from position 2 to position 1 |
KyleSchaff | 6:29245562bba3 | 96 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 97 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 98 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 99 | LED=rand()%129; //sets LEDs to randomly light up |
alphasig | 4:4b093f9bd41c | 100 | } |
KyleSchaff | 6:29245562bba3 | 101 | wait (.02); //low wait time for smooth motion |
alphasig | 4:4b093f9bd41c | 102 | } |
alphasig | 4:4b093f9bd41c | 103 | } |
jkangwi | 3:b81a3d326aee | 104 | |
alphasig | 4:4b093f9bd41c | 105 | while(Switch==8) { |
alphasig | 5:a7c0a09a7c58 | 106 | motorspeed=.35; |
alphasig | 4:4b093f9bd41c | 107 | m.speed(motorspeed); |
alphasig | 4:4b093f9bd41c | 108 | for(servopos=.40; servopos<=.60; servopos+=.03) { |
KyleSchaff | 6:29245562bba3 | 109 | servo1=servopos+.2; //sets servo1's position equal to the variable in the for loop, moving the servo from position 1 to position 2 |
KyleSchaff | 6:29245562bba3 | 110 | servo2=servopos; //sets servo2's position equal to the variable in the for loop, moving the servo from position 1 to position 2 |
alphasig | 4:4b093f9bd41c | 111 | m.speed(motorspeed); |
KyleSchaff | 6:29245562bba3 | 112 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 113 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 114 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 115 | LED=rand()%129; //sets LEDs to randomly light up |
KyleSchaff | 0:db9e971c8ba6 | 116 | } |
KyleSchaff | 6:29245562bba3 | 117 | wait (.025); //low wait time for smooth motion |
KyleSchaff | 0:db9e971c8ba6 | 118 | } |
alphasig | 4:4b093f9bd41c | 119 | for(servopos=.60; servopos>=.40; servopos-=.03) { |
KyleSchaff | 6:29245562bba3 | 120 | servo1=servopos+.2; //sets servo2's position equal to the variable in the for loop, moving the servo from position 2 to position 1 |
KyleSchaff | 6:29245562bba3 | 121 | servo2=servopos; //sets servo2's position equal to the variable in the for loop, moving the servo from position 2 to position 1 |
alphasig | 4:4b093f9bd41c | 122 | m.speed(motorspeed); |
KyleSchaff | 6:29245562bba3 | 123 | counter=counter+1; //counter will count up by one each iteration |
KyleSchaff | 6:29245562bba3 | 124 | counter1=counter%20; //counter 2 will count up until it reaches 20, then starts over |
KyleSchaff | 6:29245562bba3 | 125 | if (counter1==17) { //every twentieth iteration, on the seventeenth, this happens |
KyleSchaff | 6:29245562bba3 | 126 | LED=rand()%129; //sets LEDs to randomly light up |
KyleSchaff | 1:33ef833aae0b | 127 | } |
KyleSchaff | 6:29245562bba3 | 128 | wait (.025); //low wait time for smooth motion |
KyleSchaff | 1:33ef833aae0b | 129 | } |
KyleSchaff | 0:db9e971c8ba6 | 130 | } |
alphasig | 4:4b093f9bd41c | 131 | |
KyleSchaff | 6:29245562bba3 | 132 | while(Switch==0) { //neutral position when no switches are on |
KyleSchaff | 6:29245562bba3 | 133 | servo1=.7; //neutral position when no switches are on |
KyleSchaff | 6:29245562bba3 | 134 | servo2=.5; //neutral position when no switches are on |
KyleSchaff | 6:29245562bba3 | 135 | motorspeed=0; //neutral motor speed when no switches are on |
KyleSchaff | 2:c95e4e33eaee | 136 | m.speed(motorspeed); |
KyleSchaff | 0:db9e971c8ba6 | 137 | } |
KyleSchaff | 0:db9e971c8ba6 | 138 | } |
KyleSchaff | 0:db9e971c8ba6 | 139 | } |