RF24Network Send example program.

Dependencies:   xtoff RF24Network mbed

Fork of RF24Network_Send by Akash Vibhute

Revision:
12:38c5efed7950
Parent:
11:2aa84e063c49
diff -r 2aa84e063c49 -r 38c5efed7950 main.cpp
--- a/main.cpp	Wed Mar 21 16:22:34 2018 +0000
+++ b/main.cpp	Tue Jul 10 12:07:26 2018 +0000
@@ -2,30 +2,28 @@
 #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)
-#define MIN_TARE_VALUE 0.7575   //2.5 / 3.3 (2.5V is  zero load and adc is between 0 and 1)µ
+#define MIN_TARE_VALUE 0.500   //2.5 / 3.3 (2.5V is  zero load and adc is between 0 and 1)µ
 
 Verzender sent;
 Serial pc(USBTX, USBRX);
 Timer t1;
-Timer t2;
 State current_state = State_init;
-InterruptIn reedSensor(p25);
-AnalogIn   ain(p17);
+State previous_state;
+
+InterruptIn reedSensor(D9);
+AnalogIn ain(A2); 
+
 
 float tare = 0;
-float massa = 0;
 float calibration = 0;
 bool reed = false;
 bool tareDone = false;
 char nextState;
-float calibrationMass = 1003;
+float calibrationMass = 1000;
 float AVERAGE_TARE = 0;
-float CALIBRATION_OFFSET = 0.0199754;
+float CALIBRATION_OFFSET = 0.019992;
 
 
 /**
@@ -44,19 +42,21 @@
     reed = false;
 }
 
-
 /**
-    RSets the current status of the state machine
+    Sets the current status of the state machine
 
     @param the state to be set
 */
 
 void setCurrentState( State setState )
 {
+    previous_state = current_state;
     current_state = setState;
 }
 
 
+
+
 /**
     Get the average of a given number of samples from the analog input
 
@@ -116,12 +116,12 @@
 
     //calculate total average of 2500 samples
     AVERAGE /= count;
-    AVERAGE = ((AVERAGE - tare)*(calibrationMass))/(CALIBRATION_OFFSET);
+    AVERAGE = ((tare - AVERAGE)*(calibrationMass))/(CALIBRATION_OFFSET);
     count = 0;
 
     //loop over array and check if the samples are within range of total average
     for (int i=0; i< samples; i++) {
-        float massa = ((SUB_AVERAGE_ARRAY[i] - tare)*(calibrationMass))/(CALIBRATION_OFFSET);
+        float massa = ((tare - SUB_AVERAGE_ARRAY[i])*(calibrationMass))/(CALIBRATION_OFFSET);
 
         if (massa < AVERAGE - 30 or massa > AVERAGE + 30) {
             massa = 0;
@@ -148,6 +148,10 @@
 */
 int main()
 {
+    pc.baud(9600);
+    pc.printf("--Verzender2--\n\r");
+    sent.printDetails();
+    sent.sendMessage(STARTUP);
     while(1) {
         reedSensor.fall(&setReed);
         reedSensor.rise(&dissableReed);
@@ -155,14 +159,13 @@
 
         switch (current_state) {
             case State_init:
-                pc.baud(9600);
-                PHY_PowerDown(); //Power down Ethernet interface
+            IF_PRINT_ENABLE(pc.printf("State: Init\n\r"););
+                sent.sendMessage(INIT);
                 wait_ms(1000);
-                pc.printf("--Verzender--\n\r");
                 reedSensor.mode(PullUp);
                 setCurrentState(State_read);
                 payload_t payload;
-                sent.sendMessage(INIT);
+                sent.sendMessage(STARTUP_SUCCES);
                 break;
 
             case State_position:
@@ -197,6 +200,9 @@
                     sent.sendMessage(TARE_COMPLETE);
                     IF_PRINT_ENABLE(pc.printf("tare = %f\r\n",tare*3.3););
                     tareDone = true;
+                } else if (tare <= 0.01) {
+                    sent.sendMessage(BATTERY);
+                    IF_PRINT_ENABLE(pc.printf("ERROR: BATTERY NOT INSERTED\n\r"););
                 } else {
                     sent.sendMessage(TARE_ERROR);
                     IF_PRINT_ENABLE(pc.printf("ERROR: TARE VALUE TO LOW\n\r"););
@@ -206,15 +212,17 @@
                 break;
 
             case State_read:
+                sent.sendMessage(READ);
                 if (reed) {
-                    if (tareDone == true) {
-                        massa = getAverageSamples2(25, 100);
-                        payload.reedsensor = 1;
+                    if (tareDone == true){
+                        float massa = getAverageSamples2(100, 5);
+                        //payload.reedsensor = 1;
                         payload.gram = massa;
-                        IF_PRINT_ENABLE(pc.printf("Sent packet1 -- Reed: %d --- %f g \r\n",payload.reedsensor, payload.gram););
+                        IF_PRINT_ENABLE(pc.printf("%f\r\n", payload.gram););
                         bool ok = sent.write(payload);
                         if (ok) {
-                            IF_PRINT_ENABLE(pc.printf("ok.\n\r"););
+                            IF_PRINT_ENABLE(
+                            pc.printf("ok.\n\r"););
                         } else {
                             IF_PRINT_ENABLE(pc.printf("failed.\n\r"););
                         }
@@ -222,6 +230,7 @@
                         sent.sendMessage(TARE_FIRST);
                         IF_PRINT_ENABLE(pc.printf("Tare First.\n\r"););
                     }
+                
                     setCurrentState(State_receive);
                 }
                 break;
@@ -239,33 +248,35 @@
                         setCurrentState(State_position);
                         break;
                     }
-
                     if(state.setstate == 'c') {
                         IF_PRINT_ENABLE(pc.printf("Next state: Calibrate\n\r"););
                         nextState = 'c';
                         setCurrentState(State_position);
                         break;
                     }
-                }
+               }
 
                 setCurrentState(State_read);
+
                 break;
 
-            case State_calibrate:
-                setCurrentState(State_read);
-                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(1);
-                    IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r"););
-                    calibration = getAverageSamples(1000,10);
-                    IF_PRINT_ENABLE(pc.printf("Calibration=  %f\n\r",  calibration*3.3););
+                
+                case State_calibrate:
+                    setCurrentState(State_read);
+                    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(1);
+                        IF_PRINT_ENABLE(pc.printf("Starting calibration\n\r"););
+                        CALIBRATION_OFFSET = getAverageSamples(1000,10)-tare;
+                        IF_PRINT_ENABLE(pc.printf("Calibration=  %f\n\r",  calibration*3.3););
 
-                } else {
-                    IF_PRINT_ENABLE(pc.printf("ERROR: TARE FIRST\n\r"););
-                }
-                break;
+                    } else {
+                        IF_PRINT_ENABLE(pc.printf("ERROR: TARE FIRST\n\r"););
+                    }
+                    break;
+                
         }
     }
 }
\ No newline at end of file