Created libraries2
Dependencies: mbed ultrasonic
main.cpp@6:0d4008544238, 2015-04-16 (annotated)
- Committer:
- VegardMidt
- Date:
- Thu Apr 16 13:55:42 2015 +0000
- Revision:
- 6:0d4008544238
- Parent:
- 5:b2df25183cb1
- Child:
- 7:c763b5d61dee
- Child:
- 8:454f1f82aca4
Tester ? fjerne uonskede pulser
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
VegardMidt | 0:017111c9207d | 1 | #include "mbed.h" |
VegardMidt | 0:017111c9207d | 2 | #include "ultrasonic.h" |
hebryn | 1:21587a66f911 | 3 | |
VegardMidt | 4:ae7f8a25c748 | 4 | PwmOut TX1(D9); // Use pin D9 |
VegardMidt | 4:ae7f8a25c748 | 5 | DigitalOut TX2(D8); // Use pin D8 |
VegardMidt | 4:ae7f8a25c748 | 6 | DigitalOut HVoff(D6); // Use pin D6 |
VegardMidt | 4:ae7f8a25c748 | 7 | InterruptIn signal(D7); // Use pin D7 |
VegardMidt | 4:ae7f8a25c748 | 8 | Timeout to1; |
hebryn | 3:b6f543e4508a | 9 | Timer t1; |
hebryn | 3:b6f543e4508a | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
hebryn | 1:21587a66f911 | 11 | |
VegardMidt | 6:0d4008544238 | 12 | float previous = 1; |
VegardMidt | 6:0d4008544238 | 13 | |
VegardMidt | 4:ae7f8a25c748 | 14 | void TX1_send(){ |
VegardMidt | 6:0d4008544238 | 15 | |
VegardMidt | 6:0d4008544238 | 16 | previous = 1; |
VegardMidt | 4:ae7f8a25c748 | 17 | HVoff = 0; |
VegardMidt | 4:ae7f8a25c748 | 18 | TX1.period(0.0000252); // Set the period of TX1 to 25.2us (39,682kHz) |
VegardMidt | 4:ae7f8a25c748 | 19 | TX1.pulsewidth(0.0000116); // Set the pulsewidth of TX1 to 11.6us (ON) |
VegardMidt | 4:ae7f8a25c748 | 20 | wait(0.0002016); // Waits 201.6us |
VegardMidt | 4:ae7f8a25c748 | 21 | TX1 = 0; |
VegardMidt | 5:b2df25183cb1 | 22 | t1.reset(); |
VegardMidt | 4:ae7f8a25c748 | 23 | HVoff = 1; |
VegardMidt | 6:0d4008544238 | 24 | to1.attach(TX1_send,0.1); // Sends for 1000us |
hebryn | 3:b6f543e4508a | 25 | } |
hebryn | 3:b6f543e4508a | 26 | |
VegardMidt | 4:ae7f8a25c748 | 27 | |
VegardMidt | 4:ae7f8a25c748 | 28 | void signal_reciev(){ |
VegardMidt | 6:0d4008544238 | 29 | float _time = t1.read(); |
VegardMidt | 6:0d4008544238 | 30 | if (_time<0.006 && _time>0.0007){ |
VegardMidt | 4:ae7f8a25c748 | 31 | |
VegardMidt | 6:0d4008544238 | 32 | if (t1.read()>previous){ |
VegardMidt | 6:0d4008544238 | 33 | pc.printf("check"); |
VegardMidt | 6:0d4008544238 | 34 | } |
VegardMidt | 5:b2df25183cb1 | 35 | |
VegardMidt | 6:0d4008544238 | 36 | else{ |
VegardMidt | 6:0d4008544238 | 37 | |
VegardMidt | 6:0d4008544238 | 38 | previous = _time; |
VegardMidt | 6:0d4008544238 | 39 | |
VegardMidt | 6:0d4008544238 | 40 | float cm = 0; |
VegardMidt | 4:ae7f8a25c748 | 41 | |
VegardMidt | 5:b2df25183cb1 | 42 | |
VegardMidt | 6:0d4008544238 | 43 | cm = 34613*_time/2; // Calculate distance |
VegardMidt | 6:0d4008544238 | 44 | pc.printf("%f\r\n", cm); // Prints distance |
VegardMidt | 6:0d4008544238 | 45 | } |
VegardMidt | 5:b2df25183cb1 | 46 | } |
hebryn | 3:b6f543e4508a | 47 | } |
VegardMidt | 4:ae7f8a25c748 | 48 | |
hebryn | 1:21587a66f911 | 49 | int main() { |
VegardMidt | 5:b2df25183cb1 | 50 | |
VegardMidt | 5:b2df25183cb1 | 51 | t1.start(); |
VegardMidt | 5:b2df25183cb1 | 52 | signal.fall(&signal_reciev); |
VegardMidt | 4:ae7f8a25c748 | 53 | TX2 = 1; |
VegardMidt | 5:b2df25183cb1 | 54 | TX1_send(); |
VegardMidt | 4:ae7f8a25c748 | 55 | while(1); |
VegardMidt | 4:ae7f8a25c748 | 56 | } |