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@2:fb75a6c7a0de, 2018-04-30 (annotated)
- Committer:
- imomoh
- Date:
- Mon Apr 30 06:12:01 2018 +0000
- Revision:
- 2:fb75a6c7a0de
- Parent:
- 1:5be2682710c6
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
imomoh | 2:fb75a6c7a0de | 1 | /******************************************************************************* |
imomoh | 2:fb75a6c7a0de | 2 | ECE 595 2018 Spring |
imomoh | 2:fb75a6c7a0de | 3 | Project 2 |
imomoh | 2:fb75a6c7a0de | 4 | Group 10 |
imomoh | 2:fb75a6c7a0de | 5 | last Update 4/22/2018 |
imomoh | 2:fb75a6c7a0de | 6 | memo: |
imomoh | 2:fb75a6c7a0de | 7 | *******************************************************************************/ |
imomoh | 2:fb75a6c7a0de | 8 | |
Owen | 0:a51a6e7da590 | 9 | #include "mbed.h" |
Owen | 0:a51a6e7da590 | 10 | #include "nRF24L01P.h" |
imomoh | 2:fb75a6c7a0de | 11 | #include "HCSR04.h" |
imomoh | 2:fb75a6c7a0de | 12 | |
Owen | 0:a51a6e7da590 | 13 | Serial pc(USBTX, USBRX); // tx, rx |
Owen | 0:a51a6e7da590 | 14 | |
imomoh | 2:fb75a6c7a0de | 15 | //nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTC4, PTC12); // mosi, miso, sck, csn, ce, irq |
imomoh | 2:fb75a6c7a0de | 16 | nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTC3, PTC4, PTD0); |
imomoh | 2:fb75a6c7a0de | 17 | DigitalOut myled1(LED_GREEN); |
imomoh | 2:fb75a6c7a0de | 18 | DigitalOut myled2(LED_RED); |
imomoh | 2:fb75a6c7a0de | 19 | |
imomoh | 2:fb75a6c7a0de | 20 | |
imomoh | 2:fb75a6c7a0de | 21 | DigitalIn soundDete(D8); |
imomoh | 2:fb75a6c7a0de | 22 | DigitalIn touch(D4); |
imomoh | 2:fb75a6c7a0de | 23 | DigitalOut buzzer(D3); |
imomoh | 2:fb75a6c7a0de | 24 | DigitalOut Picamera(D2); |
Owen | 0:a51a6e7da590 | 25 | |
imomoh | 2:fb75a6c7a0de | 26 | Timer timer; |
imomoh | 2:fb75a6c7a0de | 27 | HCSR04 rangeFinder(D5, D6); |
Owen | 0:a51a6e7da590 | 28 | |
imomoh | 2:fb75a6c7a0de | 29 | float range; |
imomoh | 2:fb75a6c7a0de | 30 | float tooClose=10; |
imomoh | 2:fb75a6c7a0de | 31 | int warning = 0; |
imomoh | 2:fb75a6c7a0de | 32 | int newevent =0; |
imomoh | 2:fb75a6c7a0de | 33 | |
imomoh | 2:fb75a6c7a0de | 34 | |
imomoh | 2:fb75a6c7a0de | 35 | |
imomoh | 2:fb75a6c7a0de | 36 | |
imomoh | 2:fb75a6c7a0de | 37 | void blink(),takePic(),detectedsound(),checkrange(),shutter(); |
Owen | 0:a51a6e7da590 | 38 | int main() { |
imomoh | 2:fb75a6c7a0de | 39 | |
Owen | 0:a51a6e7da590 | 40 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
Owen | 0:a51a6e7da590 | 41 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
Owen | 0:a51a6e7da590 | 42 | // only handles 4 byte transfers in the ATMega code. |
Owen | 0:a51a6e7da590 | 43 | #define TRANSFER_SIZE 4 |
imomoh | 2:fb75a6c7a0de | 44 | |
Owen | 0:a51a6e7da590 | 45 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
Owen | 0:a51a6e7da590 | 46 | int txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 47 | int rxDataCnt = 0; |
imomoh | 2:fb75a6c7a0de | 48 | |
Owen | 0:a51a6e7da590 | 49 | my_nrf24l01p.powerUp(); |
imomoh | 2:fb75a6c7a0de | 50 | |
Owen | 0:a51a6e7da590 | 51 | // Display the (default) setup of the nRF24L01+ chip |
Owen | 0:a51a6e7da590 | 52 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
Owen | 0:a51a6e7da590 | 53 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
Owen | 0:a51a6e7da590 | 54 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
Owen | 0:a51a6e7da590 | 55 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
Owen | 0:a51a6e7da590 | 56 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
imomoh | 2:fb75a6c7a0de | 57 | |
Owen | 0:a51a6e7da590 | 58 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
imomoh | 2:fb75a6c7a0de | 59 | |
Owen | 0:a51a6e7da590 | 60 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
imomoh | 2:fb75a6c7a0de | 61 | |
imomoh | 2:fb75a6c7a0de | 62 | my_nrf24l01p.setTransmitMode(); |
Owen | 0:a51a6e7da590 | 63 | my_nrf24l01p.enable(); |
imomoh | 2:fb75a6c7a0de | 64 | |
Owen | 0:a51a6e7da590 | 65 | while (1) { |
imomoh | 2:fb75a6c7a0de | 66 | |
imomoh | 2:fb75a6c7a0de | 67 | pc.printf("init\n\r"); |
imomoh | 2:fb75a6c7a0de | 68 | Picamera = 1; |
imomoh | 2:fb75a6c7a0de | 69 | detectedsound(); |
imomoh | 2:fb75a6c7a0de | 70 | checkrange(); |
imomoh | 2:fb75a6c7a0de | 71 | if ( newevent != 1){ |
imomoh | 2:fb75a6c7a0de | 72 | shutter(); |
imomoh | 2:fb75a6c7a0de | 73 | } |
imomoh | 2:fb75a6c7a0de | 74 | if ( warning == 1 ){ |
imomoh | 2:fb75a6c7a0de | 75 | blink(); |
imomoh | 2:fb75a6c7a0de | 76 | if ( newevent == 1){ |
imomoh | 2:fb75a6c7a0de | 77 | takePic(); |
imomoh | 2:fb75a6c7a0de | 78 | |
imomoh | 2:fb75a6c7a0de | 79 | newevent =0; |
imomoh | 2:fb75a6c7a0de | 80 | } |
imomoh | 2:fb75a6c7a0de | 81 | } |
imomoh | 2:fb75a6c7a0de | 82 | |
imomoh | 2:fb75a6c7a0de | 83 | |
imomoh | 2:fb75a6c7a0de | 84 | |
imomoh | 2:fb75a6c7a0de | 85 | |
Owen | 0:a51a6e7da590 | 86 | // If we've received anything over the host serial link... |
imomoh | 2:fb75a6c7a0de | 87 | if ( warning == 1 ) { |
imomoh | 2:fb75a6c7a0de | 88 | |
Owen | 0:a51a6e7da590 | 89 | // ...add it to the transmit buffer |
imomoh | 2:fb75a6c7a0de | 90 | txData[txDataCnt++] = 'w'; |
imomoh | 2:fb75a6c7a0de | 91 | txData[txDataCnt++] = 'w'; |
imomoh | 2:fb75a6c7a0de | 92 | txData[txDataCnt++] = 'w'; |
imomoh | 2:fb75a6c7a0de | 93 | txData[txDataCnt++] = 'w'; |
Owen | 0:a51a6e7da590 | 94 | // If the transmit buffer is full |
Owen | 0:a51a6e7da590 | 95 | if ( txDataCnt >= sizeof( txData ) ) { |
imomoh | 2:fb75a6c7a0de | 96 | |
Owen | 0:a51a6e7da590 | 97 | // Send the transmitbuffer via the nRF24L01+ |
imomoh | 2:fb75a6c7a0de | 98 | pc.printf("message sent"); |
Owen | 0:a51a6e7da590 | 99 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
imomoh | 2:fb75a6c7a0de | 100 | |
Owen | 0:a51a6e7da590 | 101 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 102 | } |
imomoh | 2:fb75a6c7a0de | 103 | |
Owen | 0:a51a6e7da590 | 104 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
Owen | 0:a51a6e7da590 | 105 | myled1 = !myled1; |
imomoh | 2:fb75a6c7a0de | 106 | }else{ |
imomoh | 2:fb75a6c7a0de | 107 | // ...send safe messages |
imomoh | 2:fb75a6c7a0de | 108 | txData[txDataCnt++] = 's'; |
imomoh | 2:fb75a6c7a0de | 109 | txData[txDataCnt++] = 's'; |
imomoh | 2:fb75a6c7a0de | 110 | txData[txDataCnt++] = 's'; |
imomoh | 2:fb75a6c7a0de | 111 | txData[txDataCnt++] = 's'; |
imomoh | 2:fb75a6c7a0de | 112 | // If the transmit buffer is full |
imomoh | 2:fb75a6c7a0de | 113 | if ( txDataCnt >= sizeof( txData ) ) { |
imomoh | 2:fb75a6c7a0de | 114 | |
imomoh | 2:fb75a6c7a0de | 115 | // Send the transmitbuffer via the nRF24L01+ |
imomoh | 2:fb75a6c7a0de | 116 | pc.printf("message sent"); |
imomoh | 2:fb75a6c7a0de | 117 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
imomoh | 2:fb75a6c7a0de | 118 | |
imomoh | 2:fb75a6c7a0de | 119 | txDataCnt = 0; |
Owen | 0:a51a6e7da590 | 120 | } |
Owen | 0:a51a6e7da590 | 121 | } |
Owen | 0:a51a6e7da590 | 122 | } |
Owen | 0:a51a6e7da590 | 123 | } |
imomoh | 2:fb75a6c7a0de | 124 | |
imomoh | 2:fb75a6c7a0de | 125 | |
imomoh | 2:fb75a6c7a0de | 126 | |
imomoh | 2:fb75a6c7a0de | 127 | |
imomoh | 2:fb75a6c7a0de | 128 | void blink(){ |
imomoh | 2:fb75a6c7a0de | 129 | pc.printf("blink\n\r"); |
imomoh | 2:fb75a6c7a0de | 130 | buzzer = 1; |
imomoh | 2:fb75a6c7a0de | 131 | wait(.3); |
imomoh | 2:fb75a6c7a0de | 132 | buzzer = 0; |
imomoh | 2:fb75a6c7a0de | 133 | } |
imomoh | 2:fb75a6c7a0de | 134 | |
imomoh | 2:fb75a6c7a0de | 135 | void takePic(){ |
imomoh | 2:fb75a6c7a0de | 136 | pc.printf("take photo\n\r"); |
imomoh | 2:fb75a6c7a0de | 137 | Picamera = 0; //0 to take pic |
imomoh | 2:fb75a6c7a0de | 138 | wait(.5); |
imomoh | 2:fb75a6c7a0de | 139 | Picamera = 1; //1 not take pic |
imomoh | 2:fb75a6c7a0de | 140 | pc.printf("finish take photo\n\r"); |
imomoh | 2:fb75a6c7a0de | 141 | } |
imomoh | 2:fb75a6c7a0de | 142 | |
imomoh | 2:fb75a6c7a0de | 143 | void detectedsound(){ |
imomoh | 2:fb75a6c7a0de | 144 | pc.printf("detecte sound\n\r"); |
imomoh | 2:fb75a6c7a0de | 145 | if( soundDete == 1){ |
imomoh | 2:fb75a6c7a0de | 146 | if(warning == 0){ |
imomoh | 2:fb75a6c7a0de | 147 | pc.printf("sound too high\n\r"); |
imomoh | 2:fb75a6c7a0de | 148 | warning = 1; |
imomoh | 2:fb75a6c7a0de | 149 | newevent = 1; |
imomoh | 2:fb75a6c7a0de | 150 | } |
imomoh | 2:fb75a6c7a0de | 151 | } |
imomoh | 2:fb75a6c7a0de | 152 | } |
imomoh | 2:fb75a6c7a0de | 153 | |
imomoh | 2:fb75a6c7a0de | 154 | void checkrange(){ |
imomoh | 2:fb75a6c7a0de | 155 | pc.printf("start check range\n\r"); |
imomoh | 2:fb75a6c7a0de | 156 | timer.reset(); |
imomoh | 2:fb75a6c7a0de | 157 | timer.start(); |
imomoh | 2:fb75a6c7a0de | 158 | rangeFinder.startMeasurement(); |
imomoh | 2:fb75a6c7a0de | 159 | while(!rangeFinder.isNewDataReady()){ |
imomoh | 2:fb75a6c7a0de | 160 | } |
imomoh | 2:fb75a6c7a0de | 161 | range = rangeFinder.getDistance_cm(); |
imomoh | 2:fb75a6c7a0de | 162 | pc.printf("range = %5.1f cm\n\r", range); |
imomoh | 2:fb75a6c7a0de | 163 | if ( range < tooClose ){ |
imomoh | 2:fb75a6c7a0de | 164 | pc.printf("too close\n\r"); |
imomoh | 2:fb75a6c7a0de | 165 | if(warning == 0){ |
imomoh | 2:fb75a6c7a0de | 166 | warning = 1; |
imomoh | 2:fb75a6c7a0de | 167 | newevent = 1; |
imomoh | 2:fb75a6c7a0de | 168 | } |
imomoh | 2:fb75a6c7a0de | 169 | } |
imomoh | 2:fb75a6c7a0de | 170 | else{ |
imomoh | 2:fb75a6c7a0de | 171 | if (soundDete == 0 & range>=tooClose){ |
imomoh | 2:fb75a6c7a0de | 172 | pc.printf("not too close, clear warning\n\r"); |
imomoh | 2:fb75a6c7a0de | 173 | warning = 0; |
imomoh | 2:fb75a6c7a0de | 174 | } |
imomoh | 2:fb75a6c7a0de | 175 | } |
imomoh | 2:fb75a6c7a0de | 176 | timer.stop(); |
imomoh | 2:fb75a6c7a0de | 177 | wait_ms(500-timer.read_ms()); |
imomoh | 2:fb75a6c7a0de | 178 | } |
imomoh | 2:fb75a6c7a0de | 179 | |
imomoh | 2:fb75a6c7a0de | 180 | void shutter() |
imomoh | 2:fb75a6c7a0de | 181 | { |
imomoh | 2:fb75a6c7a0de | 182 | if ( touch == 1){ |
imomoh | 2:fb75a6c7a0de | 183 | pc.printf("shutter\n\r"); |
imomoh | 2:fb75a6c7a0de | 184 | takePic(); |
imomoh | 2:fb75a6c7a0de | 185 | } |
imomoh | 2:fb75a6c7a0de | 186 | } |