RF24Network Send example program.
Dependencies: xtoff RF24Network mbed
Fork of RF24Network_Send by
Diff: main.cpp
- Revision:
- 6:03ba3e18ced2
- Parent:
- 5:e6067799a414
- Child:
- 7:cbdbaf825b4a
--- a/main.cpp Mon Feb 12 17:16:26 2018 +0000 +++ b/main.cpp Wed Feb 21 12:09:12 2018 +0000 @@ -1,30 +1,146 @@ #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; + +/* +enum State {State_init, + State_tare, + State_position, + State_read, + State_send, + State_receive + }; +*/ + +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() { - - pc.baud(9600); - wait_ms(1000); - pc.printf("mBed RF24Network node: Tx\n\r"); - pb.mode(PullUp); - while(1) { sent.update(); - if (!pb) { - pc.printf("Sending..."); - payload_t payload; - payload.reedsensor = !pb; - payload.milligram = 521000; + switch (current_state) { + case State_init: + pc.baud(9600); + wait_ms(1000); + pc.printf("--Verzender--Tx\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; - bool ok = sent.write(payload); - if (ok) - pc.printf("ok.\n\r"); - else - pc.printf("failed.\n\r"); + 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 = !pb; + payload.milligram = 2; + setCurrentState(State_send); + } + + break; + + case State_send: + pc.printf("State: send\n\r"); + sent.update(); + bool ok = sent.write(payload); + if (ok) { + pc.printf("ok.\n\r"); + } else { + pc.printf("failed.\n\r"); + } + setCurrentState(State_receive); + 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; } } } \ No newline at end of file