Created libraries2
Dependencies: mbed ultrasonic
main.cpp
- Committer:
- VegardMidt
- Date:
- 2015-04-16
- Revision:
- 7:c763b5d61dee
- Parent:
- 6:0d4008544238
- Child:
- 10:066af872cf0e
File content as of revision 7:c763b5d61dee:
#include "mbed.h" #include "ultrasonic.h" PwmOut TX1(D9); // Use pin D9 DigitalOut TX2(D8); // Use pin D8 DigitalOut HVoff(D6); // Use pin D6 InterruptIn signal(D7); // Use pin D7 Timeout to1; Timer t1; Serial pc(SERIAL_TX, SERIAL_RX); float previous = 1; int US_count = 0; float US_buffer[100]; void TX1_send(){ previous = 1; HVoff = 0; TX1.period(0.0000252); // Set the period of TX1 to 25.2us (39,682kHz) TX1.pulsewidth(0.0000116); // Set the pulsewidth of TX1 to 11.6us (ON) wait(0.0002016); // Waits 201.6us TX1 = 0; t1.reset(); wait(0.0000116); HVoff = 1; to1.attach(TX1_send,0.1); // Sends for 1000us } void signal_reciev(){ float _time = t1.read(); if (_time<0.006 && _time>0.0008){ if (t1.read()>previous){ } else{ previous = _time; US_buffer[US_count] = 34613*_time/2; pc.printf("Forste %d", US_count); US_count++; } } } float get_avg(){ signal.fall(&signal_reciev); TX2 = 1; TX1_send(); while(US_count<20){pc.printf("\r");} float sum = 0; for(int i=0;i<20;i++){ sum += US_buffer[i]; US_buffer[i] = 0; } US_count = 0; return sum/20; } int main() { t1.start(); float nks = get_avg(); pc.printf("Avg = %f\r\n",nks); }