Goes with the Master_Main_Alert

Dependencies:   mbed HCSR04

Committer:
varza
Date:
Wed Dec 12 22:00:42 2018 +0000
Revision:
0:fd80952fcd71
Slave_Main_Alert

Who changed what in which revision?

UserRevisionLine numberNew contents of line
varza 0:fd80952fcd71 1 #include "mbed.h"
varza 0:fd80952fcd71 2 #include "HCSR04.h"
varza 0:fd80952fcd71 3 #define PIN_TRIGGER D0
varza 0:fd80952fcd71 4 #define PIN_ECHO D1
varza 0:fd80952fcd71 5
varza 0:fd80952fcd71 6 Timeout response;
varza 0:fd80952fcd71 7
varza 0:fd80952fcd71 8 DigitalOut green(LED_GREEN);
varza 0:fd80952fcd71 9 DigitalOut red(LED_RED);
varza 0:fd80952fcd71 10 DigitalIn sw2(SW2);
varza 0:fd80952fcd71 11 DigitalIn motion(D7);
varza 0:fd80952fcd71 12 Serial pc(USBTX, USBRX );
varza 0:fd80952fcd71 13
varza 0:fd80952fcd71 14 Serial blue(PTC15, PTC14); //tx, rx
varza 0:fd80952fcd71 15
varza 0:fd80952fcd71 16
varza 0:fd80952fcd71 17 int main()
varza 0:fd80952fcd71 18 {
varza 0:fd80952fcd71 19 HCSR04 rangeFinder( PIN_TRIGGER, PIN_ECHO );
varza 0:fd80952fcd71 20 char send = 'Z';
varza 0:fd80952fcd71 21 int rangeFeet;
varza 0:fd80952fcd71 22 float range;
varza 0:fd80952fcd71 23
varza 0:fd80952fcd71 24 pc.baud(9600);
varza 0:fd80952fcd71 25 blue.baud(9600);
varza 0:fd80952fcd71 26
varza 0:fd80952fcd71 27 printf("Slave connecting to the master\n");
varza 0:fd80952fcd71 28
varza 0:fd80952fcd71 29 //--------------------------------------------------------------------------
varza 0:fd80952fcd71 30
varza 0:fd80952fcd71 31 while (true) {
varza 0:fd80952fcd71 32 rangeFinder.startMeas();
varza 0:fd80952fcd71 33 wait(0.5f);
varza 0:fd80952fcd71 34 if ( rangeFinder.getMeas(range) == RANGE_MEAS_VALID )
varza 0:fd80952fcd71 35 {
varza 0:fd80952fcd71 36
varza 0:fd80952fcd71 37 // printf("pre mod range = %f \n\r", range);
varza 0:fd80952fcd71 38 range = (range * 100);
varza 0:fd80952fcd71 39 // printf("post mod range = %f \n\r", range);
varza 0:fd80952fcd71 40 rangeFeet = range;
varza 0:fd80952fcd71 41 // printf("rangeFeet = %i \n\r", rangeFeet);
varza 0:fd80952fcd71 42 }
varza 0:fd80952fcd71 43
varza 0:fd80952fcd71 44
varza 0:fd80952fcd71 45 //--------------------------------------------------------------------------
varza 0:fd80952fcd71 46
varza 0:fd80952fcd71 47
varza 0:fd80952fcd71 48 if(blue.readable())//Determine if there is a character available to read.
varza 0:fd80952fcd71 49 {
varza 0:fd80952fcd71 50 if(rangeFeet > 2900)
varza 0:fd80952fcd71 51 {
varza 0:fd80952fcd71 52 rangeFeet = 0;
varza 0:fd80952fcd71 53 }
varza 0:fd80952fcd71 54 char word = (rangeFeet) + '0';
varza 0:fd80952fcd71 55 //printf("\n word = %c \n\r", word);
varza 0:fd80952fcd71 56 blue.putc(word);
varza 0:fd80952fcd71 57 }
varza 0:fd80952fcd71 58
varza 0:fd80952fcd71 59
varza 0:fd80952fcd71 60 //--------------------------------------------------------------------------
varza 0:fd80952fcd71 61 wait(0.5f); // wait a small period of time
varza 0:fd80952fcd71 62 green = !green; // toggle a led
varza 0:fd80952fcd71 63
varza 0:fd80952fcd71 64 }
varza 0:fd80952fcd71 65 }