Inst Project Submission
Dependencies: C12832 HC_SR04_Ultrasonic_Library mbed
main.cpp@0:f28e44548b08, 2018-08-11 (annotated)
- Committer:
- PCStokes
- Date:
- Sat Aug 11 08:57:24 2018 +0000
- Revision:
- 0:f28e44548b08
Submission
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PCStokes | 0:f28e44548b08 | 1 | #include "mbed.h" |
PCStokes | 0:f28e44548b08 | 2 | #include "C12832.h" |
PCStokes | 0:f28e44548b08 | 3 | |
PCStokes | 0:f28e44548b08 | 4 | DigitalOut trig(p21); //definition of the input signal. |
PCStokes | 0:f28e44548b08 | 5 | DigitalIn echo(p22); //definition of the input signal. |
PCStokes | 0:f28e44548b08 | 6 | |
PCStokes | 0:f28e44548b08 | 7 | DigitalOut led1(LED1); // defines the led 1 used to show the trigger signal is happening. |
PCStokes | 0:f28e44548b08 | 8 | DigitalOut led2(LED2); // defines the led 2 used to show the echo signal is happening. |
PCStokes | 0:f28e44548b08 | 9 | DigitalOut led4(LED4); //defines the led 1 used to show the buzzer is turned on or off. |
PCStokes | 0:f28e44548b08 | 10 | |
PCStokes | 0:f28e44548b08 | 11 | C12832 lcd(p5, p7, p6, p8, p11); //Definition of LCD & Pins. |
PCStokes | 0:f28e44548b08 | 12 | |
PCStokes | 0:f28e44548b08 | 13 | Timer sonar; //definition of the sonar timer. |
PCStokes | 0:f28e44548b08 | 14 | |
PCStokes | 0:f28e44548b08 | 15 | PwmOut spker (p26); //Definition of the speaker. |
PCStokes | 0:f28e44548b08 | 16 | |
PCStokes | 0:f28e44548b08 | 17 | PwmOut r (p23); //Definition of the red led. |
PCStokes | 0:f28e44548b08 | 18 | PwmOut g (p24); //Definition of the green led. |
PCStokes | 0:f28e44548b08 | 19 | PwmOut b (p25); //Definition of the blue led. |
PCStokes | 0:f28e44548b08 | 20 | |
PCStokes | 0:f28e44548b08 | 21 | InterruptIn button(p14); //interrupt used to turn on/off buzzer. "calls function to flip buzzstart". |
PCStokes | 0:f28e44548b08 | 22 | Timer debounce; //debounces button timer. |
PCStokes | 0:f28e44548b08 | 23 | |
PCStokes | 0:f28e44548b08 | 24 | int measurement; //defines the measurement variable. |
PCStokes | 0:f28e44548b08 | 25 | |
PCStokes | 0:f28e44548b08 | 26 | int offset = 0; //definition of the sonar timer offset. |
PCStokes | 0:f28e44548b08 | 27 | |
PCStokes | 0:f28e44548b08 | 28 | float inMin = 200; //minimum value readable in mm. |
PCStokes | 0:f28e44548b08 | 29 | float inMax = 4000; //maximum readable value in mm. |
PCStokes | 0:f28e44548b08 | 30 | float outMin = 0; //minimum mapped output value. |
PCStokes | 0:f28e44548b08 | 31 | float outMax = 1; //maximum mapped output value. |
PCStokes | 0:f28e44548b08 | 32 | float mapping = 0; //mapping value. |
PCStokes | 0:f28e44548b08 | 33 | |
PCStokes | 0:f28e44548b08 | 34 | bool buzzstart; // value flipped from o-1 to turn on off buzzer. |
PCStokes | 0:f28e44548b08 | 35 | |
PCStokes | 0:f28e44548b08 | 36 | float spkerVal=0.5; //initial pwm out value. |
PCStokes | 0:f28e44548b08 | 37 | |
PCStokes | 0:f28e44548b08 | 38 | //ranges used for buzzer. |
PCStokes | 0:f28e44548b08 | 39 | int range1Min = 300; |
PCStokes | 0:f28e44548b08 | 40 | int range2Min = 800; |
PCStokes | 0:f28e44548b08 | 41 | int range3Min = 1200; |
PCStokes | 0:f28e44548b08 | 42 | |
PCStokes | 0:f28e44548b08 | 43 | //ranges used for speaker output. |
PCStokes | 0:f28e44548b08 | 44 | int spkroutnear= 1000; |
PCStokes | 0:f28e44548b08 | 45 | int spkroutmid= 600; |
PCStokes | 0:f28e44548b08 | 46 | int spkroutfar= 200; |
PCStokes | 0:f28e44548b08 | 47 | |
PCStokes | 0:f28e44548b08 | 48 | float mapped_led() //function to create the map value for the leds. |
PCStokes | 0:f28e44548b08 | 49 | { |
PCStokes | 0:f28e44548b08 | 50 | //mapping calculation for the measured distance to a float value between 0 and 1 used for RGB colour gradient shifts colour from green to red as the distance changes. I could have done this more directly but the mapping calculation is a nice bit of code to have to map one range of values to another. |
PCStokes | 0:f28e44548b08 | 51 | mapping = ((measurement - inMin) * (outMax - outMin) / (inMax - inMin) + outMin); |
PCStokes | 0:f28e44548b08 | 52 | //Caps the value for the RGBs between 0 and 1 |
PCStokes | 0:f28e44548b08 | 53 | if (mapping < 0.00f) { //if statement less than 0 return 0. |
PCStokes | 0:f28e44548b08 | 54 | return 0; |
PCStokes | 0:f28e44548b08 | 55 | } else if (mapping > 1.00f) { //if statement greater than 1 return 1. |
PCStokes | 0:f28e44548b08 | 56 | return 1; |
PCStokes | 0:f28e44548b08 | 57 | } else { |
PCStokes | 0:f28e44548b08 | 58 | return mapping; //value to return between 0 and 1. |
PCStokes | 0:f28e44548b08 | 59 | } |
PCStokes | 0:f28e44548b08 | 60 | } |
PCStokes | 0:f28e44548b08 | 61 | |
PCStokes | 0:f28e44548b08 | 62 | void ledCntrl() //function to control the leds pwm value on the application board. |
PCStokes | 0:f28e44548b08 | 63 | { |
PCStokes | 0:f28e44548b08 | 64 | r=mapped_led(); //sets the value of the common anode RGB red to a duty cycle equal to the mapped value. |
PCStokes | 0:f28e44548b08 | 65 | g=1-mapped_led (); //sets the value of the common anode RGB green to a duty cycle of 1 less the mapped value. |
PCStokes | 0:f28e44548b08 | 66 | |
PCStokes | 0:f28e44548b08 | 67 | if (buzzstart==1) { //condition based on bool value used to set the on condition of the onboard speaker. |
PCStokes | 0:f28e44548b08 | 68 | if (measurement <= range1Min) { //if the measurement from the sensor is less than or equal to the lower value then; |
PCStokes | 0:f28e44548b08 | 69 | spker.period(1.0/spkroutnear); //creates the Pwm out for the speaker/frequency to make the sound. |
PCStokes | 0:f28e44548b08 | 70 | spker=spkerVal; // this is the value for the speaker duty cycle output 0 would turn the speaker off. |
PCStokes | 0:f28e44548b08 | 71 | } |
PCStokes | 0:f28e44548b08 | 72 | if (measurement >= range1Min && measurement <= range2Min) { //condition if measurement is less than/equal to range 1 and less than/equal to range 2 do the following; |
PCStokes | 0:f28e44548b08 | 73 | spker.period(1.0/spkroutmid); //creates the Pwm out for the speaker/frequency to make the sound. |
PCStokes | 0:f28e44548b08 | 74 | spker=spkerVal; // this is the value for the speaker duty cycle output 0 would turn the speaker off. |
PCStokes | 0:f28e44548b08 | 75 | } |
PCStokes | 0:f28e44548b08 | 76 | if (measurement >= range2Min && measurement <= range3Min) { //condition if measurement is greater than/equal to range 2 and less than/equal to range 3 do the following; |
PCStokes | 0:f28e44548b08 | 77 | spker.period(1.0/spkroutfar); //creates the Pwm out for the speaker/frequency to make the sound. |
PCStokes | 0:f28e44548b08 | 78 | spker=spkerVal; // this is the value for the speaker duty cycle output 0 would turn the speaker off. |
PCStokes | 0:f28e44548b08 | 79 | } else if (measurement > range3Min) {//condition if measurement is greater than range 3 do the following; |
PCStokes | 0:f28e44548b08 | 80 | spker = 0; //turns off the speaker. |
PCStokes | 0:f28e44548b08 | 81 | } |
PCStokes | 0:f28e44548b08 | 82 | } else if (measurement > range3Min) { //condition if measurement is greater than range 3 do the following; |
PCStokes | 0:f28e44548b08 | 83 | spker = 0; //turns off the speaker. |
PCStokes | 0:f28e44548b08 | 84 | } |
PCStokes | 0:f28e44548b08 | 85 | } |
PCStokes | 0:f28e44548b08 | 86 | void buzzCntrl() //function to control the bool value used to activate the buzzer. |
PCStokes | 0:f28e44548b08 | 87 | { |
PCStokes | 0:f28e44548b08 | 88 | if (debounce.read_ms() >(200)) { //this conditional statement checks to see if the debounce time is greater than x milliseconds. It ignores any detected presses inside of this time. |
PCStokes | 0:f28e44548b08 | 89 | debounce.reset(); //this will reset the button press timer allowing for another press to happen and the debouncing timer to start again. |
PCStokes | 0:f28e44548b08 | 90 | led4=!led4; //flips the condition of led 4. |
PCStokes | 0:f28e44548b08 | 91 | buzzstart = led4.read(); // links the state of buzzstart to led 4. |
PCStokes | 0:f28e44548b08 | 92 | } |
PCStokes | 0:f28e44548b08 | 93 | } |
PCStokes | 0:f28e44548b08 | 94 | void printdim() //LCD Print Function |
PCStokes | 0:f28e44548b08 | 95 | { |
PCStokes | 0:f28e44548b08 | 96 | lcd.cls(); //Clears the LCD |
PCStokes | 0:f28e44548b08 | 97 | lcd.locate(2,15); //Location to be used for measurement information in mm. |
PCStokes | 0:f28e44548b08 | 98 | lcd.printf("Measurement %d mm" ,measurement); //send the measurement to the lcd for display. |
PCStokes | 0:f28e44548b08 | 99 | } |
PCStokes | 0:f28e44548b08 | 100 | void measure_offset () //allows for the measure of software polling timer delay in microseconds used to correct possible errors in the measurement times due to the delay. |
PCStokes | 0:f28e44548b08 | 101 | { |
PCStokes | 0:f28e44548b08 | 102 | while (echo==2) {}; // loops while the value of echo is 2, then following happens. |
PCStokes | 0:f28e44548b08 | 103 | led2 = 0; // condition of led 2 is set to 0. |
PCStokes | 0:f28e44548b08 | 104 | sonar.stop(); // the timer sonar is stopped. |
PCStokes | 0:f28e44548b08 | 105 | offset = sonar.read_us(); // reads the timer |
PCStokes | 0:f28e44548b08 | 106 | } |
PCStokes | 0:f28e44548b08 | 107 | void take_measure() //measure distance between 2cm and 4m. |
PCStokes | 0:f28e44548b08 | 108 | { |
PCStokes | 0:f28e44548b08 | 109 | int A_sum = 0; //sets the initial value for the average sum of the readings. |
PCStokes | 0:f28e44548b08 | 110 | |
PCStokes | 0:f28e44548b08 | 111 | while(1) { //Loop to read Sonar distance values, scale, and print values. |
PCStokes | 0:f28e44548b08 | 112 | trig = 1; // trigger sonar to send a ping |
PCStokes | 0:f28e44548b08 | 113 | led1 = trig; // led1 was set to trigger |
PCStokes | 0:f28e44548b08 | 114 | led2 = 0; // led 2 was set to 0 |
PCStokes | 0:f28e44548b08 | 115 | sonar.reset(); //the sonar timer was reset. |
PCStokes | 0:f28e44548b08 | 116 | wait_us(10.0); // wait period for trigger out signal. |
PCStokes | 0:f28e44548b08 | 117 | trig = 0; // turns of the trigger signal |
PCStokes | 0:f28e44548b08 | 118 | led1 = trig; //sets the led1 to the value of the trigger. |
PCStokes | 0:f28e44548b08 | 119 | //wait for echo high |
PCStokes | 0:f28e44548b08 | 120 | while (echo==0) {}; // loop while the echo = o, then do the following. |
PCStokes | 0:f28e44548b08 | 121 | led2=echo; // sets led 2 equal to the state of echo. |
PCStokes | 0:f28e44548b08 | 122 | sonar.start(); //echo high, so start timer |
PCStokes | 0:f28e44548b08 | 123 | while (echo==1) {}; //loop while echo = 1, then do the following; |
PCStokes | 0:f28e44548b08 | 124 | sonar.stop(); //stop timer |
PCStokes | 0:f28e44548b08 | 125 | for (int A = 0; A < 5; ++A) { //for the increments of I, read the sonar and sum the values, each reading has the software overhead timer delay subtracted and is scaled to mm |
PCStokes | 0:f28e44548b08 | 126 | A_sum += (((sonar.read_us()-offset)/58.0)*10); //value of the sum. |
PCStokes | 0:f28e44548b08 | 127 | } |
PCStokes | 0:f28e44548b08 | 128 | measurement = A_sum/5.00f; //average of the readings by dividing the sum by the number of reading “5”. |
PCStokes | 0:f28e44548b08 | 129 | led2 = 0; //sets the value of the led to 0. |
PCStokes | 0:f28e44548b08 | 130 | wait(0.2); //loop wait period. |
PCStokes | 0:f28e44548b08 | 131 | break; // breaks out of the loop. |
PCStokes | 0:f28e44548b08 | 132 | } |
PCStokes | 0:f28e44548b08 | 133 | } |
PCStokes | 0:f28e44548b08 | 134 | int main() //main function of the application. |
PCStokes | 0:f28e44548b08 | 135 | { |
PCStokes | 0:f28e44548b08 | 136 | r=1; //initial value of the red led. |
PCStokes | 0:f28e44548b08 | 137 | g=1; //initial value of the red green. |
PCStokes | 0:f28e44548b08 | 138 | b=1; //initial value of the blue led. |
PCStokes | 0:f28e44548b08 | 139 | sonar.reset();// resets the value of the sonar. |
PCStokes | 0:f28e44548b08 | 140 | sonar.start(); //starts the sonar timer. |
PCStokes | 0:f28e44548b08 | 141 | measure_offset(); //calls the measure offset value. |
PCStokes | 0:f28e44548b08 | 142 | button.rise(&buzzCntrl); //interrupt in call the buzzer control function. |
PCStokes | 0:f28e44548b08 | 143 | debounce.start(); //starts the button debounce timer. |
PCStokes | 0:f28e44548b08 | 144 | |
PCStokes | 0:f28e44548b08 | 145 | while(1) { // main while loop. |
PCStokes | 0:f28e44548b08 | 146 | take_measure(); //calls the measure function. |
PCStokes | 0:f28e44548b08 | 147 | mapped_led(); // calls the value mapping function for the leds. |
PCStokes | 0:f28e44548b08 | 148 | printdim(); //calls the print function to print the dimensions out the to the led screen. |
PCStokes | 0:f28e44548b08 | 149 | ledCntrl(); // calls the led control function. |
PCStokes | 0:f28e44548b08 | 150 | } |
PCStokes | 0:f28e44548b08 | 151 | } |
PCStokes | 0:f28e44548b08 | 152 | |
PCStokes | 0:f28e44548b08 | 153 |