ece 4180 lab 3

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem X_NUCLEO_53L0A1 HC_SR04_Ultrasonic_Library

part2.h

Committer:
emilywilson
Date:
2020-02-27
Revision:
4:1ef667567942
Parent:
2:4845e2dae429

File content as of revision 4:1ef667567942:

#include "mbed.h"
#include "ultrasonic.h"
 
Serial pc(USBTX, USBRX);

void dist(int distance)
{
    //put code here to execute when the distance has changed
    pc.printf("Distance %d mm\r\n", distance);
}
 
ultrasonic mu(p6, p7, .1, 1, &dist);    //Set the trigger pin to p6 and the echo pin to p7
                                        //have updates every .1 seconds and a timeout after 1
                                        //second, and call dist when the distance changes
 
int run_part2()
{
    mu.startUpdates();//start measuring the distance
    while(1)
    {
        //Do something else here
        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
                                //the class checks if dist needs to be called.
    }
}

//Theremin style demo using HC-SR04 Sonar and a speaker
// moving a hand away/towards sonar changes audio frequency
 
DigitalOut audio(p26); //output to speaker amp or audio jack
DigitalOut led(LED1); 
DigitalOut led2(LED2);
 
Timeout cycle;
 
volatile int half_cycle_time = 1;
 
//two calls to this interrupt routine generates a square wave
void toggle_interrupt()
{
    if (half_cycle_time>22000) audio=0; //mute if nothing in range
    else audio = !audio; //toggle to make half a square wave
    led = !led;
    cycle.detach();
    //update time for interrupt activation -change frequency of square wave
    cycle.attach_us(&toggle_interrupt, half_cycle_time);
}
void newdist(int distance)
{
    //update frequency based on new sonar data
    led2 = !led2;
    half_cycle_time = distance<<3;
}
//HC-SR04 Sonar module
ultrasonic mu2(p6, p7, .07, 1, &newdist);    
//Set the trigger pin to p6 and the echo pin to p7
//have updates every .07 seconds and a timeout after 1
//second, and call newdist when the distance changes
int run_part2_EC()
{
    audio = 0;
    led = 0;
    cycle.attach(&toggle_interrupt, half_cycle_time);
    mu2.startUpdates();//start measuring the distance with the sonar
    while(1) {
        //Do something else here
        mu2.checkDistance();     
        //call checkDistance() as much as possible, as this is where
        //the class checks if dist needs to be called.
    }
}