ece 4180 lab 3
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem X_NUCLEO_53L0A1 HC_SR04_Ultrasonic_Library
part2.h
00001 #include "mbed.h" 00002 #include "ultrasonic.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 00006 void dist(int distance) 00007 { 00008 //put code here to execute when the distance has changed 00009 pc.printf("Distance %d mm\r\n", distance); 00010 } 00011 00012 ultrasonic mu(p6, p7, .1, 1, &dist); //Set the trigger pin to p6 and the echo pin to p7 00013 //have updates every .1 seconds and a timeout after 1 00014 //second, and call dist when the distance changes 00015 00016 int run_part2() 00017 { 00018 mu.startUpdates();//start measuring the distance 00019 while(1) 00020 { 00021 //Do something else here 00022 mu.checkDistance(); //call checkDistance() as much as possible, as this is where 00023 //the class checks if dist needs to be called. 00024 } 00025 } 00026 00027 //Theremin style demo using HC-SR04 Sonar and a speaker 00028 // moving a hand away/towards sonar changes audio frequency 00029 00030 DigitalOut audio(p26); //output to speaker amp or audio jack 00031 DigitalOut led(LED1); 00032 DigitalOut led2(LED2); 00033 00034 Timeout cycle; 00035 00036 volatile int half_cycle_time = 1; 00037 00038 //two calls to this interrupt routine generates a square wave 00039 void toggle_interrupt() 00040 { 00041 if (half_cycle_time>22000) audio=0; //mute if nothing in range 00042 else audio = !audio; //toggle to make half a square wave 00043 led = !led; 00044 cycle.detach(); 00045 //update time for interrupt activation -change frequency of square wave 00046 cycle.attach_us(&toggle_interrupt, half_cycle_time); 00047 } 00048 void newdist(int distance) 00049 { 00050 //update frequency based on new sonar data 00051 led2 = !led2; 00052 half_cycle_time = distance<<3; 00053 } 00054 //HC-SR04 Sonar module 00055 ultrasonic mu2(p6, p7, .07, 1, &newdist); 00056 //Set the trigger pin to p6 and the echo pin to p7 00057 //have updates every .07 seconds and a timeout after 1 00058 //second, and call newdist when the distance changes 00059 int run_part2_EC() 00060 { 00061 audio = 0; 00062 led = 0; 00063 cycle.attach(&toggle_interrupt, half_cycle_time); 00064 mu2.startUpdates();//start measuring the distance with the sonar 00065 while(1) { 00066 //Do something else here 00067 mu2.checkDistance(); 00068 //call checkDistance() as much as possible, as this is where 00069 //the class checks if dist needs to be called. 00070 } 00071 }
Generated on Wed Jul 13 2022 11:06:00 by 1.7.2