Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 3 1 2 3 LastLast
Results 1 to 25 of 55

Thread: rewriting the functions to closed void functions

  1. #1
    Senior Member
    Join Date
    Sep 2016
    Posts
    137

    rewriting the functions to closed void functions

    Hi fellow engineers,

    i had a hunch and wanted to test something:

    could someone tell me why i am not getting the values from my function in the other function?
    the debugging from my code will continue tomorrow, but to prevail and get a cleaner code I wanted to test some code online.
    the values from the static double ArrTemp[]; should resemble an array of 4 Thermocouples K that are a readout by the MAX31855, and should be transferred to another function void receiutveFunction( double *p) and printed out.

    i would like to know how to do this, because then all of my code will be turned to voids and I can integrate timing calls for the other functions and hopefully get no TFT screen freeze. see previous posts:
    HTML Code:
    https://forum.pjrc.com/threads/63152-More-questions-SPI-Manual-Correctly-Previous-Post-MAX31855-Screen-Freezes-why-CLK?highlight=max31855

    could someone tell me what I am doing wrong? or is this not possible in the arduino environment with the libraries I have?
    i have later the same idea with my encoder function that writes the int menu to the TFT function

    Code:
    #include <stdio.h>
    Void
    #include <stdio.h>
    
    
    void *passArray()
    {
       static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    
    
      int i;
      for (i = 0; i < 4; i++)
        {
          printf ("value inside Array is:\n ");
          
          ArrTemp[i];
          
       printf("Array[%d] = %.2lf", i, ArrTemp[i] );
        
        }
      return ArrTemp;
    
    }
    
    
    
    
    int
    main()
    {
      printf ("print array to another function\n");
       
    
       double *p;
       p=passArray();
       receiveFunction(); 
         
    }
    
    
    
    void receiveFunction( double *p)
    {
        
      for (int i = 0; i < 4; i++)
        {
          //printf ("*(p + %d) : %d\n", i, *(p + i));
          printf("Receive Element[%d] = %.2lf\n", i, p[i] );
        }
      return 0;
    }
    the output data needs to be double as same as the input data from the array.

  2. #2
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    solved:

    Code:
    #include <stdio.h>
    
    
    void *passArray()
    {
       static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    
    
      int i;
      for (i = 0; i < 4; i++)
        {
          printf ("value inside Array is:\n ");
          
          ArrTemp[i];
          
       printf("Array[%d] = %.2lf", i, ArrTemp[i] );
        
        }
      return ArrTemp;
    
    }
    
    
    
    
    int
    main()
    {
      printf ("print array to another function\n");
       
    
       double *p=0;
       p=passArray();
       for(int i=0;i<4;i++)
       {
       printf("Arraypass[%d] = %.2lf\n", i, p[i] );
       }
       receiveFunction(p); 
         
    }
    
    
    
    void receiveFunction(double *p )
    {
        
      for (int i = 0; i < 4; i++)
        {
          //printf ("*(p + %d) : %d\n", i, *(p + i));
          printf("Receive Element[%d] = %lf\n", i, p[i] );
        }
      return 0;
    }

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,407
    Not positive this addresses the question?

    With:
    Code:
    void *passArray() {
       static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    ArrTemp is known only to the function passArray.

    It exposes the pointer to that persistent static memory as a pointer on return .
    Code:
    int main() {
      printf ("print array to another function\n");
       double *p;
       p=passArray();
       receiveFunction( p );   // the param 'p' not present above
       // receiveFunction( passArray() ); // Or use this
         
    }
    For global access within that file without passing pointers move the : static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    Outside the :: void *passArray()

  4. #4
    Senior Member
    Join Date
    Sep 2016
    Posts
    137

    Elsewise Zero

    Hi Defragster,
    Good morning,

    Ok Ill try that, Elsewise it would be Zero I think.

    Best regards

    Bastiaan



    Quote Originally Posted by defragster View Post
    Not positive this addresses the question?

    With:
    Code:
    void *passArray() {
       static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    ArrTemp is known only to the function passArray.

    It exposes the pointer to that persistent static memory as a pointer on return .
    Code:
    int main() {
      printf ("print array to another function\n");
       double *p;
       p=passArray();
       receiveFunction( p );   // the param 'p' not present above
       // receiveFunction( passArray() ); // Or use this
         
    }
    For global access within that file without passing pointers move the : static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    Outside the :: void *passArray()

  5. #5
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    So i have create the void loop like this:

    and I am wondering why i am not getting values from my thermocouple function.

    Code:
    void loop()
    {
    double *p=0; 
     
          unsigned long TC_currentMillis=millis();
          if(TC_currentMillis-TC_previousMillis>=TC_Interval)
          {
           TC_previousMillis=TC_currentMillis;
           p=Thermocouples1_4();
            
     
          }
    
          
          unsigned long ScreencurrentMillis=millis();
          if(ScreencurrentMillis-ScreenpreviousMillis>=ScreenInterval)
          {
            ScreenpreviousMillis=ScreencurrentMillis;
            TFTscreen(p);  
              
          }
         
     
    }
    thermocouple function like this:
    Code:
    for(int i=0;i<4;i++)
         {
         SPI.beginTransaction(SettingsA);
       
          unsigned long TC_currentMillis=millis();
          if(TC_currentMillis-TC_previousMillis>=TC_Interval)
          {
           TC_previousMillis=TC_currentMillis;
       
         if(all_sensors[i]->readCelsius()<0)//if temperature is lower than 0   ////Thermocouple 1
         {
         c[i] = all_sensors[i]->readCelsius();
         c[i+4]=all_sensors[i]->readInternal();
         Serial.print("inside the TC function");
         Serial.print("INTERNAL");
         Serial.println(c[i+4]);
         Serial.print(" Measured ");
         Serial.println(c[i]);
              
         //c[i]=(double)R1_9;
         
         if (isnan(c[i])) {
           Serial.print("Something wrong with Thermocouple ");
      
         } 
         else {
           Serial.print("C = ");
           Serial.println(c[i]);
         }
         }
         else //this is the 0...500
         {
         
         c[i+4]=all_sensors[i]->readInternal();
         c[i+8]= all_sensors[i]->readCelsius();
    
          }
        return c;
        }
         //this is an array containing negative temperature values internal temperature and positive Temperature.
        SPI.endTransaction();//for
     
      }//millis
    //thermocuple function 
    
    }
    Code:
    void TFTscreen(double *p) //Menu 1 //Display
    {
    // get some data
         j += 0.0013;
       currentmicros = micros();
        long value;
      double voltage;
      
     /* all graphics commands have to appear within the loop body. */    
           SPI.beginTransaction(settingsTFT);
         Serial.print("inside the TC function");
         Serial.print("INTERNAL");
         Serial.println(p[i+4]);
         Serial.print(" Measured ");
         Serial.println(p[i]);
          
          Data1.setTextColor(C_WHITE, C_BLACK);
          tft.setTextSize(2);
          tft.setCursor(0, 0);
          tft.print("Emagnet program");
          Data1.print(j,2);
        //  // now let's see the tft update with FlickerFree
          tft.setTextSize(1);
          tft.setCursor(0,20);
          Data1.setTextColor(C_WHITE, C_BLACK);
          tft.print("Temperature readout");
          tft.print(" Voltages from Thermocouples ");
          tft.setTextSize(2);
          Data1.setTextColor(C_WHITE, C_BLACK);
          tft.setCursor(20,45);
          Data1.print(j,2);
          sprintf(str,"TempEmag: %.2lf\n",p[0]);
          Data1.print(str);
          tft.setCursor(20,60);
          Data1.print(c[0]); //C is a global variable!
    
          Data1.print(j,2);
          tft.setCursor(20,75);
          sprintf(str,"TempWater: %.2lf\n",p[1]);
          Data1.print(str);
         
          Data1.print(j,2);
          tft.setCursor(20,90);
          sprintf(str,"TempTEC2: %.2lf\n",p[2]);
          Data1.print(str);
          
          Data1.print(j,2);
          tft.setCursor(20,105);
          sprintf(str,"Temp: %.2lf\n",p[3]);
          Data1.print(str);

  6. #6
    Senior Member
    Join Date
    Jul 2020
    Posts
    398
    p isn't global, its nuked every time through the loop() function.

    If something has to hold state across multiple function calls it must be global in extent.

  7. #7
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    Quote Originally Posted by MarkT View Post
    p isn't global, its nuked every time through the loop() function.

    If something has to hold state across multiple function calls it must be global in extent.
    Ok Ill declare as a Static global pointer.

    Thanks Mark T

  8. #8
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    Why can’t the function double Thermocouples1_4()
    {
    Return Array_thermocuple;
    }
    Be something like this? Why is the function a pointer if handling doubles?
    Double *Thermocouples1_4()
    {}

    Those are probably stupid questions nobody would ask

    I thought that a simple function

    Int simplefunction()
    {
    Return areay_i;

    }

    And give this areay to another function

    Somewhat=simplefunction();
    Int workthis(int somewhat)
    {

    }

    Best regards

    Bastiaan

  9. #9
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,407
    Any array name not indexed is pointer and should be treated that way. And should be a pointer with the type when known as that determines how offsets to the array values are determined.

    This would be correct for:
    Code:
    Double *Thermocouples1_4()
    {
         static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    // ...
         return ArrTemp;
    }
    Versus something like this where the index to the array would be used to return a single value:
    Code:
    Double ThermocouplesVal( in ii )
    {
         static double ArrTemp[] = { 22.33, 22.74, 22.30, 24.00 };
    // ...
         return ArrTemp[ii];
    }

  10. #10
    Senior Member
    Join Date
    Jul 2020
    Posts
    398
    There are no compound values passed as arguments or results in C or C++, everything is a simple numeric/character/
    bool type or a pointer. So arrays cannot be passed by value, nor structs or objects, its always a reference or a pointer.
    Same applies to built in operators, they always act on simple values or pointers.

    This is one of the reasons C is a low level language.

  11. #11
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    So I declared it global at the moment as like this:

    Code:
    double cArray_TC[12]; //thermocouple Array is passed to pointer *pArray_From_TC and given to another function TFTScreen(pArray_From_TC);//well thats the idea.
    double *pArray_From_TC;
    double pArray_Screen;
    still I get Zeros on my display and in the values outputted are 0. I would like to know what I did wrong.
    it doesnt matter if I change this to double static Array or other declaration.

    should I change the If statement TC_currentmillis before in between the for loop and SPI.beginTransaction(SettingsA);


    double *Thermocouples1_4()
    {
    //open the SPI
    //set the Delaywithout millis
    //open up the MAX31855s x4.
    //close it
    }

    Code:
    double *Thermocouples1_4()
    {
    SPI.beginTransaction(SettingsA);
          for(int i=0;i<4;i++)
          {
           
          unsigned long TC_currentMillis=millis();
          if(TC_currentMillis-TC_previousMillis>=TC_Interval)//100ms
          {
           TC_previousMillis=TC_currentMillis;
           
         Serial.print(all_sensors[i]->readInternal());
         Serial.print(" NOT ");
         Serial.println(all_sensors[i]->readCelsius());
    
         if(all_sensors[i]->readCelsius()<0)//if temperature is lower than 0   ////Thermocouple 1
         {
         cArray_TC[i] = all_sensors[i]->readCelsius();
         cArray_TC[i+4]=all_sensors[i]->readInternal();
         Serial.print("inside the TC function");
         Serial.print("INTERNAL");
         Serial.println(cArray_TC[i+4]);
         Serial.print(" Measured ");
         Serial.println(cArray_TC[i]);
         
         double SubC1_int=cArray_TC[i]-cArray_TC[i+4];
         double SubCalc=SubC1_int*0.041276; /*mV*/
         double E1=(-0.176*pow(10,-1));
         double E2=(0.389*pow(10,-1)*pow( cArray_TC[i+4],1));
         double E3=(0.186*pow(10,-4)*pow( cArray_TC[i+4],2));
         double E4=(-0.995*pow(10,-7)*pow( cArray_TC[i+4],3));
         double E5=(0.318*pow(10,-9)*pow( cArray_TC[i+4],4));
         double E6=(-0.561*pow(10,-12)*pow( cArray_TC[i+4],5));
         double E7=(0.561*pow(10,-15)*pow( cArray_TC[i+4],6));
         double E8=(-0.320*pow(10,-18)*pow( cArray_TC[i+4],7));
         double E9=(0.972*pow(10,-22)*pow( cArray_TC[i+4],8));
         double E10=(-0.121*pow(10,-25)*pow( cArray_TC[i+4],9));
         double E11=(0.119*pow(exp(-0.118*pow(10,-3)*( cArray_TC[i+4]-0.127*pow(10,3))),2));
         double E1_11=E1+E2+E3+E4+E5+E6+E7+E8+E9+E10+E11;
         double AddCJError=E1_11+SubCalc;
         double R1=0+(2.51734*pow(AddCJError,1));
         double R2=(-1.17*pow(AddCJError,2));
         double R3=(-1.08*pow(AddCJError,3));
         double R4=(-8.977*pow(10,-1)*pow(AddCJError,4));
         double R5=(-3.73*10*pow(10,-1)*pow(AddCJError,5));
         double R6=(-8.6632*pow(10,-2)*pow(AddCJError,6));
         double R7=(-1.045*pow(10,-2)*pow(AddCJError,7));
         double R8=(-5.1920577*pow(10,-4)*pow(AddCJError,8));
         double R9=0*pow(AddCJError,9);
         double R1_9=R1+R2+R3+R4+R5+R6+R7+R8+R9;
         Serial.print("Cold ");
         Serial.print("Corr Value ");
         cArray_TC[i]=(double)R1_9;
         return cArray_TC;
           if (isnan( cArray_TC[i])) 
           {
             Serial.print("Something wrong with Thermocouple ");
        
           }
           else {
           Serial.print("C = ");
           Serial.println( cArray_TC[i]);
             return 0;
           }
         }//AllSensors if statement       
         else if(all_sensors[i]->readCelsius()>0) //this is the 0...500
         {
          
         
          cArray_TC[i+4]=all_sensors[i]->readInternal();
          cArray_TC[i+8]=all_sensors[i]->readCelsius();
       
          return cArray_TC;
          
          }
           SPI.endTransaction();
          }
       
         
        }//for
         
         
         //this is an array containing negative temperature values internal temperature and positive Temperature.
      }

    where do I return the cArray_TC? i was experimenting with the Delays and now I am getting zero.

    Code:
    void loop()
    {
    
             unsigned long TC_currentMillis=millis();
          if(TC_currentMillis-TC_previousMillis>=TC_Interval)
          {
           TC_previousMillis=TC_currentMillis;
           pArray_From_TC=Thermocouples1_4();
          } 
         
          unsigned long ScreencurrentMillis=millis();
          if(ScreencurrentMillis-ScreenpreviousMillis>=ScreenInterval)
          {
            ScreenpreviousMillis=ScreencurrentMillis;
             
                TFTscreen(pArray_From_TC);
             
               
          }
    and I declared the TFTscreen() like this.
    Code:
    TFTscreen(double *pArray_Screen)
    {
     SPI.beginTransaction(settingsTFT);
    //use flicker free sprintf, str[30]; 
    //print string on screen
    SPI.EndTransaction(settingsTFT);
    }

    also I get warnings:

    control reaches end of non-void function
    Last edited by Bastiaan; 09-22-2020 at 08:01 AM.

  12. #12
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,407
    With the code in the IDE do a code format :: Ctrl+T
    > the formatting typically will go wrong where the syntax error or missing brace is.
    > Also the editor will show matching braces and parenthesis pairs where one matches the other with a bit of underlining or something.

    Though the problem looks to be that Thermocouples1_4() only has one return with a value in the middle and none at the end where it has a return; by default - not providing the required return value which is what 'control reaches end of non-void function' is saying. Without that the value of pArray_From_TC is always not properly set.

    The Ctrl+T will also give uniform indentation to make following the code easier.

    Having a single code block would better show what the compiler sees to be readable and not guess what goes where and assume nothing is missing.

    Also in loop - if the first 'if()' code doesn't execute before the second then pArray_From_TC won't be defined yet.

  13. #13
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    Quote Originally Posted by defragster View Post
    With the code in the IDE do a code format :: Ctrl+T
    > the formatting typically will go wrong where the syntax error or missing brace is.
    > Also the editor will show matching braces and parenthesis pairs where one matches the other with a bit of underlining or something.

    Though the problem looks to be that Thermocouples1_4() only has one return with a value in the middle and none at the end where it has a return; by default - not providing the required return value which is what 'control reaches end of non-void function' is saying. Without that the value of pArray_From_TC is always not properly set.

    The Ctrl+T will also give uniform indentation to make following the code easier.

    Having a single code block would better show what the compiler sees to be readable and not guess what goes where and assume nothing is missing.

    Also in loop - if the first 'if()' code doesn't execute before the second then pArray_From_TC won't be defined yet.


    that means the first if statement with the variable written into the pArray_From_TC=Thermocouples1_4(); is not executed in the right order I am not getting the value written to the TFTScreen(pArray_To_Screen)?

    second I thought arrays and pointers were closely bound? meaning that if I return a double function I can define it as:

    return cArray_TC; in the Thermocouples1_4(); function.

    if I define return the cArray_TC[i] I get compiler errors.

    i am getting confused, I have run this example on Tutorialpoint with a online C compiler.

    best

    regards

    Bastiaan

  14. #14
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,407
    The compiler warning is actually pointing out a functional ERROR and need to be corrected.

    The two IF()'s are independent - maybe the first on always enters first - but if not then the second if won't be using a valid value until the first IF() is entered - and the code is corrected to always provide the pointer on return.

    Yes an array is just a pointer where double cArray_TC[12]; is just a pointer to double values (double *cArray_TC) that the compiler knows to reserve space for [12] of them, and sets the pointer to that reserved space. And declared as 'double' the code knows that every index after [0] takes an offset of 8 bytes to get to.

    But a pointer not initialized or pointing to a known location is just an accident waiting to happen because a pointer is only that - and has no memopry of its own to point to like double cArray_TC[12]; does.

  15. #15
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    thanks for the explanation.i clearly dont understand the last part.

    so what would be the best approach to write to this variable cArray_TO_Screen;
    i just want to put the temperature on the screen.

    because the return value only gives back the first address of an array I read on a forum.
    pffff....

  16. #16
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,407
    It needs to be properly initialized - that could be done in setup as it only needs to be done once.

    Or the array as global could be used directly, as noted in p#3.

  17. #17
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    currently I have it also executed before the other function. still as global:
    Code:
    double static cArray_TC[12]; //thermocouple Array is passed to pointer *pArray and given to another function TFTScreen(pArray);//well thats the idea.
    double static *pArray_From_TC;
    double static pArray_Screen;
    
    
    //doesnt work
    
    //double static cArray_TC[12]; //thermocouple Array is passed to pointer *pArray and given to //another function TFTScreen(pArray);//well thats the idea.
    //double static *pArray_From_TC[12];
    //double static pArray_Screen[12];cannot convert 'double' to //'double*' in return
    // Error 



    Code:
      unsigned long TC_currentMillis = millis();
      if (TC_currentMillis - TC_previousMillis >= TC_Interval)
      {
        TC_previousMillis = TC_currentMillis;
        pArray_From_TC = Thermocouples1_4();
      }
    
      unsigned long ScreencurrentMillis = millis();
      if (ScreencurrentMillis - ScreenpreviousMillis >= ScreenInterval)
      {
        ScreenpreviousMillis = ScreencurrentMillis;
    
         TFTscreen( Thermocouples1_4());//changed it
      }
    Code:
    double *Thermocouples1_4() //see instanciation pointer to array class TH1,TH2,TH3,TH4
    {
      
      SPI.beginTransaction(SettingsA);
      unsigned long TC_1b1_currentMillis = millis();
      if (TC_1b1_currentMillis - TC_1b1_previousMillis >= TC_1b1_Interval)
      {
        TC_1b1_previousMillis = TC_1b1_currentMillis;
    
        for (int i = 0; i < 4; i++)
        {
          if (all_sensors[i]->readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
          {
            cArray_TC[i] = all_sensors[i]->readCelsius();
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            double SubC1_int = cArray_TC[i] - cArray_TC[i + 4];
            double SubCalc = SubC1_int * 0.041276; /*mV*/
            double E1 = (-0.176 * pow(10, -1));
            double E2 = (0.389 * pow(10, -1) * pow( cArray_TC[i + 4], 1));
            double E3 = (0.186 * pow(10, -4) * pow( cArray_TC[i + 4], 2));
            double E4 = (-0.995 * pow(10, -7) * pow( cArray_TC[i + 4], 3));
            double E5 = (0.318 * pow(10, -9) * pow( cArray_TC[i + 4], 4));
            double E6 = (-0.561 * pow(10, -12) * pow( cArray_TC[i + 4], 5));
            double E7 = (0.561 * pow(10, -15) * pow( cArray_TC[i + 4], 6));
            double E8 = (-0.320 * pow(10, -18) * pow( cArray_TC[i + 4], 7));
            double E9 = (0.972 * pow(10, -22) * pow( cArray_TC[i + 4], 8));
            double E10 = (-0.121 * pow(10, -25) * pow( cArray_TC[i + 4], 9));
            double E11 = (0.119 * pow(exp(-0.118 * pow(10, -3) * ( cArray_TC[i + 4] - 0.127 * pow(10, 3))), 2));
            double E1_11 = E1 + E2 + E3 + E4 + E5 + E6 + E7 + E8 + E9 + E10 + E11;
            double AddCJError = E1_11 + SubCalc;
            double R1 = 0 + (2.51734 * pow(AddCJError, 1));
            double R2 = (-1.17 * pow(AddCJError, 2));
            double R3 = (-1.08 * pow(AddCJError, 3));
            double R4 = (-8.977 * pow(10, -1) * pow(AddCJError, 4));
            double R5 = (-3.73 * 10 * pow(10, -1) * pow(AddCJError, 5));
            double R6 = (-8.6632 * pow(10, -2) * pow(AddCJError, 6));
            double R7 = (-1.045 * pow(10, -2) * pow(AddCJError, 7));
            double R8 = (-5.1920577 * pow(10, -4) * pow(AddCJError, 8));
            double R9 = 0 * pow(AddCJError, 9);
            double R1_9 = R1 + R2 + R3 + R4 + R5 + R6 + R7 + R8 + R9;
            Serial.print("Cold ");
            Serial.print("Corr Value ");
            cArray_TC[i] = (double)R1_9;
            return cArray_TC;
            if (isnan( cArray_TC[i]))
            {
              Serial.print("Something wrong with Thermocouple ");
            }
            else {
              Serial.print("C = ");
              Serial.println( cArray_TC[i]);
            }
          }//AllSensors if statement
          else  //this is the 0...500
          {
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            cArray_TC[i + 8] = all_sensors[i]->readCelsius();
            return cArray_TC; if I do this return cArray_TC[i]; i get error double to double not possible.
            Serial.println("inside the TC function");
            Serial.println(cArray_TC[0]);
            Serial.println(cArray_TC[1]);
            Serial.println(cArray_TC[2]);
            Serial.println(cArray_TC[3]);
            Serial.println(cArray_TC[4]);
            Serial.println(cArray_TC[5]);
            Serial.println(cArray_TC[6]);
            Serial.println(cArray_TC[7]);
            Serial.println(cArray_TC[8]);
            Serial.println(cArray_TC[9]);
            Serial.println(cArray_TC[10]);
            Serial.println(cArray_TC[11]); //values are printed.
    
            SPI.endTransaction();
          }
        }
      }
    }

    Code:
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
    //do something with pArray_Screen[10];// for example.
    
    
    
    }
    i am just feeling stupider i am not seeing it.

    now the pArray_from_TC defined but not used. but I am using the function to write to the double in the other TFTscreen(Thermocouples1_4());

    which is writing Thermocouple return value cArray_TC to the TFTScreen ?
    Last edited by Bastiaan; 09-22-2020 at 10:56 AM. Reason: program runs but screen freezes over no display. just stuff from setup is loaded.

  18. #18
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,681
    Again it really may not help much to spread this out over at least three threads now.

    This code will not work, maybe many reasons, but one most obvious is your return statements!

    It looks like you wish to loop over your 4 sensors... But for example in both cases of the if you do a return statement! Like:
    Code:
         else  //this is the 0...500
          {
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            cArray_TC[i + 8] = all_sensors[i]->readCelsius();
            return cArray_TC; if I do this return cArray_TC[i]; i get error double to double not possible.
            Serial.println("inside the TC function");
            Serial.println(cArray_TC[0]);
            Serial.println(cArray_TC[1]);
            Serial.println(cArray_TC[2]);
            Serial.println(cArray_TC[3]);
            Serial.println(cArray_TC[4]);
            Serial.println(cArray_TC[5]);
            Serial.println(cArray_TC[6]);
            Serial.println(cArray_TC[7]);
            Serial.println(cArray_TC[8]);
            Serial.println(cArray_TC[9]);
            Serial.println(cArray_TC[10]);
            Serial.println(cArray_TC[11]); //values are printed.
    
            SPI.endTransaction();
          }
    The return statement means return, or leave this function now...
    So for example in this case, the code will never do all of these Serial Print functions nor SPI.endTransaction().
    And it will never go beyond i=0 so it will never read from sensors 1-3...

    Likewise there is a return in the other branch of the initial if statement.

  19. #19
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,681
    Also there are other issues with this function as well: Example lets simply concentrate on filling in the array and remove the return statements.
    You have something like:
    Code:
    double *Thermocouples1_4() //see instanciation pointer to array class TH1,TH2,TH3,TH4
    {
      
      SPI.beginTransaction(SettingsA);
      unsigned long TC_1b1_currentMillis = millis();
      if (TC_1b1_currentMillis - TC_1b1_previousMillis >= TC_1b1_Interval)
      {
        TC_1b1_previousMillis = TC_1b1_currentMillis;
    
        for (int i = 0; i < 4; i++)
        {
          if (all_sensors[i]->readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
          {
            cArray_TC[i] = all_sensors[i]->readCelsius();
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            ...
            cArray_TC[i] = (double)R1_9;
            ...
          }//AllSensors if statement
          else  //this is the 0...500
          {
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            cArray_TC[i + 8] = all_sensors[i]->readCelsius();
            ...
            SPI.endTransaction();
          }
        }
      }
    }
    Some simple things here.

    Begin/End Transaction
    : You don't need begin and end transactions... And you remove the issue about return statements... You have one begin statement that is outside of the loop and called every time, but then the endTransaction is called within the loop, so it will only be called when your timing code says it is OK to run again and then every iteration where the temperature is >=0...

    Now for the array values: i, i+4 and I+8

    cArray_TC[i] - will only ever be assigned a value if all_sensors[i]->readCelsius() < 0
    cArray_TC[i + 4] - Will be assigned in both branches of the if to readInternal
    cArray_TC[i + 8] - Will only be assigned if all_sensors[i]->readCelsius() >= 0

    Is this what you really want? Again I don't know the meaning to you of the three indexes here... But for example if the readCelsius > 0 index 0, 4 8 will never be set to anything, so probably 0...

  20. #20
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    Quote Originally Posted by KurtE View Post
    Also there are other issues with this function as well: Example lets simply concentrate on filling in the array and remove the return statements.
    You have something like:
    Code:
    double *Thermocouples1_4() //see instanciation pointer to array class TH1,TH2,TH3,TH4
    {
      
      SPI.beginTransaction(SettingsA);
      unsigned long TC_1b1_currentMillis = millis();
      if (TC_1b1_currentMillis - TC_1b1_previousMillis >= TC_1b1_Interval)
      {
        TC_1b1_previousMillis = TC_1b1_currentMillis;
    
        for (int i = 0; i < 4; i++)
        {
          if (all_sensors[i]->readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
          {
            cArray_TC[i] = all_sensors[i]->readCelsius();
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            ...
            cArray_TC[i] = (double)R1_9;
            ...
          }//AllSensors if statement
          else  //this is the 0...500
          {
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            cArray_TC[i + 8] = all_sensors[i]->readCelsius();
            ...
            SPI.endTransaction();
          }
        }
      }
    }
    Some simple things here.

    Begin/End Transaction
    : You don't need begin and end transactions... And you remove the issue about return statements... You have one begin statement that is outside of the loop and called every time, but then the endTransaction is called within the loop, so it will only be called when your timing code says it is OK to run again and then every iteration where the temperature is >=0...

    Now for the array values: i, i+4 and I+8

    cArray_TC[i] - will only ever be assigned a value if all_sensors[i]->readCelsius() < 0
    cArray_TC[i + 4] - Will be assigned in both branches of the if to readInternal
    cArray_TC[i + 8] - Will only be assigned if all_sensors[i]->readCelsius() >= 0

    Is this what you really want? Again I don't know the meaning to you of the three indexes here... But for example if the readCelsius > 0 index 0, 4 8 will never be set to anything, so probably 0...
    Hi Kurt E
    First problem:

    So I got the impression that returning a array would be possible.
    i Cant send back Single variables because the return statement doesn’t allow me hence cArray_TC[i]; //cold values, cArray_TC[i+4] and hot cArray[i+8];

    Double *Thermocouples1_4();//pseudo code
    {
    Open SPI
    time interval
    Get info from sensors Individually.
    Send data from four sensors in Array
    Close the SPI
    }

    Second Problem:
    so the idea I have in my mind is not the same as in the Code i am getting confused. I used in another thread a abstract code from tutorial point, rewrote it to send data from an Double *Thermocouples1_4();/ *sendArray();this works and outputs the data
    To a static double *p that I must declare as a global. This pointer requires to be written to: pArray_to_Screen=Thermocouples1_4();

    I probably got of on the wrong foot and ever stuck since then.
    This Array pArray_to_Screen is supposed to be an array but isn’t I discovered. This is one of the problems
    If I use A for loop and let it run through this pArray I get the values I need in void loop.
    But the values are not passed to TFTScreen(pArray_to_Screen);

    My English is not that good

  21. #21
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,681
    Yes you can return arrays or in your case a pointer to a double variable.

    BUT: The first issue is when you return it, now what you return... That is you should probably just have one return statement, something like:
    Code:
    double *Thermocouples1_4() //see instanciation pointer to array class TH1,TH2,TH3,TH4
    {
      
    //  SPI.beginTransaction(SettingsA);
      unsigned long TC_1b1_currentMillis = millis();
      if (TC_1b1_currentMillis - TC_1b1_previousMillis >= TC_1b1_Interval)
      {
        TC_1b1_previousMillis = TC_1b1_currentMillis;
    
        for (int i = 0; i < 4; i++)
        {
          if (all_sensors[i]->readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
          {
            cArray_TC[i] = all_sensors[i]->readCelsius();
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            double SubC1_int = cArray_TC[i] - cArray_TC[i + 4];
            double SubCalc = SubC1_int * 0.041276; /*mV*/
            double E1 = (-0.176 * pow(10, -1));
            double E2 = (0.389 * pow(10, -1) * pow( cArray_TC[i + 4], 1));
            double E3 = (0.186 * pow(10, -4) * pow( cArray_TC[i + 4], 2));
            double E4 = (-0.995 * pow(10, -7) * pow( cArray_TC[i + 4], 3));
            double E5 = (0.318 * pow(10, -9) * pow( cArray_TC[i + 4], 4));
            double E6 = (-0.561 * pow(10, -12) * pow( cArray_TC[i + 4], 5));
            double E7 = (0.561 * pow(10, -15) * pow( cArray_TC[i + 4], 6));
            double E8 = (-0.320 * pow(10, -18) * pow( cArray_TC[i + 4], 7));
            double E9 = (0.972 * pow(10, -22) * pow( cArray_TC[i + 4], 8));
            double E10 = (-0.121 * pow(10, -25) * pow( cArray_TC[i + 4], 9));
            double E11 = (0.119 * pow(exp(-0.118 * pow(10, -3) * ( cArray_TC[i + 4] - 0.127 * pow(10, 3))), 2));
            double E1_11 = E1 + E2 + E3 + E4 + E5 + E6 + E7 + E8 + E9 + E10 + E11;
            double AddCJError = E1_11 + SubCalc;
            double R1 = 0 + (2.51734 * pow(AddCJError, 1));
            double R2 = (-1.17 * pow(AddCJError, 2));
            double R3 = (-1.08 * pow(AddCJError, 3));
            double R4 = (-8.977 * pow(10, -1) * pow(AddCJError, 4));
            double R5 = (-3.73 * 10 * pow(10, -1) * pow(AddCJError, 5));
            double R6 = (-8.6632 * pow(10, -2) * pow(AddCJError, 6));
            double R7 = (-1.045 * pow(10, -2) * pow(AddCJError, 7));
            double R8 = (-5.1920577 * pow(10, -4) * pow(AddCJError, 8));
            double R9 = 0 * pow(AddCJError, 9);
            double R1_9 = R1 + R2 + R3 + R4 + R5 + R6 + R7 + R8 + R9;
            Serial.print("Cold ");
            Serial.print("Corr Value ");
            cArray_TC[i] = (double)R1_9;
    //        return cArray_TC;
            if (isnan( cArray_TC[i]))
            {
              Serial.print("Something wrong with Thermocouple ");
            }
            else {
              Serial.print("C = ");
              Serial.println( cArray_TC[i]);
            }
          }//AllSensors if statement
          else  //this is the 0...500
          {
            cArray_TC[i + 4] = all_sensors[i]->readInternal();
            cArray_TC[i + 8] = all_sensors[i]->readCelsius();
    //        return cArray_TC; if I do this return cArray_TC[i]; i get error double to double not possible.
            Serial.println("inside the TC function");
            Serial.println(cArray_TC[0]);
            Serial.println(cArray_TC[1]);
            Serial.println(cArray_TC[2]);
            Serial.println(cArray_TC[3]);
            Serial.println(cArray_TC[4]);
            Serial.println(cArray_TC[5]);
            Serial.println(cArray_TC[6]);
            Serial.println(cArray_TC[7]);
            Serial.println(cArray_TC[8]);
            Serial.println(cArray_TC[9]);
            Serial.println(cArray_TC[10]);
            Serial.println(cArray_TC[11]); //values are printed.
    
            //SPI.endTransaction();
          }
        }
      }
     return cArray_TC;
    }
    Note, I commented out your begin/end transactations and your current return statements... And simply added one at the end. Again my guess is you had compiler warnings on things like you reached the end of the function and did not return anything... Again that might have return NULL and then you might have passed NULL pointer to other functions which may have crashed!

    Actually your English here is good enough to mostly be able to figure out what you are trying to say... Sorry if I am kidding too much.

    More of the difficulty here has more to do with understanding what your code is doing versus what you think it is doing... Example of this one function. Not all code paths lead having the function return the array... And also you are/were not iterating over all 4 sensors. And even if you are iterating over all 4 sensors, not all 12 values get updated, as I mentioned in previous post.

    My guess is code is currently crashing as it probably returned NULL in the case ( if (TC_1b1_currentMillis - TC_1b1_previousMillis >= TC_1b1_Interval) is not TRUE. This NULL is assigned to your variable which you then pass off to functions like your display code who then try to load a variable out of the NULL pointer and crash.


    If you are curious, you can always do something like change your display code like:
    Code:
    void TFTscreen(double *p) //Menu 1 //Display
    {
      if (p == nullptr) {
        Serial.println("TFTscreen - NULL POINTER PASSED");
        Serial.flush();
        return;
      }
    // get some data
         j += 0.0013;
       currentmicros = micros();
        long value;
      double voltage;
      
     /* all graphics commands have to appear within the loop body. */    
           SPI.beginTransaction(settingsTFT);
    Also back to:
    Code:
    return cArray_TC; if I do this return cArray_TC[i]
    Your function is defined as returning a pointer to a double, and if you do something like: cArray_TC[i]
    You are trying to return a single value not a pointer.

    You can return a pointer to that element like: return &cArray_TC[i]
    But I don't believe that is what you want... Could be wrong, but again this statement would return the pointer to the ith element of your array.

  22. #22
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,757
    Quote Originally Posted by Bastiaan View Post
    So I got the impression that returning a array would be possible.
    Just because you can do a thing does not necessarily mean you should. If you have just 4 sensors, all this array stuff is probably a lot of complexity when just using 4 copies of the code is probably much simpler. Usually simple is best.

    But to address your original question (which Mark T also answered in msg #10), you can't actually pass an array into a function or return an array from a function. That's not how C/C++ works. You're only returning a pointer to the array, not the actual array.

    Pointers are a simple concept which in practice tends to turn into complicated usage. But the idea is simple. Data exists somewhere in memory. That "somewhere" has a numerical address of the exact place it actually exists in memory. A pointer is just a number indicating where that data actually is. The pointer has type info, so the compiler "knows" what type of data to expect at that location. But consider how very simple this actually is. The pointer is just a number of where the data is located. That data might not even be an array. It could be just a single variable at that location. When you use syntax like cArray_TC[3], where cArray_TC is a "double *" pointer, you're telling the compiler that you know the memory actually has a list of double variables, and you want the 4th one (3rd offset) from that numerical location where cArray_TC is pointing.

    So when you return a "double *", you're not actually returning the array. You're just returning a number of where at least 1 double variable is located in memory.


    I probably got of on the wrong foot and ever stuck since then.
    In general, the tenancy to craft overly complicated approaches to relatively simple takes is a huge problem in software development. When you have this sort of situation, it's usually a sign to rethink your program's structure to create a simple approach. Of course it can be very difficult to recognize such signs when you're bogged down in details. This is the sort of wisdom that comes only after a lot of (usually painful) experience.

    For this particular situation, a more common approach would use simpler functions which read just 1 temperature, which display just 1 value on the screen. Usually the top level code would manage the arrays.

    Another approach is more "object oriented", where you have separate code which manages storage of data. You'd still have simple functions which read or consume just 1 thing. But instead of having a top level program manage arrays, you might have those simpler functions call other functions which do simple storage and retrieval tasks, like adding another data point to a queue or removing data (because you're consuming it). While this is more complicated that just using arrays at the top level (and I would always recommend using the simplest way that can work) using a more object oriented approach might make sense if your program responds to events and doesn't have a predictable top-level way to know when each thing will happen. The main idea of object oriented programming is to allow you to design (and test) each part separately. So you would do all the array stuff within that code which is responsible for storing and retrieving data, and keep the interface (function calls) between the parts as simple as possible with well defined uses. This sort of design where each part hides its complexity (managing array) and gives the rest of the program only simple well-defined functions to do only specific things tends to allow software projects to scale up.

    Of course there are times for using pointers and passing pointers to arrays. But this probably isn't one of them. Requiring all parts of your program to be coded with detailed knowledge of how data is stored is the exact opposite way. The tendency is for increasing complexity and adding bugs to your project. You should really reconsider this approach and restructure your design so the data storage aspect is localized to only 1 part of your program.

  23. #23
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,681
    Thanks Paul, I was just beginning to type in something similar... Sometimes I am torn between trying to answer the questions that are asked versus answer in the way that I would probably do it for myself.

    What I was or am suggesting that if it were me, I would probably start off with simple extending the Adafruit
    Code:
    Adafruit_MAX31855 TH1(MAXCLK, 14, MAXDO);
    It could either be a sub-class of their object or it could contain their object:

    Example: sub-class: Note typed in on fly so probably issues:

    Code:
    class cached_max31855 : public Adafruit_MAX31855 {
    Public:
      cached_max31855 (int8_t _sclk, int8_t _cs, int8_t _miso) : cached_max31855(_sclk, _cs, _miso) {};
    
      // We have ability to call directly all of the Adafruit methods.
    
      // cached - computed values
      inline double internal() {return _cached_internal;}
      inline double celsius()  {return _cached_celsius;}
      inline double computed() {return _cached_computed; }  // again not sure what this is.
     
      bool update();  // called to update the cached values; 
      inline bool changed() {return _changed; } // return true if some value changed since last clear.
      inline void clearChanged() {_changed = false; }
    
    private: 
       bool _changed = false; // something changed;
       double _cached_internal = 0.0;
       double _cached_celsius = 0.0;
       double _cached_computed = 0.0;
    }
    Where you would need to implement: bool cached_max31855 ::update() {... stuff to update one sensor and cache the data.


    Again unknown to me is things like I put in function like computed... Which maybe should be named whatever it actually is...

    Hope that makes sense. This obviously could be extended like maybe the class has an elapsedMillis variable that the update checks and clears so that all of this is self contained.
    Maybe you don't care about changed variable... I often try to keep one as why update the screen if nothing on the screen has changed...

    Again it is hard to know when it is best to simply answer questions that are asked or to suggest bigger changes?

    Edit: to use, you would replace your usages of the Adafruit Class to your new class:
    Code:
    cached_max31855 TH1(MAXCLK, 14, MAXDO);
    cached_max31855 TH2(MAXCLK, 15, MAXDO);
    cached_max31855 TH3(MAXCLK, 16, MAXDO);
    cached_max31855 TH4(MAXCLK, 17, MAXDO);
    
    cached_max31855 *all_sensors[] = {&TH1, &TH2, &TH3, &TH4};
    And your display code could simply do something like:
    Code:
    all_sensors[i]->celsius();
    To get the currently cached version...

  24. #24
    Senior Member
    Join Date
    Sep 2016
    Posts
    137


    a big sigh..., but a happy one. and thank you all for taking your limited time in helping me finding answers that are somewhat illogic for me, I think I understand 70% of what is being said, just dont know how to transform it in the right code, I get easily off on the wrong foot, because I am searching in google for c examples, which work but isnt the same as in the Arduino compiler, which I guessed it was.

    ill try to summarize all the # which left me riddled and further asking questions. i also added some comments from the #14,15,16,17,18,19 which will help in readability.

    Ill start with #14

    thanks for the reply defragster: allthough i understand your words, the transformation of these words i dont understand them to relation of the double *Thermocouples1_4(); as this is the only way to write a variable from a function to another, i use globals in the MenuCount to overwrite it by pressing a button and skipping through an If statement. but those are global integers, bytes and the long variables that are used. that I kind of understand ( dont want to be over confident)

    #13 second I thought arrays and pointers were closely bound? meaning that if I return a double function I can define it as return cArray_TC; in the Thermocouples1_4() function and pass it one by one to the cArray_TC[i] in side the For loop for the 270<0,DIE Temperature and 0>500C range. I tested this with a extra for loop in the Void loop millis department and I get for the cArray_TC[i] for each cArray_to_TC[0...12] temperature values; with Serial.println(cArray_to_TC[i])
    which is of course quite bothersome, you see a variable being spit out of the function and your running against a brick wall. and there is only this forum to talk about this in here. I do have friends but they are not in the programming business.

    as I said in #15 I clearly dont understand the last part. and asked what would be the best approach writing an Array from the temperature sensors to this variable cArray_TO_Screen; because i tried 3 days consequtive in a row, to get a hunch what is going on and it is frustrating agitating -> to brick wall (pun).

    #16 requires it to be properly initilized, i tried that and put it in the global department to but gets me to the understanding of a new error:
    double to //*double in return. I thought again if I write a pointer array which sounds like an array but it isnt array and declare it properly that it should work... but it doesnt. again -> to brick wall. i was using a program in #2 that worked and wrote the values perfectly to the screen. I thought I learned something great


    so in one of the threads I'd run the max31855 as fast as possible with help from MarkT short notation in writing 4 classes from the MAX31855 and accessing them over a instance that points to the four classes. I hope I said it right. in this period of.

    Click image for larger version. 

Name:	looks nice CS pin redTFT_green_yellow_Blue_max31855.png 
Views:	3 
Size:	547.0 KB 
ID:	21826Click image for larger version. 

Name:	measuring the CS pins.png 
Views:	3 
Size:	385.1 KB 
ID:	21825

    it worked, really glad and optimistic that four Thermocouples could be read in (i believe less then 3ms), thats blasting over the SPI lines i just like speed i suppose, but speed comes with a price

    #18,#19
    thanks KurtE for the patience, you must be banging your head against something, telling yourself, how stubborn or stupid he can be, he isnt looking properly or isnt finding the right tutorial.

    if have changed the code inside the function *Thermcouples1_4() many times for some experiments to see if can change the sampling speed and lost track on how to properly set back the function to the beginning
    HTML Code:
    https://forum.pjrc.com/threads/63152-More-questions-SPI-Manual-Correctly-Previous-Post-MAX31855-Screen-Freezes-why-CLK
    Begin/End Transaction
    as a matter of fact. I cannot return more then one variable to another function, hence therefore I started using Arrays, well pointers that point to an Array.
    i have 4 sensors that produce temperature readings:
    if a temperature that is measured is below Zero its need to be corrected,
    this is written in the cold part cArray_TC[i] and is corrected;.
    The DIE temperature cArray_TC[i+4] is the cold junction temperature of the DIE;
    cArray_TC[i+8] from the MAX31855 hot temperature is for the 0..500C ( no equation part) and write these values to TFT screen, or maybe put one of the variables into a PID controller which is executed in the TFT_screen(double *pArray_to_Screen) at the end in menuCount ==8.


    I had warnings like that the once, exactly as you described, well warnings are not errors, i assumed to much again about the pointers and variables and neglected them. i need to take a step back: as Paul said in
    tendency is for increasing complexity and adding bugs to your project
    , i think this has to do with confidence.

    anyway no problem with the kidding.

    more of the difficutly here
    yeah I had crashes with the TC_1b1_currentMillis. and totally stopping serial monitor and screen.
    Null Pointer noted!

    this is also noted:
    &cArray_TC[i]
    well i want to access what is written in the cArray_TC[i] and put it on the screen, not the address.
    so I dont know if this is correct. ill try this tomorrow.


    #22 Hi Paul, I am cluttering up the threads of the forum with beginners questions. start easy, divide up your program and place it together in a summary. well I came from there but I am missing steps, putting the pieces together and I dont know how to do it exactly i have an idea but... also pointers and doubles cause of doubles, and wrong foot, which was unintended. i am really greatfull for the help I get and giddy for each reply i get.

    but my biggest concern and is with others on this forum is with the TFT, SPI, I2C and talk over these protocols.
    I started with working in the electronics branch at 2014 and gradually started building from breadboard to two layer, milling with cirqoid, creating my own vias in pcbs with solutions, attended a course over EMC on circuitboards, connections and multilayer boards. and so on. parallel i have been increasing my programming understanding, starting with the Atmega328, nano, due, mega some display projects. and moved on to something new the Teensy3.2 as starters. in these occassions i used only one SPI, but for learning and further development i would like to extend my knowledge with SPI and see what is possible. repeating the previous is not giving me thirst for hunger/more.

    ill move back to the beginning and test a simple TFTScreen function and put a value from the Thermcouple on the screen.


    exactly i have an idea
    i am checking the Encoder and pushbuttons (MenuCheck) on changes with the nowmillis as fast as possible in the void loop.
    easy works. those values are updated and printed on the TFTScreen(); that works.

    i am asking the InternalADC() for Current measurements from my OPA365s to do a 140mV/1A conversion in 3.3V maybe everytime 250ms.

    i am asking the total function Thermocouples1_4() every 20ms. TFTScreen() stops working, Serial monitor is still working for MenuCheck

    just setting them in the void loop with if statements with millis aint gonna do it. I have to set the if statements like in a statemachine, check on CS_PIN and timing conditions I think.... (just tell me if this is the right direction)

    also: I would like to keep things like Adafruit_MAX31855 *all_sensors[] ={&TH1,&TH2,&TH3,&TH4}; as a fundament building block, this works like a charm, however I know now that it wont work that well if I hook up a TFT Screen.

    Big Idea/Questions for later:
    As I think I have problems over the SPI bus and Pointers with arrays, i will solve the pointers thing first in the morning.

    Timing diagram for accessing the SPI.
    Click image for larger version. 

Name:	timing_idea.png 
Views:	4 
Size:	15.1 KB 
ID:	21824

    Will this allow me to check on the CS Pins and also check on the timing how long the SPI is accessed. but doesnt this conflict with the digitalWrite(CS_PINS) because the MAX31855 does allready the accessing its libraries and does this also count for hx8357_t3n 2????????

    Code:
      
        unsigned long ScreennowMillis = millis();
        TH_nowMillis=millis();
        InternalADCnowMillis=millis();
    
    
    if(DigitalRead(CS_Pin,HIGH)&&(ScreennowMillis - ScreenpreviousMillis >= ScreenOnTime))
    {
    digitalWrite(CS_Pin,LOW);
    TFTscreen();
    }
    else if(digitalRead(MAX31855_CS1,HIGH)&&(TH_nowMillis - TH_previousMillis >= TH_OnTime)
    {
    digitalWrite(MAX31855_CS1,LOW);
    
    }
    else if(InternalADCnowMillis-InternalADCpreviousMillis>=InternalADC_OnTime)
    {
    // i dont have a digitalread CS pin for the ADC, still have to do some reseach about this.
    }
    }

    hypotheically speaking: as I am writing a few variables, temperature, current, voltage, or 0..100% to the display, wouldnt it be better to collect them all in a buffer and send them to the display? is this also possible?



    #23

    I have never written something to a library or add a class, I if have seen this can be done, but I dont know about those functions, like void, inline and how they are accessed, or work, it still is magic to me.

    what I understand is: about the max31855 and oscilloscope pictures that written functions inside the class are calls:
    to open the SPI set Chip Select pin low, send a few commands to the max31855 over MOSI, and get some values back over MISO directly during each CLK with some bitorder because devices tend to read in a specific order for the addresses to access.

    as I only had in my life a 4 weeks tutorial on C++ and programming some pushbuttons screenss on a monitor like an up and down counter, i have to think about it.
    I am allready very far in this project, the mechanical parts are almost ready. and I do have to do some testing on the with pwm Water cooling, temperature of the Emagnet etc.

    if I got carried I away Ill resort to a 1 display 1 sensor test code and add it gradually to the screen.

    thanks for reading all of this good night.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	MISOLINE_Blip at 100ms is 3ms long.png 
Views:	3 
Size:	269.2 KB 
ID:	21827   Click image for larger version. 

Name:	MISOLINE_Blip around 100ms..jpg 
Views:	4 
Size:	47.7 KB 
ID:	21828  

    Click image for larger version. 

Name:	IMG_E3087[1].jpg 
Views:	3 
Size:	134.2 KB 
ID:	21829  
    Last edited by Bastiaan; 09-22-2020 at 08:59 PM. Reason: readability and color questions

  25. #25
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,407
    Paul and Kurt put in more effort useful info - though must say I didn't read it all ...

    When you see C or cpp samples - as far as the core language [ control structures, variable allocation and usage for pointers and types ] - it is no different under Arduino. The Teensy on Arduino uses a global standard C/cpp compiler to generate the code specific to the processor in use.

    Starting gradually - or regrouping is a good plan when mysteries appear.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •