Created libraries2

Dependencies:   mbed ultrasonic

Committer:
VegardMidt
Date:
Thu Apr 23 08:21:20 2015 +0000
Revision:
13:2169890a1508
Parent:
12:086122072266
Child:
14:10b62201c9de
Changed the outputs for:; Tx1 (D11) ; Tx2 (D10) ; HVoff (D12) ; signal (D13)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
VegardMidt 0:017111c9207d 1 #include "mbed.h"
VegardMidt 0:017111c9207d 2 #include "ultrasonic.h"
hebryn 1:21587a66f911 3
VegardMidt 13:2169890a1508 4 PwmOut TX1(D11); // Use pin D11
VegardMidt 13:2169890a1508 5 DigitalOut TX2(D10); // Use pin D10
VegardMidt 13:2169890a1508 6 DigitalOut HVoff(D12); // Use pin D12
VegardMidt 13:2169890a1508 7 InterruptIn signal(D13); // Use pin D13
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 13:2169890a1508 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 11:711c539b95b8 20 wait(0.000300); // Waits 201.6us
VegardMidt 13:2169890a1508 21 TX1 = 0;
VegardMidt 5:b2df25183cb1 22 t1.reset();
VegardMidt 13:2169890a1508 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 12:086122072266 30 if (_time<0.006 && _time>0.000245){ // Reads only between 100cm and 1,7cm
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 13:2169890a1508 53 TX2 = 1;
VegardMidt 5:b2df25183cb1 54 TX1_send();
VegardMidt 4:ae7f8a25c748 55 while(1);
VegardMidt 4:ae7f8a25c748 56 }