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@4:f1782bdd08a8, 2016-10-24 (annotated)
- Committer:
- LeeJon
- Date:
- Mon Oct 24 08:58:40 2016 +0000
- Revision:
- 4:f1782bdd08a8
- Parent:
- 3:8d07dbdd3afc
- Child:
- 5:d62cebed51d2
werkt nu echt met plus min en spatie
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LeeJon | 0:ef8775413c37 | 1 | #include "mbed.h" |
LeeJon | 0:ef8775413c37 | 2 | |
LeeJon | 0:ef8775413c37 | 3 | Serial pc(USBTX,USBRX); |
LeeJon | 0:ef8775413c37 | 4 | |
LeeJon | 0:ef8775413c37 | 5 | PwmOut ServoPWMpin(D7); |
LeeJon | 0:ef8775413c37 | 6 | |
LeeJon | 0:ef8775413c37 | 7 | char Key; |
LeeJon | 1:08bf7339efdd | 8 | float ServoAngle = 89 ; |
LeeJon | 0:ef8775413c37 | 9 | float Pulsew = 0.0015; |
LeeJon | 0:ef8775413c37 | 10 | const float Frequency = 10; |
LeeJon | 0:ef8775413c37 | 11 | |
LeeJon | 0:ef8775413c37 | 12 | int main() |
LeeJon | 0:ef8775413c37 | 13 | { |
LeeJon | 0:ef8775413c37 | 14 | |
LeeJon | 2:2cc5d274f30d | 15 | ServoPWMpin.period(0.01f); // 0.01 second period |
LeeJon | 0:ef8775413c37 | 16 | |
LeeJon | 0:ef8775413c37 | 17 | while (true) { |
LeeJon | 0:ef8775413c37 | 18 | |
LeeJon | 3:8d07dbdd3afc | 19 | if (pc.readable()) { // if a key press happens |
LeeJon | 3:8d07dbdd3afc | 20 | Key = pc.getc(); // get the pressed key |
LeeJon | 2:2cc5d274f30d | 21 | switch(Key) { //Check to see which key pressed |
LeeJon | 4:f1782bdd08a8 | 22 | case 0x2B: //It was the + key... |
LeeJon | 3:8d07dbdd3afc | 23 | pc.printf("\n\r +!"); |
LeeJon | 1:08bf7339efdd | 24 | if( ServoAngle < 80){ |
LeeJon | 3:8d07dbdd3afc | 25 | ServoAngle = ServoAngle+10; // increase the angle |
LeeJon | 0:ef8775413c37 | 26 | } |
LeeJon | 0:ef8775413c37 | 27 | break; |
LeeJon | 4:f1782bdd08a8 | 28 | case 0x2D: //It was the - Key key... |
LeeJon | 3:8d07dbdd3afc | 29 | pc.printf("\n\r -!"); |
LeeJon | 4:f1782bdd08a8 | 30 | if( ServoAngle > 10){ |
LeeJon | 3:8d07dbdd3afc | 31 | ServoAngle = ServoAngle-10; // decrease the angle |
LeeJon | 0:ef8775413c37 | 32 | } |
LeeJon | 0:ef8775413c37 | 33 | break; |
LeeJon | 2:2cc5d274f30d | 34 | case 0x20: //It was the Spacebar key... |
LeeJon | 0:ef8775413c37 | 35 | pc.printf("\n\r SPACE!"); |
LeeJon | 2:2cc5d274f30d | 36 | if( ServoAngle < 45){ // Switch from open to closed or else otherwise |
LeeJon | 3:8d07dbdd3afc | 37 | ServoAngle = 89; // open/close |
LeeJon | 0:ef8775413c37 | 38 | } |
LeeJon | 0:ef8775413c37 | 39 | else{ |
LeeJon | 3:8d07dbdd3afc | 40 | ServoAngle = 1; // open/close |
LeeJon | 0:ef8775413c37 | 41 | } |
LeeJon | 0:ef8775413c37 | 42 | break; |
LeeJon | 0:ef8775413c37 | 43 | } |
LeeJon | 2:2cc5d274f30d | 44 | } else { |
LeeJon | 2:2cc5d274f30d | 45 | wait(1/Frequency); |
LeeJon | 2:2cc5d274f30d | 46 | } |
LeeJon | 0:ef8775413c37 | 47 | |
LeeJon | 3:8d07dbdd3afc | 48 | Pulsew = 0.0015+(ServoAngle)/180000; // calculate the pulsewidth in the range 1 to 2 milliseconds |
LeeJon | 3:8d07dbdd3afc | 49 | ServoPWMpin.pulsewidth(Pulsew); // write the pulsewidth |
LeeJon | 0:ef8775413c37 | 50 | pc.printf("\n\r Pulsew is %f",Pulsew); |
LeeJon | 0:ef8775413c37 | 51 | } |
LeeJon | 0:ef8775413c37 | 52 | } |