FN

Dependencies:   mbed RF24Network RF24

main.cpp

Committer:
guillaume6544
Date:
2019-02-14
Revision:
5:bfef4ea383be
Parent:
4:f70f3b565af0
Child:
6:d46e3c0c8d35

File content as of revision 5:bfef4ea383be:

#include "mbed.h"
#include <RF24Network.h>
#include <RF24.h>

Serial pc(USBTX, USBRX);
InterruptIn button(D4);

RF24 radio(SPI_MOSI, SPI_MISO, SPI_SCK, D9, SPI_CS );
Timer timer;
unsigned int temps;
int a=0;

void pressed()
{
    button.disable_irq();
    timer.stop();
    temps = timer.read_us();
    printf("distance=%f\r\n",(temps-100.0)*340.0/1000000.0);
    wait_ms(3);
    timer.reset();
    //a=0;
    //button.enable_irq();
}

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 01;

// Address of the other node
const uint16_t other_node = 00;

// How often to send payload packet to the other unit
const unsigned long interval = 200; //ms

// When did we last send?
unsigned long last_sent;
Timer t;

// How many have we sent already
unsigned long packets_sent;
Timer t_packet;

// Structure of our payload
struct payload_t 
{
    unsigned long ms;
    unsigned long counter;
};


int main()
{
  //  double cpt=0;
    
    pc.baud(115200);
    wait_ms(1000);

    pc.printf("mBed RF24Network node: Tx\n");
    radio.begin();
    network.begin(/*channel*/ 90, /*node address*/ this_node);
    wait_ms(2000);
    t.start();
    t_packet.start();
    
    
    button.rise(&pressed);
    while(1) 
    {
         
        // Pump the network regularly
        network.update();
        //if(a==0){
        /* If it's time to send a message, send it! */
        unsigned long now = t.read_ms();
        if ( now >= interval  ) 
        {
            t.reset();
            
            button.enable_irq();

            pc.printf("Sending...");
            payload_t payload_tx;
            payload_tx.ms = t_packet.read_ms();
            payload_tx.counter = packets_sent++;

            RF24NetworkHeader header_tx(/*to node*/ other_node);
            bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
            if (ok){
                timer.start();
                pc.printf("ok.\r\n");
               // a=1;
                }
            else
                pc.printf("failed.\r\n");
        }
       
       
        //}
           
            
            
        
       


    }

}