Created libraries2

Dependencies:   mbed ultrasonic

Committer:
VegardMidt
Date:
Mon Apr 27 09:01:43 2015 +0000
Revision:
14:10b62201c9de
Parent:
13:2169890a1508
Snudd om p? Tx2. Tx1 og Tx2 g?r motsatt av hverandre

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 14:10b62201c9de 5 PwmOut TX2(D10);
VegardMidt 14:10b62201c9de 6 //DigitalOut TX2(D10); // Use pin D10
VegardMidt 13:2169890a1508 7 DigitalOut HVoff(D12); // Use pin D12
VegardMidt 13:2169890a1508 8 InterruptIn signal(D13); // Use pin D13
VegardMidt 4:ae7f8a25c748 9 Timeout to1;
hebryn 3:b6f543e4508a 10 Timer t1;
hebryn 3:b6f543e4508a 11 Serial pc(SERIAL_TX, SERIAL_RX);
hebryn 1:21587a66f911 12
VegardMidt 6:0d4008544238 13 float previous = 1;
VegardMidt 6:0d4008544238 14
VegardMidt 4:ae7f8a25c748 15 void TX1_send(){
VegardMidt 6:0d4008544238 16
VegardMidt 6:0d4008544238 17 previous = 1;
VegardMidt 13:2169890a1508 18 HVoff = 0;
VegardMidt 4:ae7f8a25c748 19 TX1.period(0.0000252); // Set the period of TX1 to 25.2us (39,682kHz)
VegardMidt 4:ae7f8a25c748 20 TX1.pulsewidth(0.0000116); // Set the pulsewidth of TX1 to 11.6us (ON)
VegardMidt 14:10b62201c9de 21 wait(0.0000058);
VegardMidt 14:10b62201c9de 22 TX2.period(0.0000252);
VegardMidt 14:10b62201c9de 23 TX2.pulsewidth(0.0000116);
VegardMidt 11:711c539b95b8 24 wait(0.000300); // Waits 201.6us
VegardMidt 13:2169890a1508 25 TX1 = 0;
VegardMidt 14:10b62201c9de 26 TX2 = 0;
VegardMidt 5:b2df25183cb1 27 t1.reset();
VegardMidt 13:2169890a1508 28 HVoff = 1;
VegardMidt 6:0d4008544238 29 to1.attach(TX1_send,0.1); // Sends for 1000us
hebryn 3:b6f543e4508a 30 }
hebryn 3:b6f543e4508a 31
VegardMidt 4:ae7f8a25c748 32
VegardMidt 4:ae7f8a25c748 33 void signal_reciev(){
VegardMidt 6:0d4008544238 34 float _time = t1.read();
VegardMidt 14:10b62201c9de 35 if (_time<0.006 && _time>0.00057){ // Reads only between 100cm and 1,7cm
VegardMidt 4:ae7f8a25c748 36
VegardMidt 6:0d4008544238 37 if (t1.read()>previous){
VegardMidt 6:0d4008544238 38 pc.printf("check");
VegardMidt 6:0d4008544238 39 }
VegardMidt 5:b2df25183cb1 40
VegardMidt 6:0d4008544238 41 else{
VegardMidt 6:0d4008544238 42
VegardMidt 6:0d4008544238 43 previous = _time;
VegardMidt 6:0d4008544238 44
VegardMidt 6:0d4008544238 45 float cm = 0;
VegardMidt 4:ae7f8a25c748 46
VegardMidt 5:b2df25183cb1 47
VegardMidt 6:0d4008544238 48 cm = 34613*_time/2; // Calculate distance
VegardMidt 6:0d4008544238 49 pc.printf("%f\r\n", cm); // Prints distance
VegardMidt 6:0d4008544238 50 }
VegardMidt 5:b2df25183cb1 51 }
hebryn 3:b6f543e4508a 52 }
VegardMidt 4:ae7f8a25c748 53
hebryn 1:21587a66f911 54 int main() {
VegardMidt 5:b2df25183cb1 55
VegardMidt 5:b2df25183cb1 56 t1.start();
VegardMidt 5:b2df25183cb1 57 signal.fall(&signal_reciev);
VegardMidt 14:10b62201c9de 58 //TX2 = 0;
VegardMidt 5:b2df25183cb1 59 TX1_send();
VegardMidt 4:ae7f8a25c748 60 while(1);
VegardMidt 4:ae7f8a25c748 61 }