Created libraries2

Dependencies:   mbed ultrasonic

Committer:
VegardMidt
Date:
Fri Apr 17 15:33:33 2015 +0000
Revision:
8:454f1f82aca4
Parent:
6:0d4008544238
Child:
9:74d372e08f41
Skriver ut verdier kontinuerlig

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 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 8:454f1f82aca4 8 DigitalOut tresh (D5);
VegardMidt 8:454f1f82aca4 9 DigitalOut high (D4);
VegardMidt 4:ae7f8a25c748 10 Timeout to1;
hebryn 3:b6f543e4508a 11 Timer t1;
hebryn 3:b6f543e4508a 12 Serial pc(SERIAL_TX, SERIAL_RX);
hebryn 1:21587a66f911 13
VegardMidt 6:0d4008544238 14 float previous = 1;
VegardMidt 6:0d4008544238 15
VegardMidt 4:ae7f8a25c748 16 void TX1_send(){
VegardMidt 6:0d4008544238 17
VegardMidt 6:0d4008544238 18 previous = 1;
VegardMidt 8:454f1f82aca4 19 HVoff = 1;
VegardMidt 8:454f1f82aca4 20 //tresh = 0;
VegardMidt 4:ae7f8a25c748 21 TX1.period(0.0000252); // Set the period of TX1 to 25.2us (39,682kHz)
VegardMidt 4:ae7f8a25c748 22 TX1.pulsewidth(0.0000116); // Set the pulsewidth of TX1 to 11.6us (ON)
VegardMidt 8:454f1f82aca4 23 wait(0.000300); // Waits 201.6us
VegardMidt 8:454f1f82aca4 24 TX1 = 1;
VegardMidt 5:b2df25183cb1 25 t1.reset();
VegardMidt 8:454f1f82aca4 26 HVoff = 0;
VegardMidt 8:454f1f82aca4 27 //wait(0.0001);
VegardMidt 8:454f1f82aca4 28 //tresh = 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 8:454f1f82aca4 35 if (_time<0.006){
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 8:454f1f82aca4 58 TX2 = 0;
VegardMidt 8:454f1f82aca4 59 //high = 1;
VegardMidt 5:b2df25183cb1 60 TX1_send();
VegardMidt 4:ae7f8a25c748 61 while(1);
VegardMidt 4:ae7f8a25c748 62 }