A program designed to get the distance from an SRF02 distance sensor and create an audible and visual indication of that distance with data logging capabilities.
Dependencies: N5110 PowerControl SRF02 mbed
main.h@10:b61013738793, 2015-04-23 (annotated)
- Committer:
- el13sr
- Date:
- Thu Apr 23 15:12:49 2015 +0000
- Revision:
- 10:b61013738793
- Parent:
- 7:65bdd2d0d6ad
- Child:
- 11:41052065c916
Fixed bug when measuring 30 cm the display not responding.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el13sr | 7:65bdd2d0d6ad | 1 | /** |
el13sr | 7:65bdd2d0d6ad | 2 | * @file main.h |
el13sr | 7:65bdd2d0d6ad | 3 | * @brief Distance Sensor Project |
el13sr | 7:65bdd2d0d6ad | 4 | * @brief Header file containing functions, definitions and global variables. |
el13sr | 7:65bdd2d0d6ad | 5 | * @brief Version: 0.6 |
el13sr | 7:65bdd2d0d6ad | 6 | * @author Sam Russell |
el13sr | 7:65bdd2d0d6ad | 7 | * @date March 2015 |
el13sr | 7:65bdd2d0d6ad | 8 | */ |
el13sr | 7:65bdd2d0d6ad | 9 | |
el13sr | 7:65bdd2d0d6ad | 10 | /********************************************//** |
el13sr | 7:65bdd2d0d6ad | 11 | * Libraries and Definitions |
el13sr | 7:65bdd2d0d6ad | 12 | ***********************************************/ |
el13sr | 0:7d01de89a1ff | 13 | |
el13sr | 0:7d01de89a1ff | 14 | #include "mbed.h" |
el13sr | 0:7d01de89a1ff | 15 | #include "SRF02.h" |
el13sr | 0:7d01de89a1ff | 16 | #include "N5110.h" |
el13sr | 6:d6afc4026a1d | 17 | #include "PowerControl/PowerControl.h" |
el13sr | 6:d6afc4026a1d | 18 | #include "PowerControl/EthernetPowerControl.h" |
el13sr | 7:65bdd2d0d6ad | 19 | #define USR_POWERDOWN (0x104) |
el13sr | 7:65bdd2d0d6ad | 20 | LocalFileSystem local("local"); /*!<Creates local filesystem */ |
el13sr | 7:65bdd2d0d6ad | 21 | Ticker timer; /*!<defines the timer */ |
el13sr | 0:7d01de89a1ff | 22 | |
el13sr | 10:b61013738793 | 23 | /** |
el13sr | 10:b61013738793 | 24 | * @namespace PC |
el13sr | 10:b61013738793 | 25 | * @brief Used to transmit serial data to the PC via USB. |
el13sr | 10:b61013738793 | 26 | */ |
el13sr | 10:b61013738793 | 27 | Serial pc(USBTX,USBRX); |
el13sr | 10:b61013738793 | 28 | |
el13sr | 7:65bdd2d0d6ad | 29 | /********************************************//** |
el13sr | 7:65bdd2d0d6ad | 30 | * Inputs |
el13sr | 7:65bdd2d0d6ad | 31 | ***********************************************/ |
el13sr | 7:65bdd2d0d6ad | 32 | /** |
el13sr | 7:65bdd2d0d6ad | 33 | * @namespace SRF02 |
el13sr | 7:65bdd2d0d6ad | 34 | * @brief Data (SDA) and clock (SCL) for the sensor. |
el13sr | 7:65bdd2d0d6ad | 35 | */ |
el13sr | 7:65bdd2d0d6ad | 36 | SRF02 SRF02(p28,p27); |
el13sr | 7:65bdd2d0d6ad | 37 | /** |
el13sr | 7:65bdd2d0d6ad | 38 | * @namespace Switch |
el13sr | 7:65bdd2d0d6ad | 39 | * @brief On/Off (BOOLEAN) states, OFF: Does not log data to file ON: Dos Log Data to file. |
el13sr | 7:65bdd2d0d6ad | 40 | */ |
el13sr | 7:65bdd2d0d6ad | 41 | AnalogIn Switch(p16); |
el13sr | 7:65bdd2d0d6ad | 42 | /** |
el13sr | 7:65bdd2d0d6ad | 43 | * @namespace Button |
el13sr | 7:65bdd2d0d6ad | 44 | * @brief Three states, Toggles between Normal, Quiet and Power Saving modes. |
el13sr | 7:65bdd2d0d6ad | 45 | */ |
el13sr | 7:65bdd2d0d6ad | 46 | InterruptIn Button(p17); |
el13sr | 0:7d01de89a1ff | 47 | |
el13sr | 7:65bdd2d0d6ad | 48 | /********************************************//** |
el13sr | 7:65bdd2d0d6ad | 49 | * Outputs |
el13sr | 7:65bdd2d0d6ad | 50 | ***********************************************/ |
el13sr | 7:65bdd2d0d6ad | 51 | /** |
el13sr | 10:b61013738793 | 52 | * @namespace logLED |
el13sr | 7:65bdd2d0d6ad | 53 | * @brief Indicates if the data recorded is being logged to file or not. |
el13sr | 7:65bdd2d0d6ad | 54 | */ |
el13sr | 7:65bdd2d0d6ad | 55 | AnalogOut logLED(p18); |
el13sr | 7:65bdd2d0d6ad | 56 | /** |
el13sr | 7:65bdd2d0d6ad | 57 | * @namespace Display |
el13sr | 7:65bdd2d0d6ad | 58 | * @brief Defines the Nokia N5110 as the output display and all the corresponding pins. |
el13sr | 7:65bdd2d0d6ad | 59 | */ |
el13sr | 7:65bdd2d0d6ad | 60 | /// vcc,sce,rst,dc,mosi,clk,led |
el13sr | 0:7d01de89a1ff | 61 | N5110 display(p7,p8,p9,p10,p11,p13,p26); |
el13sr | 7:65bdd2d0d6ad | 62 | /** |
el13sr | 7:65bdd2d0d6ad | 63 | * @namespace Buzzer |
el13sr | 7:65bdd2d0d6ad | 64 | * @brief defines the buzzer pulse width modulation output. |
el13sr | 7:65bdd2d0d6ad | 65 | */ |
el13sr | 7:65bdd2d0d6ad | 66 | PwmOut buzzer(p21); |
el13sr | 7:65bdd2d0d6ad | 67 | /** |
el13sr | 7:65bdd2d0d6ad | 68 | * @namespace Baclight |
el13sr | 7:65bdd2d0d6ad | 69 | * @brief defines the baclight of the display pulse width modulation output. |
el13sr | 7:65bdd2d0d6ad | 70 | */ |
el13sr | 7:65bdd2d0d6ad | 71 | PwmOut backlight(p26); |
el13sr | 7:65bdd2d0d6ad | 72 | /** |
el13sr | 7:65bdd2d0d6ad | 73 | * @namespace LEDs |
el13sr | 7:65bdd2d0d6ad | 74 | * @brief Defines the set of on board LEDs. |
el13sr | 7:65bdd2d0d6ad | 75 | */ |
el13sr | 0:7d01de89a1ff | 76 | BusOut leds (LED1,LED2,LED3,LED4); //MBED onboard LEDs. |
el13sr | 0:7d01de89a1ff | 77 | |
el13sr | 7:65bdd2d0d6ad | 78 | /********************************************//** |
el13sr | 7:65bdd2d0d6ad | 79 | * Flags/Variables/Buffers |
el13sr | 7:65bdd2d0d6ad | 80 | ***********************************************/ |
el13sr | 0:7d01de89a1ff | 81 | |
el13sr | 7:65bdd2d0d6ad | 82 | int timerflag = 0; /*!<initialise timer flag to equal 0. */ |
el13sr | 7:65bdd2d0d6ad | 83 | int distance = 0; /*!<initialise distance to equal 0. */ |
el13sr | 7:65bdd2d0d6ad | 84 | int buttonFlag = 0; /*!<initialise buttonFlag to be 0, which defines the state of the data logger. */ |
el13sr | 7:65bdd2d0d6ad | 85 | int setTimeFlag = 0; /*!<initialise setTimeFlag to equal 0. */ |
el13sr | 7:65bdd2d0d6ad | 86 | int mode = 0; /*!<initialise mode to equal 0 (normal mode). */ |
el13sr | 7:65bdd2d0d6ad | 87 | |
el13sr | 7:65bdd2d0d6ad | 88 | char rxString[16]; /*!< buffer to store received string */ |
el13sr | 0:7d01de89a1ff | 89 | |
el13sr | 7:65bdd2d0d6ad | 90 | /********************************************//** |
el13sr | 7:65bdd2d0d6ad | 91 | * Functions |
el13sr | 7:65bdd2d0d6ad | 92 | ***********************************************/ |
el13sr | 4:0bd5c189f2f3 | 93 | |
el13sr | 7:65bdd2d0d6ad | 94 | void writeDataToFile(char* data, float Distancedata); /*!<Creates the void which takes the two types of data into the function. */ |
el13sr | 7:65bdd2d0d6ad | 95 | void welcomeScreen(); /*!<Function that generates and clears the welcome screen */ |
el13sr | 7:65bdd2d0d6ad | 96 | void barChart(); /*!<Function to update the bar chart on the display. */ |
el13sr | 7:65bdd2d0d6ad | 97 | void error(int code); /*!<DEBUG: Error Function */ |
el13sr | 7:65bdd2d0d6ad | 98 | void timerExpired(); /*!<Function for when the timer expires. */ |
el13sr | 7:65bdd2d0d6ad | 99 | float avgDist(); /*!<Function to get the average distance. */ |
el13sr | 7:65bdd2d0d6ad | 100 | void buttonPressed(); /*!<Function to change mode once button is pressed.*/ |