Created libraries2
Dependencies: mbed ultrasonic
main.cpp@7:c763b5d61dee, 2015-04-16 (annotated)
- Committer:
- VegardMidt
- Date:
- Thu Apr 16 16:45:58 2015 +0000
- Revision:
- 7:c763b5d61dee
- Parent:
- 6:0d4008544238
- Child:
- 10:066af872cf0e
average
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 | 7:c763b5d61dee | 13 | int US_count = 0; |
VegardMidt | 7:c763b5d61dee | 14 | float US_buffer[100]; |
VegardMidt | 6:0d4008544238 | 15 | |
VegardMidt | 4:ae7f8a25c748 | 16 | void TX1_send(){ |
VegardMidt | 6:0d4008544238 | 17 | |
VegardMidt | 6:0d4008544238 | 18 | previous = 1; |
VegardMidt | 4:ae7f8a25c748 | 19 | HVoff = 0; |
VegardMidt | 4:ae7f8a25c748 | 20 | TX1.period(0.0000252); // Set the period of TX1 to 25.2us (39,682kHz) |
VegardMidt | 4:ae7f8a25c748 | 21 | TX1.pulsewidth(0.0000116); // Set the pulsewidth of TX1 to 11.6us (ON) |
VegardMidt | 4:ae7f8a25c748 | 22 | wait(0.0002016); // Waits 201.6us |
VegardMidt | 4:ae7f8a25c748 | 23 | TX1 = 0; |
VegardMidt | 5:b2df25183cb1 | 24 | t1.reset(); |
VegardMidt | 7:c763b5d61dee | 25 | wait(0.0000116); |
VegardMidt | 4:ae7f8a25c748 | 26 | HVoff = 1; |
VegardMidt | 6:0d4008544238 | 27 | to1.attach(TX1_send,0.1); // Sends for 1000us |
hebryn | 3:b6f543e4508a | 28 | } |
hebryn | 3:b6f543e4508a | 29 | |
VegardMidt | 4:ae7f8a25c748 | 30 | |
VegardMidt | 4:ae7f8a25c748 | 31 | void signal_reciev(){ |
VegardMidt | 4:ae7f8a25c748 | 32 | |
VegardMidt | 7:c763b5d61dee | 33 | float _time = t1.read(); |
VegardMidt | 5:b2df25183cb1 | 34 | |
VegardMidt | 7:c763b5d61dee | 35 | if (_time<0.006 && _time>0.0008){ |
VegardMidt | 6:0d4008544238 | 36 | |
VegardMidt | 7:c763b5d61dee | 37 | if (t1.read()>previous){ |
VegardMidt | 7:c763b5d61dee | 38 | } |
VegardMidt | 7:c763b5d61dee | 39 | else{ |
VegardMidt | 7:c763b5d61dee | 40 | previous = _time; |
VegardMidt | 7:c763b5d61dee | 41 | US_buffer[US_count] = 34613*_time/2; |
VegardMidt | 7:c763b5d61dee | 42 | pc.printf("Forste %d", US_count); |
VegardMidt | 7:c763b5d61dee | 43 | US_count++; |
VegardMidt | 7:c763b5d61dee | 44 | } |
VegardMidt | 5:b2df25183cb1 | 45 | } |
hebryn | 3:b6f543e4508a | 46 | } |
VegardMidt | 4:ae7f8a25c748 | 47 | |
VegardMidt | 7:c763b5d61dee | 48 | float get_avg(){ |
VegardMidt | 7:c763b5d61dee | 49 | signal.fall(&signal_reciev); |
VegardMidt | 7:c763b5d61dee | 50 | TX2 = 1; |
VegardMidt | 7:c763b5d61dee | 51 | TX1_send(); |
VegardMidt | 7:c763b5d61dee | 52 | |
VegardMidt | 7:c763b5d61dee | 53 | while(US_count<20){pc.printf("\r");} |
VegardMidt | 7:c763b5d61dee | 54 | |
VegardMidt | 7:c763b5d61dee | 55 | |
VegardMidt | 7:c763b5d61dee | 56 | float sum = 0; |
VegardMidt | 7:c763b5d61dee | 57 | |
VegardMidt | 7:c763b5d61dee | 58 | for(int i=0;i<20;i++){ |
VegardMidt | 7:c763b5d61dee | 59 | |
VegardMidt | 7:c763b5d61dee | 60 | sum += US_buffer[i]; |
VegardMidt | 7:c763b5d61dee | 61 | US_buffer[i] = 0; |
VegardMidt | 7:c763b5d61dee | 62 | } |
VegardMidt | 7:c763b5d61dee | 63 | US_count = 0; |
VegardMidt | 7:c763b5d61dee | 64 | return sum/20; |
VegardMidt | 7:c763b5d61dee | 65 | } |
hebryn | 1:21587a66f911 | 66 | int main() { |
VegardMidt | 7:c763b5d61dee | 67 | t1.start(); |
VegardMidt | 7:c763b5d61dee | 68 | float nks = get_avg(); |
VegardMidt | 7:c763b5d61dee | 69 | pc.printf("Avg = %f\r\n",nks); |
VegardMidt | 7:c763b5d61dee | 70 | |
VegardMidt | 4:ae7f8a25c748 | 71 | } |