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
main.cpp
00001 /******************************************************************************* 00002 ECE 595 2018 Spring 00003 Project 2 00004 Group 10 00005 last Update 4/22/2018 00006 memo: 00007 *******************************************************************************/ 00008 00009 #include "mbed.h" 00010 #include "nRF24L01P.h" 00011 #include "HCSR04.h" 00012 00013 Serial pc(USBTX, USBRX); // tx, rx 00014 00015 //nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTC4, PTC12); // mosi, miso, sck, csn, ce, irq 00016 nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTC3, PTC4, PTD0); 00017 DigitalOut myled1(LED_GREEN); 00018 DigitalOut myled2(LED_RED); 00019 00020 00021 DigitalIn soundDete(D8); 00022 DigitalIn touch(D4); 00023 DigitalOut buzzer(D3); 00024 DigitalOut Picamera(D2); 00025 00026 Timer timer; 00027 HCSR04 rangeFinder(D5, D6); 00028 00029 float range; 00030 float tooClose=10; 00031 int warning = 0; 00032 int newevent =0; 00033 00034 00035 00036 00037 void blink(),takePic(),detectedsound(),checkrange(),shutter(); 00038 int main() { 00039 00040 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's 00041 // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) 00042 // only handles 4 byte transfers in the ATMega code. 00043 #define TRANSFER_SIZE 4 00044 00045 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; 00046 int txDataCnt = 0; 00047 int rxDataCnt = 0; 00048 00049 my_nrf24l01p.powerUp(); 00050 00051 // Display the (default) setup of the nRF24L01+ chip 00052 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); 00053 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); 00054 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); 00055 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); 00056 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); 00057 00058 pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); 00059 00060 my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); 00061 00062 my_nrf24l01p.setTransmitMode(); 00063 my_nrf24l01p.enable(); 00064 00065 while (1) { 00066 00067 pc.printf("init\n\r"); 00068 Picamera = 1; 00069 detectedsound(); 00070 checkrange(); 00071 if ( newevent != 1){ 00072 shutter(); 00073 } 00074 if ( warning == 1 ){ 00075 blink(); 00076 if ( newevent == 1){ 00077 takePic(); 00078 00079 newevent =0; 00080 } 00081 } 00082 00083 00084 00085 00086 // If we've received anything over the host serial link... 00087 if ( warning == 1 ) { 00088 00089 // ...add it to the transmit buffer 00090 txData[txDataCnt++] = 'w'; 00091 txData[txDataCnt++] = 'w'; 00092 txData[txDataCnt++] = 'w'; 00093 txData[txDataCnt++] = 'w'; 00094 // If the transmit buffer is full 00095 if ( txDataCnt >= sizeof( txData ) ) { 00096 00097 // Send the transmitbuffer via the nRF24L01+ 00098 pc.printf("message sent"); 00099 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); 00100 00101 txDataCnt = 0; 00102 } 00103 00104 // Toggle LED1 (to help debug Host -> nRF24L01+ communication) 00105 myled1 = !myled1; 00106 }else{ 00107 // ...send safe messages 00108 txData[txDataCnt++] = 's'; 00109 txData[txDataCnt++] = 's'; 00110 txData[txDataCnt++] = 's'; 00111 txData[txDataCnt++] = 's'; 00112 // If the transmit buffer is full 00113 if ( txDataCnt >= sizeof( txData ) ) { 00114 00115 // Send the transmitbuffer via the nRF24L01+ 00116 pc.printf("message sent"); 00117 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); 00118 00119 txDataCnt = 0; 00120 } 00121 } 00122 } 00123 } 00124 00125 00126 00127 00128 void blink(){ 00129 pc.printf("blink\n\r"); 00130 buzzer = 1; 00131 wait(.3); 00132 buzzer = 0; 00133 } 00134 00135 void takePic(){ 00136 pc.printf("take photo\n\r"); 00137 Picamera = 0; //0 to take pic 00138 wait(.5); 00139 Picamera = 1; //1 not take pic 00140 pc.printf("finish take photo\n\r"); 00141 } 00142 00143 void detectedsound(){ 00144 pc.printf("detecte sound\n\r"); 00145 if( soundDete == 1){ 00146 if(warning == 0){ 00147 pc.printf("sound too high\n\r"); 00148 warning = 1; 00149 newevent = 1; 00150 } 00151 } 00152 } 00153 00154 void checkrange(){ 00155 pc.printf("start check range\n\r"); 00156 timer.reset(); 00157 timer.start(); 00158 rangeFinder.startMeasurement(); 00159 while(!rangeFinder.isNewDataReady()){ 00160 } 00161 range = rangeFinder.getDistance_cm(); 00162 pc.printf("range = %5.1f cm\n\r", range); 00163 if ( range < tooClose ){ 00164 pc.printf("too close\n\r"); 00165 if(warning == 0){ 00166 warning = 1; 00167 newevent = 1; 00168 } 00169 } 00170 else{ 00171 if (soundDete == 0 & range>=tooClose){ 00172 pc.printf("not too close, clear warning\n\r"); 00173 warning = 0; 00174 } 00175 } 00176 timer.stop(); 00177 wait_ms(500-timer.read_ms()); 00178 } 00179 00180 void shutter() 00181 { 00182 if ( touch == 1){ 00183 pc.printf("shutter\n\r"); 00184 takePic(); 00185 } 00186 }
Generated on Wed Jul 27 2022 03:16:19 by 1.7.2