This program reads the values from the sonar sensor and prints these to the screen
Dependencies: HC_SR04_Ultrasonic_Library mbed
main.cpp@0:46268c53a91f, 2017-11-09 (annotated)
- Committer:
- coloursearch
- Date:
- Thu Nov 09 17:17:04 2017 +0000
- Revision:
- 0:46268c53a91f
Initial Commit, this code reads from a sonar sensor and prints to screen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coloursearch | 0:46268c53a91f | 1 | /*********************** |
coloursearch | 0:46268c53a91f | 2 | * |
coloursearch | 0:46268c53a91f | 3 | * This programme reads the values |
coloursearch | 0:46268c53a91f | 4 | * from a sonar sensors and then prints |
coloursearch | 0:46268c53a91f | 5 | * these to the onboard prototyping screen. |
coloursearch | 0:46268c53a91f | 6 | * |
coloursearch | 0:46268c53a91f | 7 | ************************/ |
coloursearch | 0:46268c53a91f | 8 | |
coloursearch | 0:46268c53a91f | 9 | #include "mbed.h" |
coloursearch | 0:46268c53a91f | 10 | #include "ultrasonic.h" |
coloursearch | 0:46268c53a91f | 11 | |
coloursearch | 0:46268c53a91f | 12 | #define SENREAD_SONAR_READINGSTORES_SIZE 20 |
coloursearch | 0:46268c53a91f | 13 | #define SENREAD_SONAR_NUMBEROFSENSORS 3 |
coloursearch | 0:46268c53a91f | 14 | |
coloursearch | 0:46268c53a91f | 15 | |
coloursearch | 0:46268c53a91f | 16 | const PinName SENPIN_SONAR_FCENTRE_TRIG = p24; |
coloursearch | 0:46268c53a91f | 17 | const PinName SENPIN_SONAR_FCENTRE_ECHO = p23; |
coloursearch | 0:46268c53a91f | 18 | |
coloursearch | 0:46268c53a91f | 19 | |
coloursearch | 0:46268c53a91f | 20 | Serial pc(USBTX, USBRX); |
coloursearch | 0:46268c53a91f | 21 | |
coloursearch | 0:46268c53a91f | 22 | |
coloursearch | 0:46268c53a91f | 23 | void SENFUNC_SONAR_FCENTRE(int distance); |
coloursearch | 0:46268c53a91f | 24 | |
coloursearch | 0:46268c53a91f | 25 | |
coloursearch | 0:46268c53a91f | 26 | //Sensor Related |
coloursearch | 0:46268c53a91f | 27 | int SENREAD_SONAR_FCENTRE_READING = 1111; |
coloursearch | 0:46268c53a91f | 28 | int SENREAD_SONAR_FCENTRE_READERROR = 0; |
coloursearch | 0:46268c53a91f | 29 | |
coloursearch | 0:46268c53a91f | 30 | int SENREAD_SONAR_MAXREADING = 1000; |
coloursearch | 0:46268c53a91f | 31 | int SENREAD_SONAR_MINREADING = 0; |
coloursearch | 0:46268c53a91f | 32 | |
coloursearch | 0:46268c53a91f | 33 | int SENREAD_SONAR_MAXERROR = 5; |
coloursearch | 0:46268c53a91f | 34 | int SENREAD_SONAR_READERRORSTANDARD = 1111; |
coloursearch | 0:46268c53a91f | 35 | |
coloursearch | 0:46268c53a91f | 36 | int SENREAD_SONAR_FCENTRE_READINGSTORE[SENREAD_SONAR_READINGSTORES_SIZE]; |
coloursearch | 0:46268c53a91f | 37 | int SENREAD_SONAR_FCENTRE_READINGSTORE_POINTER = SENREAD_SONAR_READINGSTORES_SIZE - 1; |
coloursearch | 0:46268c53a91f | 38 | |
coloursearch | 0:46268c53a91f | 39 | ultrasonic SENSOR_SONAR_FCENTRE(SENPIN_SONAR_FCENTRE_TRIG, SENPIN_SONAR_FCENTRE_ECHO, 0.5, 1, &SENFUNC_SONAR_FCENTRE); |
coloursearch | 0:46268c53a91f | 40 | |
coloursearch | 0:46268c53a91f | 41 | |
coloursearch | 0:46268c53a91f | 42 | void SENFUNC_SONAR_FCENTRE(int distance){ |
coloursearch | 0:46268c53a91f | 43 | //put code here to happen when the distance is changed |
coloursearch | 0:46268c53a91f | 44 | SENREAD_SONAR_FCENTRE_READING = distance; |
coloursearch | 0:46268c53a91f | 45 | } |
coloursearch | 0:46268c53a91f | 46 | |
coloursearch | 0:46268c53a91f | 47 | |
coloursearch | 0:46268c53a91f | 48 | void printToScreen(){ |
coloursearch | 0:46268c53a91f | 49 | pc.printf("Value: %d\n\r", SENREAD_SONAR_FCENTRE_READING); |
coloursearch | 0:46268c53a91f | 50 | } |
coloursearch | 0:46268c53a91f | 51 | |
coloursearch | 0:46268c53a91f | 52 | void updateQueue(int *store, int reading, int& element_pointer, int size){ |
coloursearch | 0:46268c53a91f | 53 | |
coloursearch | 0:46268c53a91f | 54 | |
coloursearch | 0:46268c53a91f | 55 | if(element_pointer < (size-1)){ |
coloursearch | 0:46268c53a91f | 56 | element_pointer++; |
coloursearch | 0:46268c53a91f | 57 | } |
coloursearch | 0:46268c53a91f | 58 | else{ |
coloursearch | 0:46268c53a91f | 59 | element_pointer = 0; |
coloursearch | 0:46268c53a91f | 60 | } |
coloursearch | 0:46268c53a91f | 61 | |
coloursearch | 0:46268c53a91f | 62 | store[element_pointer] = reading; |
coloursearch | 0:46268c53a91f | 63 | } |
coloursearch | 0:46268c53a91f | 64 | |
coloursearch | 0:46268c53a91f | 65 | int main() { |
coloursearch | 0:46268c53a91f | 66 | |
coloursearch | 0:46268c53a91f | 67 | wait(1.0); |
coloursearch | 0:46268c53a91f | 68 | |
coloursearch | 0:46268c53a91f | 69 | //Start the sensors |
coloursearch | 0:46268c53a91f | 70 | SENSOR_SONAR_FCENTRE.startUpdates(); |
coloursearch | 0:46268c53a91f | 71 | pc.printf("Sonar Program Initialising\n\r"); |
coloursearch | 0:46268c53a91f | 72 | |
coloursearch | 0:46268c53a91f | 73 | while(1) { |
coloursearch | 0:46268c53a91f | 74 | |
coloursearch | 0:46268c53a91f | 75 | SENSOR_SONAR_FCENTRE.checkDistance(); |
coloursearch | 0:46268c53a91f | 76 | updateQueue(SENREAD_SONAR_FCENTRE_READINGSTORE, SENREAD_SONAR_FCENTRE_READING, SENREAD_SONAR_FCENTRE_READINGSTORE_POINTER, SENREAD_SONAR_READINGSTORES_SIZE); |
coloursearch | 0:46268c53a91f | 77 | printToScreen(); |
coloursearch | 0:46268c53a91f | 78 | wait(0.5); |
coloursearch | 0:46268c53a91f | 79 | |
coloursearch | 0:46268c53a91f | 80 | } |
coloursearch | 0:46268c53a91f | 81 | } |