this is 1/2 of the whole project, this is the part of the slave board that has all the sensors on it, controls the rasberri pi through GPIO, gets sensor data, and sends alert messages through the nrf RF transceiver module to the master board
Dependencies: HCSR04 mbed nRF24L01P
Fork of nRF24L01P_Hello_World by
Revision 2:fb75a6c7a0de, committed 2018-04-30
- Comitter:
- imomoh
- Date:
- Mon Apr 30 06:12:01 2018 +0000
- Parent:
- 1:5be2682710c6
- Commit message:
- first commit
Changed in this revision
diff -r 5be2682710c6 -r fb75a6c7a0de HCSR04.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04.lib Mon Apr 30 06:12:01 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#cf3e4e307d15
diff -r 5be2682710c6 -r fb75a6c7a0de main.cpp --- a/main.cpp Wed Jan 19 23:53:19 2011 +0000 +++ b/main.cpp Mon Apr 30 06:12:01 2018 +0000 @@ -1,75 +1,186 @@ +/******************************************************************************* +ECE 595 2018 Spring +Project 2 +Group 10 +last Update 4/22/2018 +memo: +*******************************************************************************/ + #include "mbed.h" #include "nRF24L01P.h" - +#include "HCSR04.h" + Serial pc(USBTX, USBRX); // tx, rx -nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq +//nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTC4, PTC12); // mosi, miso, sck, csn, ce, irq +nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTC3, PTC4, PTD0); +DigitalOut myled1(LED_GREEN); +DigitalOut myled2(LED_RED); + + +DigitalIn soundDete(D8); +DigitalIn touch(D4); +DigitalOut buzzer(D3); +DigitalOut Picamera(D2); -DigitalOut myled1(LED1); -DigitalOut myled2(LED2); +Timer timer; +HCSR04 rangeFinder(D5, D6); +float range; +float tooClose=10; +int warning = 0; +int newevent =0; + + + + +void blink(),takePic(),detectedsound(),checkrange(),shutter(); int main() { - + // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) // only handles 4 byte transfers in the ATMega code. #define TRANSFER_SIZE 4 - + char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; int txDataCnt = 0; int rxDataCnt = 0; - + my_nrf24l01p.powerUp(); - + // Display the (default) setup of the nRF24L01+ chip pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); - + pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); - + my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); - - my_nrf24l01p.setReceiveMode(); + + my_nrf24l01p.setTransmitMode(); my_nrf24l01p.enable(); - + while (1) { - + + pc.printf("init\n\r"); + Picamera = 1; + detectedsound(); + checkrange(); + if ( newevent != 1){ + shutter(); + } + if ( warning == 1 ){ + blink(); + if ( newevent == 1){ + takePic(); + + newevent =0; + } + } + + + + // If we've received anything over the host serial link... - if ( pc.readable() ) { - + if ( warning == 1 ) { + // ...add it to the transmit buffer - txData[txDataCnt++] = pc.getc(); - + txData[txDataCnt++] = 'w'; + txData[txDataCnt++] = 'w'; + txData[txDataCnt++] = 'w'; + txData[txDataCnt++] = 'w'; // If the transmit buffer is full if ( txDataCnt >= sizeof( txData ) ) { - + // Send the transmitbuffer via the nRF24L01+ + pc.printf("message sent"); my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); - + txDataCnt = 0; } - + // Toggle LED1 (to help debug Host -> nRF24L01+ communication) myled1 = !myled1; - } - - // If we've received anything in the nRF24L01+... - if ( my_nrf24l01p.readable() ) { - - // ...read the data into the receive buffer - rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); - - // Display the receive buffer contents via the host serial link - for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { - - pc.putc( rxData[i] ); + }else{ + // ...send safe messages + txData[txDataCnt++] = 's'; + txData[txDataCnt++] = 's'; + txData[txDataCnt++] = 's'; + txData[txDataCnt++] = 's'; + // If the transmit buffer is full + if ( txDataCnt >= sizeof( txData ) ) { + + // Send the transmitbuffer via the nRF24L01+ + pc.printf("message sent"); + my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); + + txDataCnt = 0; } - - // Toggle LED2 (to help debug nRF24L01+ -> Host communication) - myled2 = !myled2; } } } + + + + +void blink(){ + pc.printf("blink\n\r"); + buzzer = 1; + wait(.3); + buzzer = 0; + } + +void takePic(){ + pc.printf("take photo\n\r"); + Picamera = 0; //0 to take pic + wait(.5); + Picamera = 1; //1 not take pic + pc.printf("finish take photo\n\r"); + } + +void detectedsound(){ + pc.printf("detecte sound\n\r"); + if( soundDete == 1){ + if(warning == 0){ + pc.printf("sound too high\n\r"); + warning = 1; + newevent = 1; + } + } + } + +void checkrange(){ + pc.printf("start check range\n\r"); + timer.reset(); + timer.start(); + rangeFinder.startMeasurement(); + while(!rangeFinder.isNewDataReady()){ + } + range = rangeFinder.getDistance_cm(); + pc.printf("range = %5.1f cm\n\r", range); + if ( range < tooClose ){ + pc.printf("too close\n\r"); + if(warning == 0){ + warning = 1; + newevent = 1; + } + } + else{ + if (soundDete == 0 & range>=tooClose){ + pc.printf("not too close, clear warning\n\r"); + warning = 0; + } + } + timer.stop(); + wait_ms(500-timer.read_ms()); +} + +void shutter() + { + if ( touch == 1){ + pc.printf("shutter\n\r"); + takePic(); + } + }
diff -r 5be2682710c6 -r fb75a6c7a0de mbed.bld --- a/mbed.bld Wed Jan 19 23:53:19 2011 +0000 +++ b/mbed.bld Mon Apr 30 06:12:01 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e +https://os.mbed.com/users/mbed_official/code/mbed/builds/994bdf8177cb \ No newline at end of file