Search code examples
arduinoreset

Arduino nano data points missing


thanks for helping me out with my previous problem. I am now able to get the data from the arduino to PC using putty with the FSR's attached to a sole and detecting pressures.

The only problem I am facing now is that it seems to be missing instances of Heel strike and Toe Off every now and then.(Image Attached)Plot of data

Blue line represents data from Heel FSR and Red line for Toe FSR It can be seen from the picture that the arduino is missing a Heel Strike at blue coloured peak No 3 and a toe OFF instant at red peak 5 from the end.

Can any one tell me why this is happening regularly.. I am using arduino nano with baudrate 9600 and 2 FSR's ..On increasing the baudrate above 38400 the loss seems to occur more often

/** Code finds instances of toe off and heel strike using data from FSR's*/

    void setup()
    {

      /*************************************************SERIAL**************************************************/
      Serial.begin(9600);


    }

    /*Variables for FSRS*/
    //A0= Toe,A1=Heel
    int  sum_toe_max = 0,sum_toe_min = 0, sum_heel_max = 0, sum_heel_min = 0  ,toe, heel, temp,diff_toe,diff_heel,data_heel=0, data_toe=0,              data_toe2, data_heel2 ;
    int heel_max[5] = {0, 0, 0, 0, 0}, toe_max[5] = {0, 0, 0, 0, 0}; /*These arrays holds the top 5 maximas upto the time of calibration*/
    int heel_min[5]= {100,100,100,100,100}, toe_min[5]={100,100,100,100,100};/*These arrays holds the top 5 maximas upto the time of            calibration*/
    float avg_heel_max,avg_heel_min,avg_toe_max,avg_toe_min;/*variables to hold the averages of the valus of max and min arrays*/
    float UL=0.80,LL=0.05;/* Setting the Limiters*/
    int counter_toe_max = 0, counter_heel_max = 0,counter_toe_min = 0, counter_heel_min = 0;//counter for the number of H and T occured 
    int cal_limit = 10;/*time for which calibration should go on*/
    int timer = 0;/*stores the time elapsed*/

    void loop()
    {

      read_FSR();//Call The FSR function
      //read_acc();


    }


    /*********************************************FSR_TOE***********************************************/
                      /*Function to read the FSR_TOE data and save to SD*/
                      void read_FSR()
                      {


                          data_toe = analogRead(A0);
                          data_heel = analogRead(A1);
                          timer = millis() / 1000;

                          Serial.print(millis());
                          Serial.print(" ");                             
                          Serial.print(data_toe);
                          Serial.print(" ");
                          Serial.println(data_heel);     






                          /*Calibration and finding the maxima uptil cal_limit seconds.*/
                          if (timer < cal_limit)
                          {


                            /*TOE calibration*/

                            /*To find the top 5 max pressures*/
                            if (data_toe > toe_max[counter_toe_max])
                            {
                              toe_max[counter_toe_max] =data_toe;                                                                                                   
                              counter_toe_max = counter_toe_max + 1;


                            }
                            if (counter_toe_max >= 5)
                            {
                              counter_toe_max = 0;
                            }



                            /*To find the top 5 min pressures*/
                            if (data_toe < toe_max[counter_toe_min])
                            {
                              toe_min[counter_toe_min] = data_toe;                                  
                              counter_toe_min = counter_toe_min + 1;

                            }
                            if (counter_toe_min >= 5)
                            {
                              counter_toe_min = 0;
                            }

                            /*HEEL FSR calibration*/
                            /*To find the top 5 max pressures*/

                            if (data_heel > heel_max[counter_heel_max])
                            {
                              heel_max[counter_heel_max] =data_heel;                                                                                                          
                              counter_heel_max = counter_heel_max + 1;

                            }
                            if (counter_heel_max >= 5)
                            {
                              counter_heel_max = 0;
                            }


                            /*To find the top 5 min pressures*/
                            if (data_heel < heel_min[counter_heel_min])
                            {
                              heel_min[counter_heel_min]=data_heel; =                                                                           
                              counter_heel_min = counter_heel_min + 1;

                            }
                            if (counter_heel_min >= 5)
                            {
                              counter_heel_min = 0;
                            }


                          }




                          /*Displaying the maximum and minimum valus array and finding the averages for both the FSR's*/
                          if (timer == cal_limit && temp == 0)
                          {


                            /*Finding sum of the array elements*/
                            for (int i = 0; i < 5; i++)
                            {
                              sum_toe_max = sum_toe_max + toe_max[i];
                              sum_toe_min = sum_toe_min + toe_min[i];


                            }

                            for (int i = 0; i < 5; i++)
                            {
                              sum_heel_max = sum_heel_max + heel_max[i];
                              sum_heel_min = sum_heel_min + heel_min[i];


                            }

                            avg_toe_max = sum_toe_max / 5;/*dividing by 5 to get the avg of the 5 values*/
                            avg_toe_min = sum_toe_min / 5;


                            avg_heel_max = sum_heel_max / 5;
                            avg_heel_min = sum_heel_min / 5;

                            diff_toe = avg_toe_max-avg_toe_min;/*difference between maximas and minimas*/
                            diff_heel = avg_heel_max-avg_heel_min ;




                            Serial.println();
                            Serial.print(F("Avg ToePress max "));
                            Serial.println(avg_toe_max);
                            Serial.print(F("Avg ToePress min "));
                            Serial.println(avg_toe_min);
                            Serial.print(F("Avg HeelPress max "));
                            Serial.println(avg_heel_max);
                            Serial.print(F("Avg HeelPress min "));
                            Serial.println(avg_heel_min);
                            Serial.print(F("Diff in avg toe press:"));
                            Serial.println(diff_toe);
                            Serial.print(F("Diff in avg heel press:"));
                            Serial.println(diff_heel);
                            Serial.print(F("Target HeelPress "));
                            Serial.println(UL*(avg_heel_max-avg_heel_min));
                            Serial.print(F("Target ToePress "));
                            Serial.println(LL*(avg_toe_max-avg_toe_min));


                            temp = temp + 1;
                          }
                          /*Done with calibration( when timer =10s)*/




                          /*Checking the oncoming data for a condition of Toe Off
                            Consider it as a toe off if the normalised value of data_toe i.e (data_toe-avg_toe_min)/diff_toe
                            at the previous instant is greater than LL(Lower Limit) i.e 0.2 times the differnce between the                         averages of max and min of toe FSR 
                            and the normalised value of data_toe2 at the current instant is lesser than the same*/

                          if (timer > cal_limit && (data_toe-avg_toe_min)/diff_toe > LL)
                          {
                            data_toe2 = analogRead(A0);/*Data toe at the current instant*/

                            if ((data_toe2-avg_toe_min)/diff_toe < LL)
                            {

    //                                      
                                  Serial.print(timer*1000);
                                  Serial.print(" ");
                                  Serial.print(("f t T "));
                                  Serial.print(data_toe);
                                  Serial.print("Avg min ");
                                  Serial.print(avg_toe_min);
                                  Serial.print("Diff ");
                                  Serial.print(diff_toe);
                                  Serial.println(" TOE");




                            }


                          }

                          /*Checking the oncoming data for a condition of Heel Stike
                            Consider it as a Heel Strike if the normalised value of data_heel i.e (data_heel2-avg_heel_max)/diff_heel
                            at the previous instant is lesser than UL(Lower Limit) i.e 0.8 times the differnce between the                          averages of max and min of heel FSR 
                            and the normalised value of data_heel2 at the current instant is greater than the same*/

                        if(timer>cal_limit && (data_heel-avg_heel_min)/diff_heel<=UL)
                          {

                            data_heel2=analogRead(A1);/*Data heel at the current instant*/
                            if((data_heel2-avg_heel_min)/diff_heel>=UL)
                            {


                                Serial.print(timer*1000);
                                Serial.print(" ");
                                Serial.print(("f t H "));
                                Serial.print(data_heel);
                                Serial.print(" HEEL");
                                Serial.println(UL);
                                Serial.flush();





                            }

                          }





                      }

Solution

  • As you removed SD usage, could you also remove the #include Does that influence your memory requirements? ( Or even the bad behaviour ? ) Freezing instead of Reset is usually the same cause, just hitting some other address. ;)

    I cannot not see any array out of bounds issue with the current code, but that's where I'd double/triple check...

    Other questions:

    • I2C (Wire): you request 14 bytes, but read only 2 of them ? ...
    • start is a float ? dt should be 0.0, I guess