RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

Revision:
10:875812a04307
Parent:
8:62b4607c44ca
Child:
11:2aa84e063c49
--- a/main.cpp	Fri Mar 09 09:58:02 2018 +0000
+++ b/main.cpp	Mon Mar 12 11:02:12 2018 +0000
@@ -1,3 +1,6 @@
+//uncommend // in #define PRINT_ENABLE to see prints
+#define PRINT_ENABLE
+
 #include "Verzender.h"
 #include "PowerControl/PowerControl.h"
 #include "PowerControl/EthernetPowerControl.h"
@@ -15,7 +18,10 @@
 
 float tare = 0;
 float massa = 0;
+float calibration = 0;
 bool reed = false;
+bool tareDone = false;
+State nextState;
 
 
 /**
@@ -71,7 +77,7 @@
 
 /**
     Get the average from the analog input over a mount of time
-    
+
     @param amount of time in seconds
     @return average of the analog input
 */
@@ -98,7 +104,7 @@
             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 
+            Read:   Read the mass when the paddle passes the read position and
                     send the data to the receiver.
             Receive: Check if there were messages
 */
@@ -108,10 +114,9 @@
         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);
@@ -122,17 +127,27 @@
                 break;
 
             case State_position:
-                pc.printf("State: position\n\r");
-                if (reed)
-                    setCurrentState(State_tare);
+                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:
-                pc.printf("State: tare\n\r");
+                IF_PRINT_ENABLE(pc.printf("State: tare\n\r"););
                 tare = getAverageSamples(50000);
-                pc.printf("tare = %f\r\n",tare*3.3);
-
+                IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3););
+                tareDone = true;
                 setCurrentState(State_read);
                 break;
 
@@ -141,12 +156,12 @@
                     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);
+                    IF_PRINT_ENABLE(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");
+                        IF_PRINT_ENABLE(pc.printf("ok.\n\r"););
                     } else {
-                        pc.printf("failed.\n\r");
+                        IF_PRINT_ENABLE(pc.printf("failed.\n\r"););
                     }
                     setCurrentState(State_receive);
                 }
@@ -157,13 +172,33 @@
                 if (sent.available()) {
                     state_Packet state;
                     state = sent.read();
-                    if(state.setstate == State_position) {
+                    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;
         }
     }
 }
\ No newline at end of file