Sevro_drive

Dependencies:   UoY-serial

Committer:
ajp109
Date:
Thu Mar 18 10:41:40 2021 +0000
Revision:
1:a7c3f3f3f2e7
Parent:
0:77209603a6fe
Child:
2:061e2fe8ae1a
Initial commit (assessment)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ajp109 0:77209603a6fe 1 #include "mbed.h"
ajp109 0:77209603a6fe 2
ajp109 1:a7c3f3f3f2e7 3 // Initialise D2 as an OPEN DRAIN output
ajp109 1:a7c3f3f3f2e7 4 // It can be used in the same way as a normal DigitalOut
ajp109 1:a7c3f3f3f2e7 5 DigitalInOut trig(D2, PIN_OUTPUT, OpenDrain, 0);
ajp109 1:a7c3f3f3f2e7 6
ajp109 1:a7c3f3f3f2e7 7 // Initialise D3 as an input
ajp109 1:a7c3f3f3f2e7 8 DigitalIn echo(D3);
ajp109 1:a7c3f3f3f2e7 9
ajp109 0:77209603a6fe 10 int main()
ajp109 0:77209603a6fe 11 {
ajp109 1:a7c3f3f3f2e7 12 while(true) {
ajp109 1:a7c3f3f3f2e7 13 // Send 10us trigger pulse
ajp109 1:a7c3f3f3f2e7 14 trig = true;
ajp109 1:a7c3f3f3f2e7 15 wait_us(10);
ajp109 1:a7c3f3f3f2e7 16 trig = false;
ajp109 1:a7c3f3f3f2e7 17
ajp109 1:a7c3f3f3f2e7 18 while(!echo); // Wait for echo pulse to start
ajp109 1:a7c3f3f3f2e7 19 while(echo); // Wait for echo pulse to end
ajp109 1:a7c3f3f3f2e7 20 printf("Echo received\n");
ajp109 1:a7c3f3f3f2e7 21 thread_sleep_for(500);
ajp109 1:a7c3f3f3f2e7 22 }
ajp109 0:77209603a6fe 23 }