This program reads the values from the sonar sensor and prints these to the screen

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Files at this revision

API Documentation at this revision

Comitter:
coloursearch
Date:
Thu Nov 09 17:17:04 2017 +0000
Commit message:
Initial Commit, this code reads from a sonar sensor and prints to screen

Changed in this revision

HC_SR04_Ultrasonic_Library.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HC_SR04_Ultrasonic_Library.lib	Thu Nov 09 17:17:04 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 09 17:17:04 2017 +0000
@@ -0,0 +1,81 @@
+/***********************
+*
+*   This programme reads the values
+*   from a sonar sensors and then prints
+*   these to the onboard prototyping screen.
+*
+************************/
+
+#include "mbed.h"
+#include "ultrasonic.h"
+
+#define SENREAD_SONAR_READINGSTORES_SIZE 20
+#define SENREAD_SONAR_NUMBEROFSENSORS     3
+
+
+const PinName SENPIN_SONAR_FCENTRE_TRIG = p24;
+const PinName SENPIN_SONAR_FCENTRE_ECHO = p23;
+
+
+Serial pc(USBTX, USBRX);
+
+
+void SENFUNC_SONAR_FCENTRE(int distance);
+
+ 
+ //Sensor Related
+int SENREAD_SONAR_FCENTRE_READING = 1111;
+int SENREAD_SONAR_FCENTRE_READERROR = 0;
+
+int SENREAD_SONAR_MAXREADING = 1000;
+int SENREAD_SONAR_MINREADING = 0;
+
+int SENREAD_SONAR_MAXERROR = 5;
+int SENREAD_SONAR_READERRORSTANDARD = 1111;
+
+int SENREAD_SONAR_FCENTRE_READINGSTORE[SENREAD_SONAR_READINGSTORES_SIZE];
+int SENREAD_SONAR_FCENTRE_READINGSTORE_POINTER = SENREAD_SONAR_READINGSTORES_SIZE - 1;
+
+ultrasonic SENSOR_SONAR_FCENTRE(SENPIN_SONAR_FCENTRE_TRIG, SENPIN_SONAR_FCENTRE_ECHO, 0.5, 1, &SENFUNC_SONAR_FCENTRE); 
+
+
+ void SENFUNC_SONAR_FCENTRE(int distance){
+    //put code here to happen when the distance is changed
+    SENREAD_SONAR_FCENTRE_READING = distance;
+}
+
+
+void printToScreen(){
+    pc.printf("Value: %d\n\r", SENREAD_SONAR_FCENTRE_READING);
+}
+
+void updateQueue(int *store, int reading, int& element_pointer, int size){
+        
+    
+    if(element_pointer < (size-1)){
+        element_pointer++;
+    }
+    else{
+        element_pointer = 0;
+    }
+    
+    store[element_pointer] = reading;
+}
+
+int main() {
+
+    wait(1.0);
+    
+    //Start the sensors
+    SENSOR_SONAR_FCENTRE.startUpdates();
+    pc.printf("Sonar Program Initialising\n\r");
+    
+    while(1) {
+
+        SENSOR_SONAR_FCENTRE.checkDistance();
+        updateQueue(SENREAD_SONAR_FCENTRE_READINGSTORE, SENREAD_SONAR_FCENTRE_READING, SENREAD_SONAR_FCENTRE_READINGSTORE_POINTER, SENREAD_SONAR_READINGSTORES_SIZE);
+        printToScreen();
+        wait(0.5);
+        
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Nov 09 17:17:04 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b
\ No newline at end of file