Saltware / Mbed 2 deprecated Water Play

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ProximityController.cpp Source File

ProximityController.cpp

00001 #include "ProximityController.h"
00002 #include "SensorAlarmController.h"
00003 
00004 static Ping pinger(p30);
00005 static DigitalOut alarmled(LED3);
00006 
00007 void ProximityController::update() {
00008     int range;
00009      
00010     pinger.Send();    
00011     Thread::wait(30);
00012     range = pinger.Read_cm()/ 2;   
00013     this->distance = (float)range;
00014     
00015     printf("Read distance: %.3f\r\n", distance);
00016     
00017     if(distance < 0.01) {
00018         SensorAlarmController::buzzOnce();
00019         setLed(true);
00020     } else {
00021         setLed(false);
00022     }
00023 }        
00024 
00025 float ProximityController::getDistance() {
00026     return this->distance;
00027 }
00028 
00029 float ProximityController::getValue() {
00030     if(distance < 0.01) {
00031         this->volume = -1.0f;    
00032     } else {
00033         this->volume = (DISTANCE_TO_BASE - this->distance) * 76.92f;
00034     }
00035     
00036     return this->volume;    
00037 }
00038 
00039 std::string ProximityController::getName() {
00040     return "ProximityController";
00041 }
00042 
00043 void ProximityController::setLed(bool value){
00044     if (value) alarmled = 1;
00045     if (!value) alarmled = 0;
00046 }