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.
main.cpp@0:89d76ea57469, 2016-11-30 (annotated)
- Committer:
- tmurchison
- Date:
- Wed Nov 30 04:30:44 2016 +0000
- Revision:
- 0:89d76ea57469
Useless Box Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tmurchison | 0:89d76ea57469 | 1 | #include "mbed.h" |
tmurchison | 0:89d76ea57469 | 2 | |
tmurchison | 0:89d76ea57469 | 3 | DigitalIn switchin(p19); //Set pin 19 as input for when switch is towards the arm |
tmurchison | 0:89d76ea57469 | 4 | DigitalIn switchout(p18); //Set pin 18 as input for when switch is away from arm |
tmurchison | 0:89d76ea57469 | 5 | DigitalIn limit(p30); //Set pin 30 as input for limit switch |
tmurchison | 0:89d76ea57469 | 6 | |
tmurchison | 0:89d76ea57469 | 7 | DigitalOut armout(p21); //Set pin 21 as output for when motor is turning CCW towards switch |
tmurchison | 0:89d76ea57469 | 8 | DigitalOut armin(p22); //Set pin 22 as output for when motor is turning CW into box |
tmurchison | 0:89d76ea57469 | 9 | |
tmurchison | 0:89d76ea57469 | 10 | int main() |
tmurchison | 0:89d76ea57469 | 11 | { |
tmurchison | 0:89d76ea57469 | 12 | while(1) //Repeat indefinitely |
tmurchison | 0:89d76ea57469 | 13 | { |
tmurchison | 0:89d76ea57469 | 14 | armin = 0; //Ensure all motor function is stopped |
tmurchison | 0:89d76ea57469 | 15 | |
tmurchison | 0:89d76ea57469 | 16 | if ((switchin) && (switchout==0)) //If the switch is pushed towards the arm |
tmurchison | 0:89d76ea57469 | 17 | { |
tmurchison | 0:89d76ea57469 | 18 | while(switchout) |
tmurchison | 0:89d76ea57469 | 19 | { |
tmurchison | 0:89d76ea57469 | 20 | armout = 1; //The MOSFET enabling the motor to turn CCW (towards the switch) is activated |
tmurchison | 0:89d76ea57469 | 21 | wait(0.002); //PWM is used to run motor at a slower speed with full torque so it can still push the switch |
tmurchison | 0:89d76ea57469 | 22 | armout = 0; |
tmurchison | 0:89d76ea57469 | 23 | wait(0.008); |
tmurchison | 0:89d76ea57469 | 24 | } |
tmurchison | 0:89d76ea57469 | 25 | } |
tmurchison | 0:89d76ea57469 | 26 | else if ((switchin==0) && (switchout)) //If the switch is not pushed towards the arm |
tmurchison | 0:89d76ea57469 | 27 | { |
tmurchison | 0:89d76ea57469 | 28 | while(limit) //And the limit switch is not actuated (it is normally closed) |
tmurchison | 0:89d76ea57469 | 29 | //Once the limit switch is actuated the program will end and the motor will stop |
tmurchison | 0:89d76ea57469 | 30 | { |
tmurchison | 0:89d76ea57469 | 31 | armout = 0; //The MOSFET enabling the motor to turn CCW is deactivated |
tmurchison | 0:89d76ea57469 | 32 | armin = 1; //The MOSFET enabling the motor to turn CW (back into box) is activated |
tmurchison | 0:89d76ea57469 | 33 | wait(0.002); //PWM is again used to protect the limit switch |
tmurchison | 0:89d76ea57469 | 34 | armin = 0; |
tmurchison | 0:89d76ea57469 | 35 | wait(0.008); |
tmurchison | 0:89d76ea57469 | 36 | } |
tmurchison | 0:89d76ea57469 | 37 | } |
tmurchison | 0:89d76ea57469 | 38 | } |
tmurchison | 0:89d76ea57469 | 39 | } |