Emily Wilson / Mbed 2 deprecated ECE4180Lab3

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lidar_theremin.h Source File

lidar_theremin.h

00001 #include "mbed.h"
00002 #include "XNucleo53L0A1.h"
00003 #include <stdio.h>
00004 Serial pc(USBTX,USBRX);
00005 DigitalOut shdn(p25);
00006 // This VL53L0X board test application performs a range measurement in polling mode
00007 // Use 3.3(Vout) for Vin, p28 for SDA, p27 for SCL, P26 for shdn on mbed LPC1768
00008 
00009 //I2C sensor pins
00010 #define VL53L0_I2C_SDA   p28
00011 #define VL53L0_I2C_SCL   p27
00012 
00013 static XNucleo53L0A1 *board=NULL;
00014 
00015 DigitalOut audio(p26); //output to speaker amp or audio jack
00016 DigitalOut led(LED1); 
00017 DigitalOut led2(LED2);
00018 
00019 //Timeout cycle;
00020 Ticker cycle;
00021  
00022 volatile int half_cycle_time = 1;
00023  
00024 //two calls to this interrupt routine generates a square wave
00025 void toggle_interrupt()
00026 {
00027     if (half_cycle_time>22000) audio=0; //mute if nothing in range
00028     else audio = !audio; //toggle to make half a square wave
00029     led = !led;
00030     cycle.detach();
00031     //update time for interrupt activation -change frequency of square wave
00032     cycle.attach_us(&toggle_interrupt, half_cycle_time);
00033 }
00034 
00035 uint32_t distance;
00036 void newdist()
00037 {
00038     //update frequency based on new sonar data
00039     led2 = !led2;
00040     half_cycle_time = (distance*10)<<3;
00041 }  
00042 
00043 //Set the trigger pin to p6 and the echo pin to p7
00044 //have updates every .07 seconds and a timeout after 1
00045 //second, and call newdist when the distance changes
00046 int run_lidar_theremin()
00047 {
00048     int status;
00049     DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
00050     /* creates the 53L0A1 expansion board singleton obj */
00051     board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
00052     shdn = 0; //must reset sensor for an mbed reset to work
00053     wait(0.1);
00054     shdn = 1;
00055     wait(0.1);
00056     /* init the 53L0A1 board with default values */
00057     status = board->init_board();
00058     
00059     while (status) {
00060         pc.printf("Failed to init board! \r\n");
00061         status = board->init_board();
00062     }
00063     //loop taking and printing distance
00064     
00065     audio = 0;
00066     led = 0;
00067     cycle.attach(&toggle_interrupt, half_cycle_time);
00068     while(1) {
00069         //Do something else here
00070         status = board->sensor_centre->get_distance(&distance);
00071         newdist();
00072         pc.printf("distance %d", distance);    
00073         //call checkDistance() as much as possible, as this is where
00074         //the class checks if dist needs to be called.
00075     }
00076 }