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 Owen Edwards

main.cpp

Committer:
imomoh
Date:
2018-04-30
Revision:
2:fb75a6c7a0de
Parent:
1:5be2682710c6

File content as of revision 2:fb75a6c7a0de:

/*******************************************************************************
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(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);

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.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 ( warning == 1 ) {
 
            // ...add it to the transmit buffer
            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;
        }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;
            }
        }
    }
}




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();
        }
    }