RF24Network Send example program.
Dependencies: xtoff RF24Network mbed
Fork of RF24Network_Send by
Diff: main.cpp
- Revision:
- 8:62b4607c44ca
- Parent:
- 7:cbdbaf825b4a
- Child:
- 10:875812a04307
--- a/main.cpp Thu Feb 22 14:07:50 2018 +0000 +++ b/main.cpp Thu Mar 08 09:23:05 2018 +0000 @@ -1,21 +1,45 @@ #include "Verzender.h" +#include "PowerControl/PowerControl.h" +#include "PowerControl/EthernetPowerControl.h" #define NUM_SAMPLES 2000 // size of sample series - -Serial pc(USBTX, USBRX); - -DigitalIn pb(p25); +#define USR_POWERDOWN (0x104) Verzender sent; - +Serial pc(USBTX, USBRX); +Timer t1; +Timer t2; +State current_state = State_init; +InterruptIn reedSensor(p25); AnalogIn ain(p17); float tare = 0; float massa = 0; +bool reed = false; -Timer t2; + +/** + Sets the reed status on rising trigger +*/ +void setReed() +{ + reed = true; +} -State current_state = State_init; +/** + resets the reed status on falling trigger +*/ +void dissableReed() +{ + reed = false; +} + + +/** + RSets the current status of the state machine + + @param the state to be set +*/ void setCurrentState( State setState ) { @@ -23,11 +47,12 @@ } -State getCurrentState() -{ - return current_state; -} +/** + Get the average of a given number of samples from the analog input + @param amount of samples + @return average of the analog input +*/ float getAverageSamples(int samples) { float AVERAGE = 0; @@ -44,44 +69,61 @@ return AVERAGE; } -float getAverageTime() +/** + Get the average from the analog input over a mount of time + + @param amount of time in seconds + @return average of the analog input +*/ +float getAverageTime(int time) { t2.start(); t2.reset(); float AVERAGE = 0; int num_samples = 0; - while (!pb &t2.read() <= 1) { + while (num_samples <= NUM_SAMPLES & t2.read() <= time) { float r = ain.read(); AVERAGE += r; num_samples++; } - AVERAGE /= num_samples; num_samples = 0; return AVERAGE; } - - - +/** + Main function: + State machine: + Init: initialization + Position: Checks if the paddle is on tare position + Tare: Set Zeroload point on average of 50000 samples + Read: Read the mass when the paddle passes the read position and + send the data to the receiver. + Receive: Check if there were messages +*/ int main() { while(1) { + reedSensor.fall(&setReed); + reedSensor.rise(&dissableReed); sent.update(); + switch (current_state) { case State_init: + //sent.test(); pc.baud(9600); + PHY_PowerDown(); //Power down Ethernet interface wait_ms(1000); pc.printf("--Verzender--\n\r"); - pb.mode(PullUp); + reedSensor.mode(PullUp); setCurrentState(State_read); payload_t payload; break; case State_position: pc.printf("State: position\n\r"); - if (!pb) + if (reed) setCurrentState(State_tare); break; @@ -95,32 +137,22 @@ break; case State_read: - //pc.printf("State: read\n\r"); - - if (!pb) { - massa = getAverageTime() - tare; - //pc.printf("massa = %f\r\n", massa*3.3); + if (reed) { + massa = getAverageTime(1) - tare; payload.reedsensor = 1; payload.milligram = massa * 3.3; - //pc.printf("Sent packet1 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram); - + 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;