RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

main.cpp

Committer:
pietor
Date:
2018-02-22
Revision:
7:cbdbaf825b4a
Parent:
6:03ba3e18ced2
Child:
8:62b4607c44ca

File content as of revision 7:cbdbaf825b4a:

#include "Verzender.h"

#define NUM_SAMPLES 2000 // size of sample series

Serial pc(USBTX, USBRX);

DigitalIn pb(p25);

Verzender sent;

AnalogIn   ain(p17);

float tare = 0;
float massa = 0;

Timer t2;

State current_state = State_init;

void setCurrentState( State setState )
{
    current_state = setState;
}


State getCurrentState()
{
    return current_state;
}

float getAverageSamples(int samples)
{
    float AVERAGE = 0;
    int num_samples = 0;
    while (num_samples < samples) {
        float r = ain.read();
        AVERAGE += r;
        num_samples++;
    }

    AVERAGE /= num_samples;
    num_samples = 0;

    return AVERAGE;
}

float getAverageTime()
{
    t2.start();
    t2.reset();
    float AVERAGE = 0;
    int num_samples = 0;
    while (!pb &t2.read() <= 1) {
        float r = ain.read();
        AVERAGE += r;
        num_samples++;
    }

    AVERAGE /= num_samples;
    num_samples = 0;

    return AVERAGE;
}




int main()
{
    while(1) {
        sent.update();
        switch (current_state) {
            case State_init:
                pc.baud(9600);
                wait_ms(1000);
                pc.printf("--Verzender--\n\r");
                pb.mode(PullUp);
                setCurrentState(State_read);
                payload_t payload;
                break;

            case State_position:
                pc.printf("State: position\n\r");
                if (!pb)
                    setCurrentState(State_tare);
                break;


            case State_tare:
                pc.printf("State: tare\n\r");
                tare = getAverageSamples(50000);
                pc.printf("tare = %f\r\n",tare*3.3);

                setCurrentState(State_read);
                break;

            case State_read:
                //pc.printf("State: read\n\r");
                
                if (!pb) {
                    massa = getAverageTime() - tare;
                    //pc.printf("massa = %f\r\n", massa*3.3);
                    payload.reedsensor = 1;
                    payload.milligram = massa * 3.3;
                    //pc.printf("Sent packet1 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram);
                    
                    bool ok = sent.write(payload);
                    if (ok) {
                        pc.printf("ok.\n\r");
                    } else {
                        pc.printf("failed.\n\r");
                    }
                    //pc.printf("Sent packet2 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram);
                    
                    setCurrentState(State_receive);
                    //setCurrentState(State_send);
                }

                break;


            case State_receive:
                //pc.printf("State :receive");
                sent.update();
                if (sent.available()) {
                    state_Packet state;
                    state = sent.read();
                    if(state.setstate == State_position) {
                        setCurrentState(State_position);
                        break;
                    }
                }
                setCurrentState(State_read);
                break;
        }
    }
}