Created libraries2

Dependencies:   mbed ultrasonic

Committer:
VegardMidt
Date:
Tue Apr 21 06:40:31 2015 +0000
Revision:
11:711c539b95b8
Parent:
9:74d372e08f41
Child:
12:086122072266
Dette er en endelig versjon som skriver 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 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 8:454f1f82aca4 17 HVoff = 1;
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 8:454f1f82aca4 21 TX1 = 1;
VegardMidt 5:b2df25183cb1 22 t1.reset();
VegardMidt 8:454f1f82aca4 23 HVoff = 0;
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 11:711c539b95b8 30 if (_time<0.006 && _time>0.0001){ // 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 8:454f1f82aca4 53 TX2 = 0;
VegardMidt 5:b2df25183cb1 54 TX1_send();
VegardMidt 4:ae7f8a25c748 55 while(1);
VegardMidt 4:ae7f8a25c748 56 }