Siobhan Duncan / Mbed 2 deprecated HammiltonCircleWorkhopSonar

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /***********************
00002 *
00003 *   This programme reads the values
00004 *   from a sonar sensors and then prints
00005 *   these to the onboard prototyping screen.
00006 *
00007 ************************/
00008 
00009 #include "mbed.h"
00010 #include "ultrasonic.h"
00011 
00012 #define SENREAD_SONAR_READINGSTORES_SIZE 20
00013 #define SENREAD_SONAR_NUMBEROFSENSORS     3
00014 
00015 
00016 const PinName SENPIN_SONAR_FCENTRE_TRIG = p24;
00017 const PinName SENPIN_SONAR_FCENTRE_ECHO = p23;
00018 
00019 
00020 Serial pc(USBTX, USBRX);
00021 
00022 
00023 void SENFUNC_SONAR_FCENTRE(int distance);
00024 
00025  
00026  //Sensor Related
00027 int SENREAD_SONAR_FCENTRE_READING = 1111;
00028 int SENREAD_SONAR_FCENTRE_READERROR = 0;
00029 
00030 int SENREAD_SONAR_MAXREADING = 1000;
00031 int SENREAD_SONAR_MINREADING = 0;
00032 
00033 int SENREAD_SONAR_MAXERROR = 5;
00034 int SENREAD_SONAR_READERRORSTANDARD = 1111;
00035 
00036 int SENREAD_SONAR_FCENTRE_READINGSTORE[SENREAD_SONAR_READINGSTORES_SIZE];
00037 int SENREAD_SONAR_FCENTRE_READINGSTORE_POINTER = SENREAD_SONAR_READINGSTORES_SIZE - 1;
00038 
00039 ultrasonic SENSOR_SONAR_FCENTRE(SENPIN_SONAR_FCENTRE_TRIG, SENPIN_SONAR_FCENTRE_ECHO, 0.5, 1, &SENFUNC_SONAR_FCENTRE); 
00040 
00041 
00042  void SENFUNC_SONAR_FCENTRE(int distance){
00043     //put code here to happen when the distance is changed
00044     SENREAD_SONAR_FCENTRE_READING = distance;
00045 }
00046 
00047 
00048 void printToScreen(){
00049     pc.printf("Value: %d\n\r", SENREAD_SONAR_FCENTRE_READING);
00050 }
00051 
00052 void updateQueue(int *store, int reading, int& element_pointer, int size){
00053         
00054     
00055     if(element_pointer < (size-1)){
00056         element_pointer++;
00057     }
00058     else{
00059         element_pointer = 0;
00060     }
00061     
00062     store[element_pointer] = reading;
00063 }
00064 
00065 int main() {
00066 
00067     wait(1.0);
00068     
00069     //Start the sensors
00070     SENSOR_SONAR_FCENTRE.startUpdates();
00071     pc.printf("Sonar Program Initialising\n\r");
00072     
00073     while(1) {
00074 
00075         SENSOR_SONAR_FCENTRE.checkDistance();
00076         updateQueue(SENREAD_SONAR_FCENTRE_READINGSTORE, SENREAD_SONAR_FCENTRE_READING, SENREAD_SONAR_FCENTRE_READINGSTORE_POINTER, SENREAD_SONAR_READINGSTORES_SIZE);
00077         printToScreen();
00078         wait(0.5);
00079         
00080     }
00081 }