RF24Network Send example program.
Dependencies: xtoff RF24Network mbed
Fork of RF24Network_Send by
main.cpp
- Committer:
- pietor
- Date:
- 2018-03-12
- Revision:
- 10:875812a04307
- Parent:
- 8:62b4607c44ca
- Child:
- 11:2aa84e063c49
File content as of revision 10:875812a04307:
//uncommend // in #define PRINT_ENABLE to see prints #define PRINT_ENABLE #include "Verzender.h" #include "PowerControl/PowerControl.h" #include "PowerControl/EthernetPowerControl.h" #define NUM_SAMPLES 2000 // size of sample series #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; float calibration = 0; bool reed = false; bool tareDone = false; State nextState; /** Sets the reed status on rising trigger */ void setReed() { reed = true; } /** 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 ) { current_state = setState; } /** 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; int num_samples = 0; while (num_samples < samples) { float r = ain.read(); AVERAGE += r; num_samples++; } AVERAGE /= num_samples; num_samples = 0; return AVERAGE; } /** 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 (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: pc.baud(9600); PHY_PowerDown(); //Power down Ethernet interface wait_ms(1000); pc.printf("--Verzender--\n\r"); reedSensor.mode(PullUp); setCurrentState(State_read); payload_t payload; break; case State_position: IF_PRINT_ENABLE(pc.printf("State: position\n\r");); if (reed) { IF_PRINT_ENABLE(pc.printf("Waiting for 5 seconds\n\r");); wait(5); if (reed and nextState == State_tare) { IF_PRINT_ENABLE(pc.printf("Selected state\n\r");); setCurrentState(State_tare); } else if (reed and nextState == State_calibrate) { setCurrentState(State_calibrate); } else { IF_PRINT_ENABLE(pc.printf("Error on position\n\r");); } } break; case State_tare: IF_PRINT_ENABLE(pc.printf("State: tare\n\r");); tare = getAverageSamples(50000); IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3);); tareDone = true; setCurrentState(State_read); break; case State_read: if (reed) { massa = getAverageTime(1) - tare; payload.reedsensor = 1; payload.milligram = massa * 3.3; IF_PRINT_ENABLE(pc.printf("Sent packet1 -- Reed: %d --- %f mg \r\n",payload.reedsensor, payload.milligram);); bool ok = sent.write(payload); if (ok) { IF_PRINT_ENABLE(pc.printf("ok.\n\r");); } else { IF_PRINT_ENABLE(pc.printf("failed.\n\r");); } setCurrentState(State_receive); } break; case State_receive: sent.update(); if (sent.available()) { state_Packet state; state = sent.read(); if(state.setstate == State_tare) { IF_PRINT_ENABLE(pc.printf("Next state: Tare\n\r");); nextState = State_tare; setCurrentState(State_position); break; } else if ( state.setstate == State_calibrate) { IF_PRINT_ENABLE(pc.printf("Next state: Calibrate\n\r");); nextState = State_calibrate; setCurrentState(State_position); } } setCurrentState(State_read); break; case State_calibrate: if(tareDone == true) { IF_PRINT_ENABLE(pc.printf("State: calibreren\n\r");); IF_PRINT_ENABLE(pc.printf("Put 1kg on paddle...\n\r");); IF_PRINT_ENABLE(pc.printf("Waiting: 10 seconds\n\r");); wait(10); IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r");); calibration = getAverageSamples(50000); IF_PRINT_ENABLE(pc.printf("Calibration done: %f\n\r", calibration);); } else { IF_PRINT_ENABLE(pc.printf("ERROR: TARE FIRST\n\r");); } break; } } }