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

Thread: rewriting the functions to closed void functions

  1. #26
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    @bastiaan: I hear you, we have all been there. For example just ask me how to do something in JAVA, and I am lost... (Or busy with google)...

    There are many different ways for you structure your code.


    Note: I started off as a C only person (although before that: Fortran2, some assembly, PLI1, COBOL, Datapoint, ...), but later branched into C++. Although I am an old timer who is still not comfortable with some of those new fanged things like templates, Lambda...

    Sorry I should have avoided adding extra complexities like inline... All that means is the code of that method is put in line. So if you call it 20 places there may be 20 copies of that same code... But plus side if it is something as simple as returning an internal variable than it allows you to protect the access to those data members.
    Then there is some other magic glue things like:
    public, versus private, versus protected: public implies code outside of this class can access the members and variables that are in the public area.

    Those areas in private: can only be accessed by the class itself.
    Sort of like private: protected means that in addition to this call can access those data members or methods, classes that are derived from this class can access them as well.


    I also took the slightly more complicated route and made this new class be a sublclass of the Adafruit class?


    I also took some short cuts with how I defined it... So for example the Adafruit class is defined like:
    Code:
    class Adafruit_MAX31855 {
    public:
      Adafruit_MAX31855(int8_t _sclk, int8_t _cs, int8_t _miso);
      Adafruit_MAX31855(int8_t _cs);
    
      bool begin(void);
      double readInternal(void);
      double readCelsius(void);
      double readFahrenheit(void);
      uint8_t readError();
    
      // Define the update timeouts
      static const uint32_t TC_1b1_Interval = 100;  // Choose the time... this is same as #define except it is only in class and has a type
    
    private:
      Adafruit_SPIDevice spi_dev = NULL;
      boolean initialized;
    
      uint32_t spiread32(void);
    };
    Again pretty simple, the class is pretty straight forward.
    So if I rework my simple class above, and unwind it, it might look like:

    First the class definition. Could still be in .ino file or could be split out... For now I would maybe keep in same file...
    Code:
    class cached_max31855 : public Adafruit_MAX31855 {
    Public:
      // constructor
      cached_max31855 (int8_t _sclk, int8_t _cs, int8_t _miso);
    
      // We have ability to call directly all of the Adafruit methods.
    
      // cached - computed values
      double internal() {return _cached_internal;}
      double celsius()  {return _cached_celsius;}
      double computed() {return _cached_computed; }  // again not sure what this is.
     
      bool update();  // called to update the cached values; 
      bool changed() {return _changed; } // return true if some value changed since last clear.
      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;
       elapsedMillis emUpdate = TC_1b1_Interval;  // make sure it fires the first time through
    }
    Now split out the code. Could be in same file or a separate file (.cpp) would need to include appropriate header files.
    Code:
    // Constructor: First call off to parent class
    cached_max31855::cached_max31855 (int8_t _sclk, int8_t _cs, int8_t _miso) : Adafruit_MAX31855 (_sclk, _cs, _miso) {
    };
    
    // We have ability to call directly all of the Adafruit methods.
    
    // cached - computed values
    double cached_max31855::internal() {
      return _cached_internal;
    }
    double cached_max31855::celsius()  {
      return _cached_celsius;
    }
    
    double cached_max31855::computed() {
      return _cached_computed; 
    }  // again not sure what this is.
    
    bool cached_max31855::changed() {
      return _changed; 
    } // return true if some value changed since last clear.
    
    
    // Now the fun part... 
    void cached_max31855::clearChanged() {
      _changed = false; 
    }
     
    bool cached_max31855::update() {
    ;  // called to update the cached values; 
    
       if (emUpdate >= TC_1b1_Interval) {
          emUpdate = 0; // clear timer
    
           // read in your updated stuff maybe update changed. 
           // changed for double is sort of tricky...  for now absolute difference... could be an abs delta > threshold or ??? 
    
          double new_val = readCelsius();  // Uses base classes call
          if (new_val != _cached_celsius ) {
             _changed = true;
             _cached_celsius = new_val;
    
          new_val =  readInternal();
          if (new_val != _cached_internal) {
             _changed = true;
             _cached_internal = new_val;
          }
    
          if (_cached_celsius  < 0) //if temperature is lower than 0   ////Thermocouple 1
          {
             // compute the other value here...
            ...
          }//_cached_celsius  if statement
          else  //this is the 0...500į
          {
             // again compute ...
          }
        return _changed;  // return our cached value of being changed... If useful?  
    }
    Again why I like going this way is everything about your sensor and dealing with it are contained here. Your display code simply has to use the methods to retrieve the values.
    So you should be able to debug the class on its own... Create 4 of them either loop on them or just add 4 lines that do call update()..

    Another completely different way is again to not use an array to return multiple values, is instead create a structure that contains all of the data for the return values:
    Something like: Sorry in advance I will complicate slightly be adding typedef as you will see many examples of this in the base code.

    Something like:
    Code:
    typedef struct {
        double celcius;
        double internal;
        double computed; 
    } MAX31855_t;

    Then your code could return a structure...
    That is you could have it return this structure.

    Code:
    MAX31855_t UpdateFunction() {
       MAX31855_t max; 
    
       max.celcius  = mysensors(i)->readCelsius();
       max.internal = mysensors(i)->readInternal();
    ,,,
       return max;
    }
    ...
    More later.

  2. #27
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    so I wrote 2 simple programs.
    both are accessed with a delay of millis, both Programs are Serial.flush()ed at the end and a Serial.println(); statement is presented on the Serial Monitor, showing the difference in millis of execution time.

    the problem that occurs is that both if statements with (delay without millis) are accessed at the same time, or would like to talk over the same SPI bus. I think.

    I had in some occassions in testing the code and placing the serial.flush() and serial.println("something")
    that there was a count up on the TFT display like 0...65 or 0...70 like in less then a second.

    question is:
    can I just add the Timing of one millis to another if statement to create a timing distance who is talking over the SPI?
    i get two blips on the Thermocouple CS pin and on the TFT_CS pin.

    second idea:if ((ScreencurrentMillis+TC_currentMillis) -ScreenpreviousMillis>=2000)
    {
    do something with the function
    }

    second Test:
    so I tested the max31855 code that works, and copied into two functions into the void loop(). both functions are in an if statement with millis.
    so If i understand it correctly it checks the first if statement on the first condition. and if this is true, do the other condition.

    I also commented the Screen If statement but I still get Zeros on the output of the Thermocouple() function, and on the inside of the Thermocouple Function.

    Code:
    void setup() 
    {
       
      pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
      pinMode(MAX31855_2, OUTPUT); digitalWrite(MAX31855_2, HIGH);
      pinMode(MAX31855_3, OUTPUT); digitalWrite(MAX31855_3, HIGH);
      pinMode(MAX31855_4, OUTPUT); digitalWrite(MAX31855_4, HIGH);
    
      pinMode(TFT_CS, OUTPUT);   digitalWrite(TFT_CS, HIGH);
    
      
       SPI.begin();
      Serial.begin(9600);
      while (!Serial) delay(1); // wait for Serial on Leonardo/Zero, etc
    
    
      SPI.beginTransaction(SettingsA); //5MHz, SPIMODE0, MSBFIRST
      Serial.println("MAX31855 test");
      // wait for MAX chip to stabilize
      delay(500);
      Serial.print("Initializing sensor...");
      if (!thermocouple.begin()) {
        Serial.println("ERROR.");
        while (1) delay(10);
      }
      Serial.println("DONE.");
      SPI.endTransaction();
    
      SPI.beginTransaction(settingsTFT);
    
      tft.begin();
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      SPI.endTransaction();
    
    
    }

    Code:
    void loop()
    {
    
    
      unsigned long ScreencurrentMillis = millis();
      unsigned long TC_currentMillis = millis();
    
      unsigned long startTCmillis = millis();
      unsigned long startTFTmillis = millis();
      if (TC_currentMillis - TC_previousMillis >= 500)
      {
        Serial.print("TC start Millis in ms = ");
        Serial.print(startTCmillis / 1000);
        Temperature = Thermocouple(); //just a variable double.
        Serial.print("Temp value from the max31855 = ");
    
        Serial.print("TC execution time in ms = ");
        Serial.println((startTCmillis - millisTCprevious) / 1);
        Serial.println(" ");
    
        millisTCprevious = startTCmillis; //this is just execution time measurement
        TC_previousMillis = TC_currentMillis;//this value overwrites and resets the previousmillis
    
      } else if (ScreencurrentMillis - ScreenpreviousMillis >= 100)
      {
    
        Serial.print("TFT start Millis in ms = ");
        Serial.print(startTFTmillis / 1000);
      
        TFTscreen(Temperature); //just to see if it refreshes
        Serial.print("TFT Execution Time in ms = ");
        Serial.print((startTFTmillis - millisprevious) / 1);
        Serial.println(" ");
    
        millisprevious = startTFTmillis;//this is just execution time measurement
        ScreenpreviousMillis = ScreencurrentMillis; //this value overwrites and resets the previousmillis
      }
    
    
    }
    }
    
    double Thermocouple()
    {
      SPI.beginTransaction(SettingsA);
      Serial.flush();
      // basic readout test, just print the current temp
      Serial.print("Internal Temp = ");
      Serial.println(thermocouple.readInternal());
    
      c = thermocouple.readCelsius();
      if (isnan(c)) {
        Serial.println("Something wrong with thermocouple!");
      } else
      {
        Serial.print("C = ");
        Serial.println(c);
      }
    
    
    
      //Serial.print("F = ");
      //Serial.println(thermocouple.readFahrenheit());
      SPI.endTransaction();
      return c;
    
    
    }
    
    double TFTscreen(double Temperature_toScreen)
    {
      i++;
      Serial.flush();
      //Serial.println(cTFT);
    
      SPI.beginTransaction(settingsTFT);
      tft.setTextSize(3);
      tft.setCursor(10, 30);
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.print("Crude repaint: ");
      //  tft.print(pArray_Screen[0]);
      tft.fillRect(tft.getCursorX(), 0, 100, 35, C_RED);
      tft.print(i);
      tft.setCursor(10, 60);
      tft.print("temperature");
      tft.print(Temperature_toScreen);
      Serial.println("i am in the function TFT_SPI_noValues to screen only count up");
    
    
      SPI.endTransaction();
    
    
    }
    Serial monitor posts me:
    TFT start Millis in ms = 207Internal Temp = 0.00

    C = 0.00

    i am in the function TFT_SPI_noValues to screen only count up

    TFT Execution Time in ms = 1000

    TC start Millis in ms = 208

    Internal Temp = 0.00
    C = 0.00
    if I run the code in standalone as given in the max38155 example It works.... Serial Monitor spits out
    Code:
    MAX31855 test
    Initializing sensor...DONE.
    Internal Temp = 26.63
    C = 25.75
    Internal Temp = 26.63
    C = 25.75

    what is going on....
    Last edited by KurtE; 09-23-2020 at 12:24 PM.

  3. #28
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Sorry I am not sure we are seeing the whole thing, and hard to know what is going on... Example:

    Code:
    if (TC_currentMillis - TC_previousMillis >= 500)
      {
        Serial.print("TC start Millis in ms = ");
        Serial.print(startTCmillis / 1000);
        Temperature = Thermocouple(); //just a variable double.
        Serial.print("Temp value from the max31855 = ");
    
        Serial.print("TC execution time in ms = ");
        Serial.println((startTCmillis - millisTCprevious) / 1);
        Serial.println(" ");
    
        millisTCprevious = startTCmillis; //this is just execution time measurement
        TC_previousMillis = TC_currentMillis;//this value overwrites and resets the previousmillis
    In here I see you read in Temperature and you look like you are going to print it out, but there is no print statement that does anything with it.

    So again sort of hard to suggest what might be wrong, unless get clearer idea of what you are running.

  4. #29
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    The thing with the nullptr works, and the variables are written as Array to the TFTscreen.
    However the display is not refreshing.

    i googled „like a worldchamp on Statemachines, and if else statements. So I would like to ask how do you time who is talking and who is isn’t? Can’t a check be made on Tft CS pins and max31855 CS pins?

    question is:
    can I just add the Timing of one millis to another if statement to create a timing distance who is talking over the SPI?
    i get two blips on the Thermocouple CS pin and on the TFT_CS pin so that tells me there is talk but not the way I want it.

    Cant I use a train of CS low from all the devices on SPI to the TFT screen?
    I don’t know how far my explanation is getting there.

    Is this a way?
    if ((TFTcurrentmillis+TC_previousMillis) -ScreenpreviousMillis>=2000)
    {
    During this time write to TFTscreen(Temperature);
    }
    Else if( Some timing with millis accessing the thermocouples>=1000)
    {
    Please read thermcouple();
    Temperature=thermcouple();//Return double value is passed to Temperature, it’s a single value so no pointer is required I suppose.

    I’ll start tomorrow from the basic MAX31855 and redo everything. Maybe I’ll spot the error.
    }
    Last edited by Bastiaan; 09-23-2020 at 01:08 PM.

  5. #30
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    I boiled your loop() down to something really basic:
    Code:
    void loop()
    {
      if ((millis() - TC_previousMillis) >= 500)
      {
        Temperature = Thermocouple(); //just a variable double.
    
        TC_previousMillis = millis();//this value overwrites and resets the previousmillis
    
      } else if ((millis() - ScreenpreviousMillis) >= 100)
      {
        TFTscreen(Temperature); //just to see if it refreshes
        ScreenpreviousMillis = millis(); //this value overwrites and resets the previousmillis
      }
    }
    Yeah it won't run each of the thing at exactly 500 and 100, could simply cache millis() with one local at start and use it. But I wanted to show this as simple as possible.

    Now look at the flow here. Suppose, we are at the start time + 100. Your first if will not happen, but the second one will. So therefore during the first about 5 calls off to TFTscreen(temperatore), you will have never called Thermocouple() to actually ever set the Temperature variable.

    Edit: I see we cross posted. Again I personally am not convinced there is an SPI timing issue yet. So far every lock up or strange result has been due to generic things like the sequence in which the code runs and what values that are stored in the global.

    i.e. the Crash, or freeze of the display was not anything with SPI but simple de-referencing a NULL pointer.

  6. #31
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    I’ll wait for further posts, and ask if I can get an extra teensy With display for homework.

  7. #32
    Senior Member
    Join Date
    Sep 2016
    Posts
    137

    spin this idea further

    Quote Originally Posted by Bastiaan View Post
    I’ll wait for further posts, and ask if I can get an extra teensy With display for homework.
    ok, so the idea was in the right direction. thanks Kurt.

    as I have never done something like that before, I see it visually in my head.
    i imagine that the system is not running at full speed but very slowly.
    1 get a temperature value from the Thermocouple MAX31855,
    2 save it into the variable/pointer depending on the many sensors I have.
    3 give it to another function Screen
    4 print the value on screen

    something like this...
    Click image for larger version. 

Name:	thumbnail2.png 
Views:	4 
Size:	397.8 KB 
ID:	21837

    however as I have more then 1 data that is written to the screen. the Function accessing the Screen is gathering all data.
    so be it:
    8x long values=encoder values for the (8 menu points)
    3x floats=Currents measured from the OPA365,
    4x doubles=temperatures
    8 integers=menuCount (menu Position)

    would it be better to write the data in a "" buffer and then write it to the screen?


    because if I have different portions of data be it long, int, float or whatever, will this not clogg up the SPI ? collision errors?

    best regards

    Bastiaan

  8. #33
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Sorry, it is difficult to give you any hard recommendations on what is best or the like, as we don't really know your actual requirements or what all your plans are...
    And many of us try to help, but probably don't have the time or energy to completely understand.... But do try to help as we can.

    Precision:
    There are simple questions, like why read in the Thermocouples so often if you are only going to display the values at much slower cycle. Obviously maybe there is some other processing and alarms and the like that you might be doing with the values. But if it is only to display them, reading them at high rate, I am not sure what that gains you.

    As for holding data to display, again unclear to me, what is a good value for you to hold on to. For example while the Adafruit library returns the temp as a double, what precision do you need in your program. For example are you wanting to display the temp as in degrees, 10ths of a degree, 100ths... Now depending on that answer, does it make sense to keep it as a double? maybe or if only in degrees maybe hold it as a form on integer... sometimes I will convert floating point to integer for example if I am working in 10ths of a degree, I may simply multiply by 10 and then convert to integer and hold on to that... Or maybe not. Why might I do this. Most of the time I might not want to update the display for a field if nothing changed. You can do this with floating points as well, by seeing if the new value would generate the same value displayed...

    Timing:
    Do you really need to break things up? Or can you simply get all of the values and then update display, alternatively if it makes more sense to update each field as you update the value from the sensor... Again sometimes one approach works better and sometime the other. When in your case you may have different logical screens, again it still may depend. For example if you only look at the values of the stuff to maybe change page and one of the sensors on a page, than maybe only read that sensor...

    Do you update the whole screen or only those fields that change? Obviously if each time you update something it changes the layout of everything else on the screen than your update function may need to do stuff to draw whole screen.

    If instead you have a layout where for example you have things like: Title: <Value>
    Then I will often setup a function, or table or class or... that has information about where the value should be displayed. How much information depends on how rich of output you want in these fields, like are you using multiple fonts, or colors or..

    So again the question is, do you need to read each sensor in some specific timing and the display is taking too much time to update to do this? If this is the case then you can also optionally setup to only update some fields on each pass through. That is if you have 8 fields to update. Do you have enough time to update 4 of them, per pass in which case you update the first 4 that have changed on one pass, the next pass you update the next 4, which may wrap around back to the first ones or ...

    Again lots of options.

    But again I would start off simple:
    Loop:
    Read in all sensors
    update the display

    And then add complexity if you need to...

  9. #34
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    really interesting.
    so i started from scratch, tested the MAX31855,

    changed the Timing.
    Tested if the Variable in TFTScreen(double pArray_Screen[9] contains a value.
    value is there.

    I am sampling 1x a 50ms a temperature value that is given to the Screen
    I am updating the Screen in 210ms

    here is the total Code:

    includes:
    Code:
    #include <SPI.h>
    #include "Adafruit_MAX31855.h"
    #include "HX8357_t3n.h"
    #include <FlickerFreePrint.h>// library to draw w/o flicker
    #include <math.h>
    #include <stdio.h>
    
    #include <ButtonEvents.h>
    #include <ADC.h>
    #include <Encoder.h>
    #include <PID_v1.h>
    
    #define MAXCLK 13 //max31855 nothing else.
    #define MAXMISO    12
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    
    #define TFT_DC  10
    #define TFT_CS 8
    #define TFT_RST -1
    
    
    //TFT size
    #define TS_MINX 3800
    #define TS_MAXX 100
    #define TS_MINY 100
    #define TS_MAXY 3750
    
    //TFT defined colors
    #define C_BLACK      0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    FlickerFreePrint<HX8357_t3n> Data1(&tft, C_WHITE, C_BLACK);
    FlickerFreePrint<HX8357_t3n> Data2(&tft, C_WHITE, C_BLACK);
    
    SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
    SPISettings SettingsA(5000000, MSBFIRST, SPI_MODE0);
    
    // Default connection is using software SPI, but comment and uncomment one of
    // the two examples below to switch between software SPI and hardware SPI:
    
    // Example creating a thermocouple instance with software SPI on any three
    // digital IO pins.
    #define MAXCLK 13 //max31855 nothing else.
    #define MAXMISO    12
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    
    // initialize the Thermocouple
    Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
    Adafruit_MAX31855 TH2(MAXCLK, MAX31855_2, MAXMISO);
    Adafruit_MAX31855 TH3(MAXCLK, MAX31855_3, MAXMISO);
    Adafruit_MAX31855 TH4(MAXCLK, MAX31855_4, MAXMISO);
    Adafruit_MAX31855 *all_sensors[] = {&TH1, &TH2, &TH3, &TH4};
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double static cArray_TC[12]; 
    double static *pArray_From_TC;
    double static pArray_Screen;
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    Thermocouple Function:
    Code:
    double *Thermocouples1_4()
    {
      SPI.beginTransaction(SettingsA);
      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;
    
    
          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();
    
          Serial.print("Thermocouples1_4 Print cArray_TC[11] = ");
          Serial.println(cArray_TC[11]);
    
    
        }
      }
    
      SPI.endTransaction();
      return cArray_TC;
    
    }
    TFTscreen
    Code:
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
    
    
      if (pArray_Screen == nullptr)
      {
        Serial.println("TFTSCREEN- NULL pointer Passed!");
        Serial.flush();
    
        return;
      }
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      SPI.beginTransaction(settingsTFT);
    //  Serial.println("inside the TFT function");
      //      Serial.println(pArray_Screen[0]);
      //      Serial.println(pArray_Screen[1]);
      //      Serial.println(pArray_Screen[2]);
      //      Serial.println(pArray_Screen[3]);
      //      Serial.println(pArray_Screen[4]);
      //      Serial.println(pArray_Screen[5]);
      //      Serial.println(pArray_Screen[6]);
      //      Serial.println(pArray_Screen[7]);
      //      Serial.println(pArray_Screen[8]);
      //      Serial.println(pArray_Screen[9]);
      //      Serial.println(pArray_Screen[10]);
      //Serial.print(pArray_Screen[11]);
      /* all graphics commands have to appear within the loop body. */
    
    
      Data1.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(4);
      tft.print(" This is just a test");
      tft.print(" this is just a test ");
      tft.setTextSize(4);
      Serial.print("TFTscreen pArray_Screen[11] = " );
      Serial.println(pArray_Screen[11]);
      Data1.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 45);
      tft.print(pArray_Screen[11]);
      Data1.print(j, 2);
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[11]);
      Data1.print(str);
      
      SPI.endTransaction();
    }
    Void Setup()

    Code:
    void setup()
    {
      //SPI CS pins for the MAX31855 need to be set high!
      pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
      pinMode(MAX31855_2, OUTPUT); digitalWrite(MAX31855_2, HIGH);
      pinMode(MAX31855_3, OUTPUT); digitalWrite(MAX31855_3, HIGH);
      pinMode(MAX31855_4, OUTPUT); digitalWrite(MAX31855_4, HIGH);
    
      pinMode(TFT_CS, OUTPUT);   digitalWrite(TFT_CS, HIGH);
      SPI.begin();
      SPI.beginTransaction(SettingsA);
      Serial.begin(9600);
    
      while (!Serial) delay(1); // wait for Serial on Leonardo/Zero, etc
      for (int i = 0; i > 4; i++)
      {
    
    
        Serial.println("MAX31855 test");
        // wait for MAX chip to stabilize
        delay(500);
        Serial.print("Initializing sensor...");
        if (!all_sensors[i]->begin())
        {
          Serial.println("ERROR.");
          while (1) delay(10);
        }
        Serial.println("DONE.");
    
      }
    
      SPI.endTransaction();
      
    SPI.beginTransaction(settingsTFT); //25000000,MSBFirst,SPI_MODE3
    
      tft.begin();
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      SPI.endTransaction();
    
    
    }
    Void loop
    Code:
    void loop()
    {
      if ((millis() - ValuespreviousMillis) >= 1000)// basic readout test every 1 second repeat, do Temperature measurement 4x, write to screen every 210ms //to get no overlap with temperature values that are written on SPI 
      {
        if ((millis() - TC_previousMillis) >= 50)
        
      {
        Thermocouples1_4();
        TC_previousMillis = millis();
      }
      else if ((millis() - ScreenpreviousMillis) >= 210)
      {
        TFTscreen(Thermocouples1_4());
        ScreenpreviousMillis = millis();
      }
    ValuespreviousMillis=millis();
    }
    
    
    }
    Serial Monitor Posts:

    Code:
    _t3n::begin mosi:11 miso:12 SCLK:13 CS:8 DC:10 SPI clocks: 26000000 2000000
    _t3n::begin - completed
    
    Display Power Mode: 0xFF
    MADCTL Mode: 0xFF
    Pixel Format: 0xFF
    Image Format: 0xFF
    Self Diagnostic: 0xFF
    
    Thermocouples1_4 Print cArray_TC[11] = 0.00
    Thermocouples1_4 Print cArray_TC[11] = 0.00
    Thermocouples1_4 Print cArray_TC[11] = 0.00
    Thermocouples1_4 Print cArray_TC[11] = 16.25
    TFTscreen pArray_Screen[11] = 16.25
    Thermocouples1_4 Print cArray_TC[11] = 16.25
    Thermocouples1_4 Print cArray_TC[11] = 16.25
    Thermocouples1_4 Print cArray_TC[11] = 16.25
    Thermocouples1_4 Print cArray_TC[11] = 24.25
    TFTscreen pArray_Screen[11] = 24.25
    Thermocouples1_4 Print cArray_TC[11] = 24.25
    Thermocouples1_4 Print cArray_TC[11] = 24.25
    Thermocouples1_4 Print cArray_TC[11] = 24.25
    Thermocouples1_4 Print cArray_TC[11] = 24.25
    TFTscreen pArray_Screen[11] = 24.25
    Screen shows: nada.
    Click image for larger version. 

Name:	Display_without Values.png 
Views:	3 
Size:	231.4 KB 
ID:	21840

    I have also run a graphics test. which works.

  10. #35
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,390
    Bummer redoing the code still not giving desired results.

    The loop() code was not Ctrl+T formatted before posting - the closing parens and indentation don't match so scanning by eye takes extra effort to see what looks like an error is just bad formatting.
    Code:
      }
    ValuespreviousMillis=millis();
    }
    
    
    }
    Also noted before one contiguous code posting is easier to view and use. If I were to want to look at the code I'd copy the whole thing out into my editor for proper viewing as it make following the code like when self written.

    ... So I have not looked yet at the code

    There should be noting stopping values printing. If the MAX device is suspected of causing an error - comment all calls to that out and supply known test values and then confirm the code works to put those values on the display. This will confirm the wiring and other elements are correct in software.

    Then maybe with USB connected print the MAX value out to the USB port to confirm they are arriving as expected, and combine to see them on the display.

  11. #36
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    //total Code:
    Code:
    #include <SPI.h>
    #include "Adafruit_MAX31855.h"
    #include "HX8357_t3n.h"
    #include <FlickerFreePrint.h>// library to draw w/o flicker
    #include <math.h>
    #include <stdio.h>
    
    #include <ButtonEvents.h>
    #include <ADC.h>
    #include <Encoder.h>
    #include <PID_v1.h>
    
    #define MAXCLK 13 //max31855 nothing else.
    #define MAXMISO    12
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    
    #define TFT_DC  10
    #define TFT_CS 8
    #define TFT_RST -1
    
    
    //TFT size
    #define TS_MINX 3800
    #define TS_MAXX 100
    #define TS_MINY 100
    #define TS_MAXY 3750
    
    //TFT defined colors
    #define C_BLACK      0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    FlickerFreePrint<HX8357_t3n> Data1(&tft, C_WHITE, C_BLACK);
    FlickerFreePrint<HX8357_t3n> Data2(&tft, C_WHITE, C_BLACK);
    
    SPISettings settingsTFT(25000000, LSBFIRST, SPI_MODE3);
    SPISettings SettingsA(5000000, MSBFIRST, SPI_MODE0);
    
    // Default connection is using software SPI, but comment and uncomment one of
    // the two examples below to switch between software SPI and hardware SPI:
    
    // Example creating a thermocouple instance with software SPI on any three
    // digital IO pins.
    
    
    // initialize the Thermocouple
    Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
    Adafruit_MAX31855 TH2(MAXCLK, MAX31855_2, MAXMISO);
    Adafruit_MAX31855 TH3(MAXCLK, MAX31855_3, MAXMISO);
    Adafruit_MAX31855 TH4(MAXCLK, MAX31855_4, MAXMISO);
    Adafruit_MAX31855 *all_sensors[] = {&TH1, &TH2, &TH3, &TH4};
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double static cArray_TC[12]; 
    double static *pArray_From_TC;
    double static pArray_Screen;
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    
    
    
    // Example creating a thermocouple instance with hardware SPI
    // on a given CS pin.
    //#define MAXCS   10
    //Adafruit_MAX31855 thermocouple(MAXCS);
    double *Thermocouples1_4()
    {
      SPI.beginTransaction(SettingsA);
      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;
    
    
          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();
    
          Serial.print("Thermocouples1_4 Print cArray_TC[11] = ");
          Serial.println(cArray_TC[11]);
    
    
        }
      }
    
      SPI.endTransaction();
      return cArray_TC;
    
    }
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
    
    
      if (pArray_Screen == nullptr)
      {
        Serial.println("TFTSCREEN- NULL pointer Passed!");
        Serial.flush();
    
        return;
      }
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      SPI.beginTransaction(settingsTFT);
    //  Serial.println("inside the TFT function");
      //      Serial.println(pArray_Screen[0]);
      //      Serial.println(pArray_Screen[1]);
      //      Serial.println(pArray_Screen[2]);
      //      Serial.println(pArray_Screen[3]);
      //      Serial.println(pArray_Screen[4]);
      //      Serial.println(pArray_Screen[5]);
      //      Serial.println(pArray_Screen[6]);
      //      Serial.println(pArray_Screen[7]);
      //      Serial.println(pArray_Screen[8]);
      //      Serial.println(pArray_Screen[9]);
      //      Serial.println(pArray_Screen[10]);
      //Serial.print(pArray_Screen[11]);
      /* all graphics commands have to appear within the loop body. */
    
    
      Data1.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(4);
      tft.print(" This is just a test");
      tft.print(" this is just a test ");
      tft.setTextSize(4);
      Serial.print("TFTscreen pArray_Screen[11] = " );
      Serial.println(pArray_Screen[11]);
      Data1.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 45);
      tft.print(pArray_Screen[11]);
      Data1.print(j, 2);
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[11]);
      Data1.print(str);
    
      
      
      
      SPI.endTransaction();
    }
    
    
    
    
    
    void setup()
    {
      Serial.begin(9600);
      //SPI CS pins for the MAX31855 need to be set high!
      pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
      pinMode(MAX31855_2, OUTPUT); digitalWrite(MAX31855_2, HIGH);
      pinMode(MAX31855_3, OUTPUT); digitalWrite(MAX31855_3, HIGH);
      pinMode(MAX31855_4, OUTPUT); digitalWrite(MAX31855_4, HIGH);
    
      pinMode(TFT_CS, OUTPUT);   digitalWrite(TFT_CS, HIGH);
      SPI.begin();
      
      
     SPI.beginTransaction(settingsTFT);
    
      tft.begin();
    
        uint8_t x = tft.readcommand8(HX8357_RDMODE);
        Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
        x = tft.readcommand8(HX8357_RDMADCTL);
        Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
        x = tft.readcommand8(HX8357_RDPIXFMT);
        Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
        x = tft.readcommand8(HX8357_RDIMGFMT);
        Serial.print("Image Format: 0x"); Serial.println(x, HEX);
        x = tft.readcommand8(HX8357_RDSELFDIAG);
        Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      SPI.endTransaction();
    
    SPI.beginTransaction(SettingsA);
    
      while (!Serial) delay(1); // wait for Serial on Leonardo/Zero, etc
      for (int i = 0; i > 4; i++)
      {
    
    
        Serial.println("MAX31855 test");
        // wait for MAX chip to stabilize
        delay(500);
        Serial.print("Initializing sensor...");
        if (!all_sensors[i]->begin())
        {
          Serial.println("ERROR.");
          while (1) delay(10);
        }
        Serial.println("DONE.");
    
      }
    
      SPI.endTransaction();
     
    
    
    }
    
    void loop()
    {
      if ((millis() - ValuespreviousMillis) >= 1000)
      {
        if ((millis() - TC_previousMillis) <= 50)
        
      {
        Thermocouples1_4();
        TC_previousMillis = millis();
      }
      else if ((millis() - ScreenpreviousMillis) >= 75)
      {
        TFTscreen(Thermocouples1_4());
        ScreenpreviousMillis = millis();
      }
    ValuespreviousMillis=millis();  
    // basic readout test every 1 second repeat, do Temperature measurement 4x, write to screen every 210ms //to get no overlap 
    }
    
    
    
    
    }

  12. #37
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    the CS pin is constantly HIGH!, why? i didnt set it to HIGH. only in the Void Setup the lines are drawn on the screen then after that it is constantly HIGH.
    the CS pin is pin 8.

    currently I have only one thermocouple asking for Info.

    Code:
    /***************************************************
      This is an example for the Adafruit Thermocouple Sensor w/MAX31855K
    
      Designed specifically to work with the Adafruit Thermocouple Sensor
      ----> https://www.adafruit.com/products/269
    
      These displays use SPI to communicate, 3 pins are required to
      interface
      Adafruit invests time and resources providing this open source code,
      please support Adafruit and open-source hardware by purchasing
      products from Adafruit!
    
      Written by Limor Fried/Ladyada for Adafruit Industries.
      BSD license, all text above must be included in any redistribution
     ****************************************************/
    
    #include <SPI.h>
    #include "Adafruit_MAX31855.h"
    #include "HX8357_t3n.h"
    #include <FlickerFreePrint.h>// library to draw w/o flicker
    #include <math.h>
    #include <stdio.h>
    
    #include <ButtonEvents.h>
    #include <ADC.h>
    #include <Encoder.h>
    #include <PID_v1.h>
    
    
    // Default connection is using software SPI, but comment and uncomment one of
    // the two examples below to switch between software SPI and hardware SPI:
    
    // Example creating a thermocouple instance with software SPI on any three
    // digital IO pins.
    #define C_BLACK      0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    #define TFT_DC  10
    #define TFT_CS 8
    #define TFT_RST -1
    
    
    #define MAXCLK     13 //max31855 nothing else.
    #define MAXMISO    12
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    SPISettings settingsTFT(25000000, LSBFIRST, SPI_MODE3);
    SPISettings SettingsA(5000000, MSBFIRST, SPI_MODE0);
    SPISettings SettingsB(5000000, MSBFIRST, SPI_MODE0);
    SPISettings SettingsC(5000000, MSBFIRST, SPI_MODE0);
    SPISettings SettingsD(5000000, MSBFIRST, SPI_MODE0);
    
    // initialize the Thermocouple
    Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
    
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    FlickerFreePrint<HX8357_t3n> Data1(&tft, C_WHITE, C_BLACK);
    FlickerFreePrint<HX8357_t3n> Data2(&tft, C_WHITE, C_BLACK);
    
    // Example creating a thermocouple instance with hardware SPI
    // on a given CS pin.
    //#define MAXCS   10
    //Adafruit_MAX31855 thermocouple(MAXCS);
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double static TC1[12];
    double static *pArray_From_TC;
    double static pArray_Screen;
    
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
      SPI.beginTransaction(settingsTFT);
    
        if (pArray_Screen == nullptr)
        {
          Serial.println("TFTSCREEN- NULL pointer Passed!");
          Serial.flush();
      
          return;
        }
    
    
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      
      Data1.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(4);
      tft.print(" This is just a test");
      tft.print(" this is just a test ");
      tft.setTextSize(4);
      Data1.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 45);
    
      //  Serial.print(pArray_Screen[0]); //cold
      //  Serial.print(pArray_Screen[4]); //Internal
      //  Serial.print(pArray_Screen[8]); //Hot
    
      tft.print("test1");
      tft.print("test2");
      tft.print("test3");
      tft.print(pArray_Screen[4]);
      tft.print(pArray_Screen[8]);
    
      Data1.print(j, 2);
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[0]);
      Data1.print(str);
    
     Data1.print(j, 2);
     sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[4]);
      Data1.print(str);
    
      Data1.print(j, 2);
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[8]);
      Data1.print(str);
      SPI.endTransaction();
    }
    
    
    
    
    double *Thermocouples1()
    {
      SPI.beginTransaction(SettingsA);
      if (TH1.readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
      {
        TC1[8] = TH1.readCelsius();
        delay(10);
        TC1[4] = TH1.readInternal();
    
        if (isnan( TC1[8]))
        {
          Serial.print("Something wrong with Thermocouple ");
        }
        else {
          Serial.print("C = ");
          Serial.println(TC1[8]);
        }
      } else if (TH1.readCelsius() > 0) //this is the 0...500į
      {
    
        TC1[4] = TH1.readInternal();
        
        TC1[8] = TH1.readCelsius();
        if (isnan( TC1[8]))
        {
          Serial.print("Something wrong with Thermocouple ");
        }
        else {
          Serial.print("C = ");
          Serial.println(TC1[8]);
        }
        Serial.print("Thermocouples1 Print cArray_TC[8] = ");
        Serial.println(TC1[8]);
    
    
    
      }
      SPI.endTransaction();
      return TC1;
    }
    
    
    
    void setup()
    {
      Serial.begin(115200);
      //SPI CS pins for the MAX31855 need to be set high!
      pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
      pinMode(MAX31855_2, OUTPUT); digitalWrite(MAX31855_2, HIGH);
      pinMode(MAX31855_3, OUTPUT); digitalWrite(MAX31855_3, HIGH);
      pinMode(MAX31855_4, OUTPUT); digitalWrite(MAX31855_4, HIGH);
    
      pinMode(TFT_CS, OUTPUT);   digitalWrite(TFT_CS, HIGH);
      SPI.begin();
    
    
      SPI.beginTransaction(settingsTFT);
    
      tft.begin();
    
      uint8_t x = tft.readcommand8(HX8357_RDMODE);
      Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDMADCTL);
      Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDPIXFMT);
      Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDIMGFMT);
      Serial.print("Image Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDSELFDIAG);
      Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      SPI.endTransaction();
    
      SPI.beginTransaction(SettingsA);
    
    
      /////////////////////////////////// Thermocopule Sensor 1
      Serial.println("MAX31855 test");
      // wait for MAX chip to stabilize
      delay(500);
      Serial.print("Initializing sensor...");
      if (!TH1.begin())
      {
        Serial.println("ERROR.");
        while (1) delay(10);
      }
    
      SPI.endTransaction();
    
    }
    
    void loop()
    {
      if ((millis() - ValuespreviousMillis) >= 1000)
      {
        if ((millis() - TC_previousMillis) >= 50)
    
        {
         Thermocouples1();
          
          TC_previousMillis = millis();
    
        }
        else if ((millis() - ScreenpreviousMillis) >= 210)
        {
     
          TFTscreen(Thermocouples1());
          ScreenpreviousMillis = millis();
          
        }
        ValuespreviousMillis = millis();
      
        // basic readout test every 1 second repeat, do Temperature measurement 4x, write to screen every 210ms //to get no overlap
      }
    }
    I also tested it with the HX8357 from Adafruit. both libraries show the same behavior.
    the Thermocouple is low for a measurement each 1second, then the screen is up to its work. but the CS PIN from the Screen is HIGH.

    only during Setup where data is written to the screen (putting two lines one red and blue on the screen) the CS Pin goes low.
    Last edited by Bastiaan; 09-24-2020 at 12:04 PM.

  13. #38
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Again - always look for the simple stuff first.

    I always reformat the text to make sure things are what you think they are so for example loop:
    Code:
    void loop()
    {
      if ((millis() - ValuespreviousMillis) >= 1000)
      {
        if ((millis() - TC_previousMillis) <= 50)
    
        {
          Thermocouples1_4();
          TC_previousMillis = millis();
        }
        else if ((millis() - ScreenpreviousMillis) >= 75)
        {
          TFTscreen(Thermocouples1_4());
          ScreenpreviousMillis = millis();
        }
        ValuespreviousMillis = millis();
        // basic readout test every 1 second repeat, do Temperature measurement 4x, write to screen every 210ms //to get no overlap
      }
    }
    Will always be controlled by that first if, so you your internal So you only enter the second if >1000 millis have elapsed.
    So when in this case would the second if

    So yo will never enter the else as the first if will always be true!

    Edit: Never mind, looks like you netured the green if which will never be true...

  14. #39
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    yeah I have to play some more with the statemachine

    i had a line on the screen that was gradually getting thicker, and the thermocouple was printing data on the Serial monitor.

    succes! allthough in small steps.

  15. #40
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    But again why do you continue to call SPI.beginTransaction/endTransaction when it will be called internal to the calls to the sensors?
    Why do you also continue to do so in the display area, when the display code takes care of itself?


    Why as a global do you have: double static pArray_Screen;

    When you call the "This is a test text" does it show up?

    You might change these to println as to start on new lines...

    And again I have never used the flicker free stuff, so if only your values are not showing up, maybe issue with that...

    And again you set text color in the Data1.. No idea of that sets the actual text color to print for other text...

    Again personally KISS..

  16. #41
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    Quote Originally Posted by KurtE View Post
    But again why do you continue to call SPI.beginTransaction/endTransaction when it will be called internal to the calls to the sensors?
    Why do you also continue to do so in the display area, when the display code takes care of itself?


    Why as a global do you have: double static pArray_Screen;

    When you call the "This is a test text" does it show up?

    You might change these to println as to start on new lines...

    And again I have never used the flicker free stuff, so if only your values are not showing up, maybe issue with that...

    And again you set text color in the Data1.. No idea of that sets the actual text color to print for other text...

    Again personally KISS..

    Ok KISS== knights in satans service?... itís a horrible work to help youngsters...I know
    I thought that counted only for the digitalWrite(CSpin,high);
    Especially with different SPI settings. I read through the #5 about handling digitalWrite and Adafruit MAX31855 libraries That it is done automatically. I didnít know it did also the SPI configuration if set in the void Setup once. ok thats smart.

    Static double had to do with me getting on the wrong foot in the beginning. I had a variable that was inside a function. Markt or defragster adviced me to put it outside the loop. Clearly I had no idea what it meant/just partially.
    But I googled it.
    ok Iíll make it a global without Static.
    This is a test text
    Does not show up.


    You might change these to println as to start on new lines...
    Iíll set it on a other cursor.

    Flickerfree works I think it still a timing issue. I asked the guy who wrote it and he pointed me to the wall clearly that his code was working with big CAPITALS

    As I tried the previous staemachine code, and moved everthing inside the if statement to an else. It drew a small dot on the screen that gradually became larger.

    I read pauls state machine yesterday Iíll have to look at it again and form it to my liking.
    I thought that the if statement did something and if this conditions true.
    go through the if statement and do the following if else statement. Thatís the idea I had.
    Insearch of an answer I did also look at the CS pins if something was happening. I came to the conclusion it didnít. started asking myself maybe itís the library, run a graphics test , letís change To the Adafruit library and see if I get CS pins triggered.

    Iíll have to change some conditions in the if statement to have it working maybe add another if statement.
    a while back I had similar problems with th LT3760 led driver with timing on and off.

    Best regards

    Bastiaan

  17. #42
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Again whenever possible I try to: Keep it Simple Stupid (KISS)

    So here is a quick hacked up version of your sketch where I removed all of the transactions. Instead of sensors it just increments the value by .25... It also does not use flicker free.

    It does it real simple, You already output using Opaque text so it overwrites the previous text, except if your new string is shorter than previous. So it just outputs a couple blank characters after...

    Note: I changed TFT pins to the pins used by graphic test...
    Code:
    #include <SPI.h>
    #include "HX8357_t3n.h"
    //#include <FlickerFreePrint.h>// library to draw w/o flicker
    #include <math.h>
    #include <stdio.h>
    
    #define TFT_DC  9
    #define TFT_CS 10
    #define TFT_RST 8
    
    
    //TFT size
    #define TS_MINX 3800
    #define TS_MAXX 100
    #define TS_MINY 100
    #define TS_MAXY 3750
    
    //TFT defined colors
    #define C_BLACK      0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    //FlickerFreePrint<HX8357_t3n> Data1(&tft, C_WHITE, C_BLACK);
    //FlickerFreePrint<HX8357_t3n> Data2(&tft, C_WHITE, C_BLACK);
    
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double static cArray_TC[12];
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    
    
    
    // Example creating a thermocouple instance with hardware SPI
    // on a given CS pin.
    //#define MAXCS   10
    //Adafruit_MAX31855 thermocouple(MAXCS);
    double *Thermocouples1_4()
    {
      for (int i = 0; i < 12; i++)  cArray_TC[i] += 0.25;
      return cArray_TC;
    
    }
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
      if (pArray_Screen == nullptr)
      {
        Serial.println("TFTSCREEN- NULL pointer Passed!");
        Serial.flush();
    
        return;
      }
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      Serial.println("inside the TFT function");
    
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(2);
      tft.println(" This is just a test");
      tft.println(" this is just a test ");
      tft.setTextSize(2);
      
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 200);
      tft.print("TempEmag: ");
      tft.print(pArray_Screen[11]);
    //  tft.print(j, 2);
      tft.print("    "); // blank out some if new string not as long as previous..
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[11]);
      tft.print(str);
    }
    
    void setup()
    {
      Serial.begin(9600);
      pinMode(TFT_CS, OUTPUT);   digitalWrite(TFT_CS, HIGH);
      SPI.begin();
    
      tft.begin();
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
    
    
    }
    
    void loop()
    {
      if ((millis() - ValuespreviousMillis) >= 1000)
      {
        if ((millis() - TC_previousMillis) <= 50)
    
        {
          Thermocouples1_4();
          TC_previousMillis = millis();
        }
        else if ((millis() - ScreenpreviousMillis) >= 75)
        {
          TFTscreen(Thermocouples1_4());
          ScreenpreviousMillis = millis();
        }
        ValuespreviousMillis = millis();
        // basic readout test every 1 second repeat, do Temperature measurement 4x, write to screen every 210ms //to get no overlap
      }
    }
    Again nothing special here, but it does show up text...

  18. #43
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    clearly I really dont understand it.

    i have been trying this morning how to figure out the if and else statements
    i moved over to elapsedmillis to see how it its executed using Paull stofregens example.
    so the values are written to the screen exactly as your program which works.
    so I am guessing there is something else going on, or I still dont get it with the State machine.

    with thermocouple:

    YELLOW is the CSTFT 9
    BLUE thermocouple CSPIN 14
    RED MISO from Thermocouple on pin 12
    GREEN MOSI to TFT Screen 11


    With Thermocouple

    Click image for larger version. 

Name:	Tek000.jpg 
Views:	9 
Size:	119.8 KB 
ID:	21849
    any suggestions?

    as if MOSI is blocked

    Serial monitor with Thermocouple
    Code:
    _t3n::begin - completed
    Display Power Mode: 0xFF
    MADCTL Mode: 0xFF
    Pixel Format: 0xFF
    Image Format: 0xFF
    Self Diagnostic: 0xFF
    MAX31855 test
    Initializing sensor...DONE.
    C = 26.75
    Thermocouples1 Print cArray_TC[8] = 26.75
    C = 26.75
    Thermocouples1 Print cArray_TC[8] = 26.75
    C = 26.75
    Thermocouples1 Print cArray_TC[8] = 26.75
    C = 26.75



    Code:
    #include <SPI.h>
    #include "Adafruit_MAX31855.h"
    #include "HX8357_t3n.h"
    //#include <FlickerFreePrint.h>// library to draw w/o flicker
    #include <math.h>
    #include <stdio.h>
    #include <elapsedMillis.h>
    #include <ButtonEvents.h>
    #include <ADC.h>
    #include <Encoder.h>
    #include <PID_v1.h>
    
    
    // Default connection is using software SPI, but comment and uncomment one of
    // the two examples below to switch between software SPI and hardware SPI:
    
    // Example creating a thermocouple instance with software SPI on any three
    // digital IO pins.
    #define C_BLACK      0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    
    #define TFT_CS     10
    #define TFT_RST    -1
    #define TFT_DC      9
    #define MAXMISO    12
    #define MAXCLK     13 //max31855 nothing else.
    
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    
    SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
    SPISettings SettingsA(5000000, MSBFIRST, SPI_MODE0);
    
    // initialize the Thermocouple
    Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double TC1[12];
    double *pArray_From_TC;
    double  pArray_Screen;
    
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    unsigned long DelaypreviousMillis = 0;
    
    elapsedMillis firstsecond;
    elapsedMillis TCmillis;
    elapsedMillis TFTmillis;
    elapsedMillis Delaymillis;
    
    
    
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
    
      if (pArray_Screen == nullptr)
      {
        Serial.println("TFTSCREEN- NULL pointer Passed!");
        Serial.flush();
    
        return;
      }
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      Serial.println("inside the TFT function");
    
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(2);
      tft.println(" This is just a test");
      tft.println(" this is just a test ");
      tft.setTextSize(2);
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 200);
      tft.print("TempEmag: ");
      Serial.println("pArray_Screen[8]");
      Serial.println(pArray_Screen[8]);
      tft.print(pArray_Screen[8]);
    
      //  tft.print(j, 2);
      tft.print("    "); // blank out some if new string not as long as previous..
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[8]);
      tft.print(str);
    }
    
    
    
    
    double *Thermocouples1()
    {
      //use this to test the code and the pointers, you dont need to have MAX31855. good idea for debugging
      ////    for (int i = 0; i < 12; i++) TC1[i] += 0.25;
      ////    return TC1;
      if (TH1.readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
      {
    
    
    
        TC1[8] = TH1.readCelsius();
        delay(10);
        TC1[4] = TH1.readInternal();
    
        if (isnan( TC1[8]))
        {
          Serial.print("Something wrong with Thermocouple ");
        }
        else {
          Serial.print("C = ");
          Serial.println(TC1[8]);
        }
      } else  //this is the 0...500į
      {
    
        TC1[4] = TH1.readInternal();
    
        TC1[8] = TH1.readCelsius();
        if (isnan( TC1[8]))
        {
          Serial.print("Something wrong with Thermocouple ");
        }
        else {
          Serial.print("C = ");
          Serial.println(TC1[8]);
        }
        Serial.print("Thermocouples1 Print cArray_TC[8] = ");
        Serial.println(TC1[8]);
    
    
    
      }
    
      return TC1;
    }
    
    
    
    void setup()
    {
      Serial.begin(9600);
      //SPI CS pins for the MAX31855 need to be set high!
      pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
    
      pinMode(TFT_CS,     OUTPUT); digitalWrite(TFT_CS, HIGH);
    
      SPI.begin();
      tft.begin();
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      uint8_t x = tft.readcommand8(HX8357_RDMODE);
      Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDMADCTL);
      Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDPIXFMT);
      Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDIMGFMT);
      Serial.print("Image Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDSELFDIAG);
      Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
    
      Serial.println("MAX31855 test");
      // wait for MAX chip to stabilize
      delay(500);
      Serial.print("Initializing sensor...");
      if (!TH1.begin()) {
        Serial.println("ERROR.");
        while (1) delay(10);
      }
      Serial.println("DONE.");
    }
    
    void loop()
    {
    
      if (firstsecond >= 1000)
      {
    
        if (TCmillis >= 50)
        {
          Thermocouples1();
          TCmillis = TCmillis - 50;
        }
        else if (TFTmillis >= 255)
        {
          TFTscreen(Thermocouples1());
          TFTmillis = TFTmillis - 255;
        }
        firstsecond = firstsecond - 1000;
    
      }
    }

    without Thermocouple
    I get this on my screen
    Click image for larger version. 

Name:	withoutmax31855.jpg 
Views:	5 
Size:	130.1 KB 
ID:	21850
    Code:
    #include <SPI.h>
    //#include "Adafruit_MAX31855.h"
    #include "HX8357_t3n.h"
    #include <math.h>
    #include <stdio.h>
    #include <elapsedMillis.h>
    #include <ButtonEvents.h>
    #include <ADC.h>
    #include <Encoder.h>
    #include <PID_v1.h>
    
    
    // Default connection is using software SPI, but comment and uncomment one of
    // the two examples below to switch between software SPI and hardware SPI:
    
    // Example creating a thermocouple instance with software SPI on any three
    // digital IO pins.
    #define C_BLACK       0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    
    #define TFT_CS     10
    #define TFT_RST    -1
    #define TFT_DC      9
    #define MAXMISO    12
    #define MAXCLK     13 //max31855 nothing else.
    
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    
    SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
    SPISettings SettingsA(5000000, MSBFIRST, SPI_MODE0);
    
    // initialize the Thermocouple
    //Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double  TC1[12];
    double *pArray_From_TC;
    double  pArray_Screen;
    
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    
    elapsedMillis firstsecond;
    elapsedMillis TCmillis;
    elapsedMillis TFTmillis;
    elapsedMillis Delaymillis;
    
    
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
    
      if (pArray_Screen == nullptr)
      {
        Serial.println("TFTSCREEN- NULL pointer Passed!");
        Serial.flush();
    
        return 0;
      }
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      Serial.println("inside the TFT function");
    
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(2);
      tft.println(" This is just a test");
      tft.println(" this is just a test ");
      tft.setTextSize(2);
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 200);
      tft.print("TempEmag: ");
      Serial.println("pArray_Screen[8]");
      Serial.println(pArray_Screen[8]);
      tft.print(pArray_Screen[8]);
    
      //  tft.print(j, 2);
      tft.print("    "); // blank out some if new string not as long as previous..
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[8]);
      tft.print(str);
    }
    double *Thermocouples1()
    {
      //use this to test the code and the pointers, you dont need to have MAX31855. good idea for debugging
      for (int i = 0; i < 12; i++) TC1[i] += 0.25;
      return TC1;
    }
    
    void setup()
    {
      Serial.begin(9600);
      //SPI CS pins for the MAX31855 need to be set high!
    //  pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
    
      pinMode(TFT_CS,     OUTPUT); digitalWrite(TFT_CS, HIGH);
    
      SPI.begin();
      tft.begin();
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      uint8_t x = tft.readcommand8(HX8357_RDMODE);
      Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDMADCTL);
      Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDPIXFMT);
      Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDIMGFMT);
      Serial.print("Image Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDSELFDIAG);
      Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
    
    //  Serial.println("MAX31855 test");
    //  // wait for MAX chip to stabilize
    //  delay(500);
    //  Serial.print("Initializing sensor...");
    //  if (!TH1.begin()) {
    //    Serial.println("ERROR.");
    //    while (1) delay(10);
    //  }
    //  Serial.println("DONE.");
    }
    void loop()
    {
      if (firstsecond >= 1000)
      {firstsecond=firstsecond-1000;
      }
        if (TCmillis>=50)
    
        {
          Thermocouples1();
    
          TCmillis=TCmillis-50;
    
        }
        else if (TFTmillis>=255)
        {
    
          TFTscreen(Thermocouples1());
          TFTmillis=TFTmillis-255;
    
        }
        firstsecond=firstsecond-1000;
      }
    Display WORKS!
    Serial monitor without Thermocouple
    Code:
    _t3n::begin - completed
    
    Display Power Mode: 0xFF
    MADCTL Mode: 0xFF
    Pixel Format: 0xFF
    Image Format: 0xFF
    Self Diagnostic: 0xFF
    inside the TFT function
    pArray_Screen[8]
    5.00
    inside the TFT function
    pArray_Screen[8]
    5.25
    inside the TFT function
    pArray_Screen[8]
    5.75
    inside the TFT function
    pArray_Screen[8]
    6.00
    inside the TFT function
    pArray_Screen[8]
    9.00
    inside the TFT function
    pArray_Screen[8]
    10.50
    also sometimes I had a warning end of function return or something, cant reproduce it.
    Last edited by KurtE; 09-25-2020 at 12:40 PM. Reason: MOSI being blocked?

  19. #44
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    all the Warnings with the MAX31855

    Code:
    C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library/Adafruit_MAX31855.h:49:32: warning: passing NULL to non-pointer argument 1 of 'Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t, uint32_t, BitOrder, uint8_t, SPIClass*)' [-Wconversion-null]
       Adafruit_SPIDevice spi_dev = NULL;
                                    ^
    Single_Max31855_to_screen_withMAX31855_withdelay: In function 'double TFTscreen(double*)':
    K:\algemein\Arduino Code\Single_Max31855_to_screen_withMAX31855_withdelay\Single_Max31855_to_screen_withMAX31855_withdelay.ino:105:5: warning: return-statement with no value, in function returning 'double' [-fpermissive]
         return;
         ^
    Single_Max31855_to_screen_withMAX31855_withdelay:110: warning: unused variable 'value' 
       long value;
            ^
    Single_Max31855_to_screen_withMAX31855_withdelay:111: warning: unused variable 'voltage' 
       double voltage;
              ^
    In file included from C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library\Adafruit_MAX31855.cpp:36:0:
    C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library\Adafruit_MAX31855.h:49:32: warning: passing NULL to non-pointer argument 1 of 'Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t, uint32_t, BitOrder, uint8_t, SPIClass*)' [-Wconversion-null]
       Adafruit_SPIDevice spi_dev = NULL;
                                    ^
    C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library\Adafruit_MAX31855.cpp: In member function 'uint32_t Adafruit_MAX31855::spiread32()':
    C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library\Adafruit_MAX31855.cpp:190:7: warning: unused variable 'i' [-Wunused-variable]
       int i;
           ^
    Der Sketch verwendet 59908 Bytes (5%) des Programmspeicherplatzes. Das Maximum sind 1048576 Byte

  20. #45
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Quick note on warnings: The lines:
    Code:
     C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library/Adafruit_MAX31855.h:49:32: warning: passing NULL to non-pointer argument 1 of 'Adafruit_SPIDevice::Adafruit_SPIDevice(int8_t, uint32_t, BitOrder, uint8_t, SPIClass*)' [-Wconversion-null]
       Adafruit_SPIDevice spi_dev = NULL;
     and
    C:\Users\Bpierik\Documents\Arduino\libraries\Adafruit_MAX31855_library\Adafruit_MAX31855.cpp:190:7: warning: unused variable 'i' [-Wunused-variable]
       int i;
    Were warnings from the library. Someone about 20+ hours ago fixed these, so you may want to update this library to newest stuff (it is already in library manager updates...)

    Some of the other warnings are because you declared your TFTScreen function to return a double, but you are not returning anything... Probably not the issue...

  21. #46
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Again look at your loop code:
    Code:
    void loop()
    {
    
      if (firstsecond >= 1000)
      {
    
        if (TCmillis >= 50)
        {
          Thermocouples1();
          TCmillis = TCmillis - 50;
        }
        else if (TFTmillis >= 255)
        {
          TFTscreen(Thermocouples1());
          TFTmillis = TFTmillis - 255;
        }
        firstsecond = firstsecond - 1000;
    
      }
    }
    When would the If in RED ever be false? Your first top level second wait will always imply that
    tcmillis will always be incremented by at least 1000 each time you get to this line... Maybe small chance when you overflow a 32 bit number as you only decrement it by 50...

  22. #47
    Senior Member
    Join Date
    Sep 2016
    Posts
    137
    Ok I’ll update the library. Just came across Adafruit tech forum.

    i clearly don’t understand the State machine.
    it has to be smaller then <=50;

  23. #48
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    There is no real state machine stuff here. Typically state machine is usually something like, I am in state X, if condition Y happens I go to state Z...

    This is just a simple set of nested if with an else clause...

    Again remember that elapsedMillis is real simple stuff... It simply remembers a millis value and when ask for it's value it is simply the current millis minus the saved value...
    So just mentally walk through the code, let me label and remove the stuff they actually do...

    Code:
    void loop()
    {
    
    1)  if (firstsecond >= 1000)  /
      {
     2)   if (TCmillis >= 50)
        {
          TCmillis = TCmillis - 50;
        }
     3)   else if (TFTmillis >= 255)
        {
          TFTmillis = TFTmillis - 255;
        }
        firstsecond = firstsecond - 1000;
    
      }
    }
    This is obviously not valid code now>
    But suppose we start off with all three of elapsedMillis objects set to 0.

    When is the first time that the 1) will be true. For now lets assume everything runs with no additional millis() being used up.
    So when we first make it through the 1)

    All three of the elapsedMillis objects will now have a value 1000
    So your first pass through:
    TCmillis is 1000 so 2) is true... 3) does not get called as it is an else to 2)...
    So when we complete this pass the values of the three elapsed millis is
    firstsecond(0), TCmillis(950), TFTmillis(1000);

    So we continue to loop until firstSecond >= 1000, which now will imply that TCmillis is about 1950 and TFTmillis 2000 so again 2) is true and runs 3) won't because of 2)
    So after this pass: firstsecond(0), TCmillis(1950), TFTmillis(2000);

    ...

    Now if you did not have the outer if the inner if/else would probably work... Assuming that your reading of the sensor takes < 50ms...

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

    commented out the initialiser for the max31855, your program works, display shows val

    Hi Kurt,

    first the things I tried:

    I updated the library for the MAX31855 the error is gone, still no data on the screen.
    I tried also using a other MAX31855 from Rob Tillaart the MAX31855_RT.
    I used the hx8357 from Adafruit, did a graphicstest. pins are in the same configuration as in the graphicstest.
    I did a check on the Serial.println(tft.println("test"));== results in 6 on the serial monitor.

    currently I have this code, i tried playing with the >,< <= =>,
    i used the tft.fillScreen(C_RED); to paint the screen red.
    I used the Serial.println(tft.println("TEST"); which results in 6 on the Serial monitor.
    all show no promising results.

    Code:
     
     if (TCmillis >= 50) //during the first 50ms read the sensor.
      {
        Thermocouples1();
        TCmillis = TCmillis - 50;
      }
      else if (TFTmillis >= 250)//after the first 50ms run the TFTscreen function for 250ms
      {
        TFTscreen(Thermocouples1());
        tft.fillScreen(C_RED);
        TFTmillis = TFTmillis - 250;
    
      }



    I tried your code without the max31855 and added the Initialize for the MAX31855 in the void Setup.
    Screen freezes.
    Click image for larger version. 

Name:	Display_without Values.png 
Views:	2 
Size:	231.4 KB 
ID:	21937


    the code in RED if commented in will result the screen to freeze, no values on the screen. This gives me the validation that the cause of the if else statement in the void loop isnt the problem.

    so ill be waiting for advice.

    Code:
    #include <SPI.h>
    #include "Adafruit_MAX31855.h"
    #include "HX8357_t3n.h"
    #include <math.h>
    #include <stdio.h>
    #include <elapsedMillis.h>
    #include <ButtonEvents.h>
    #include <ADC.h>
    #include <Encoder.h>
    #include <PID_v1.h>
    
    
    // Default connection is using software SPI, but comment and uncomment one of
    // the two examples below to switch between software SPI and hardware SPI:
    
    // Example creating a thermocouple instance with software SPI on any three
    // digital IO pins.
    #define C_BLACK       0x0000
    #define C_BLUE        0x001F
    #define C_RED         0xF800
    #define C_GREEN       0x07E0
    #define C_CYAN        0x07FF
    #define C_MAGENTA     0xF81F
    #define C_YELLOW      0xFFE0
    #define C_WHITE       0xFFFF
    
    
    #define TFT_CS     10
    #define TFT_RST    -1
    #define TFT_DC      9
    #define MAXMISO    12
    #define MAXCLK     13 //max31855 nothing else.
    
    #define MAX31855_1 14
    #define MAX31855_2 15
    #define MAX31855_3 16
    #define MAX31855_4 17
    
    SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
    SPISettings SettingsA(5000000, MSBFIRST, SPI_MODE0);
    
    // initialize the Thermocouple
    Adafruit_MAX31855 TH1(MAXCLK, MAX31855_1, MAXMISO);
    
    //constructors for TFT without flickering
    HX8357_t3n tft = HX8357_t3n(TFT_CS, TFT_DC, TFT_RST);
    
    //flickerfree variables:
    float j;
    unsigned long StartTime, EndTime;
    char str[30];
    unsigned long endmicros;  //speed check variables
    unsigned long currentmicros;
    
    //arrays for the temperature
    double  TC1[12];
    double *pArray_From_TC;
    double  pArray_Screen;
    
    
    //Intervals execution of the various functions
    //TFT update:
    unsigned long ScreenpreviousMillis = 0;
    unsigned long ScreenInterval = 100;//used
    //Thermocouple Function update
    unsigned long TC_previousMillis = 0;
    unsigned long TC_Interval = 1000;//not used
    //total update repeat Thermocouple + TFT every Second
    unsigned long ValuespreviousMillis = 0;
    unsigned long ValuesInterval = 1000;//used
    
    elapsedMillis firstsecond;
    elapsedMillis TCmillis;
    elapsedMillis TFTmillis;
    elapsedMillis Delaymillis;
    
    
    double TFTscreen(double *pArray_Screen) //Menu 1 //Display
    {
    
      if (pArray_Screen == nullptr)
      {
        Serial.println("TFTSCREEN- NULL pointer Passed!");
        Serial.flush();
    
        return 0;
      }
      // get some data
      j += 0.0013;
      currentmicros = micros();
      long value;
      double voltage;
      Serial.println("inside the TFT function");
    
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(0, 20);
      tft.setTextSize(2);
      tft.println(" This is just a test");
      tft.println(" this is just a test ");
      tft.setTextSize(2);
    
      tft.setTextColor(C_WHITE, C_BLACK);
      tft.setCursor(20, 200);
      tft.print("TempEmag: ");
      Serial.println("pArray_Screen[8]");
      Serial.println(pArray_Screen[8]);
      tft.print(pArray_Screen[8]);
    
      //  tft.print(j, 2);
      tft.print("    "); // blank out some if new string not as long as previous..
      sprintf(str, "TempEmag: %.2lf\n", pArray_Screen[8]);
      tft.print(str);
    }
    double *Thermocouples1()
    {
      //use this to test the code and the pointers, you dont need to have MAX31855. good idea for debugging
      for (int i = 0; i < 12; i++) TC1[i] += 0.25;
      return TC1;
    
    //if I use the code for the MAX31855 the screen doesnt show values on the screen
    //  if (TH1.readCelsius() < 0) //if temperature is lower than 0   ////Thermocouple 1
    //  {
    //
    //
    //
    //    TC1[8] = TH1.readCelsius();
    //    delay(10);
    //    TC1[4] = TH1.readInternal();
    //
    //    if (isnan( TC1[8]))
    //    {
    //      Serial.print("Something wrong with Thermocouple ");
    //    }
    //    else {
    //      Serial.print("C = ");
    //      Serial.println(TC1[8]);
    //    }
    //  } else  //this is the 0...500į
    //  {
    //
    //    TC1[4] = TH1.readInternal();
    //
    //    TC1[8] = TH1.readCelsius();
    //    if (isnan( TC1[8]))
    //    {
    //      Serial.print("Something wrong with Thermocouple ");
    //    }
    //    else {
    //      Serial.print("C = ");
    //      Serial.println(TC1[8]);
    //    }
    //    Serial.print("Thermocouples1 Print cArray_TC[8] = ");
    //    Serial.println(TC1[8]);
    //
    //
    //
    //  }
    
    
    
    
    
    }
    
    void setup()
    {
      Serial.begin(9600);
      //SPI CS pins for the MAX31855 need to be set high!
       pinMode(MAX31855_1, OUTPUT); digitalWrite(MAX31855_1, HIGH);
    
      pinMode(TFT_CS,     OUTPUT); digitalWrite(TFT_CS, HIGH);
    
      SPI.begin();
    //if the commented part MAX31855 in red is shifted to here, the MAX31855 doesnt show values, but the screen does.
    
       // Serial.println("MAX31855 test");
        // wait for MAX chip to stabilize
       // delay(500);
       // Serial.print("Initializing sensor...");
       // if (!TH1.begin()) {
        //  Serial.println("ERROR.");
        //  while (1) delay(10);
      //  }
       // Serial.println("DONE.");
    
    
    
      tft.begin();
    
      tft.setRotation(1);
      tft.fillScreen(C_BLACK);
    
      tft.fillRect(0, 30, 480, 10, C_RED);
      tft.fillRect(0, 140, 480, 10, C_BLUE);
    
      uint8_t x = tft.readcommand8(HX8357_RDMODE);
      Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDMADCTL);
      Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDPIXFMT);
      Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDIMGFMT);
      Serial.print("Image Format: 0x"); Serial.println(x, HEX);
      x = tft.readcommand8(HX8357_RDSELFDIAG);
      Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
    
       // Serial.println("MAX31855 test");
        // wait for MAX chip to stabilize
       // delay(500);
       // Serial.print("Initializing sensor...");
       // if (!TH1.begin()) {
        //  Serial.println("ERROR.");
        //  while (1) delay(10);
      //  }
       // Serial.println("DONE.");
    }
    void loop()
    {
    
      
      if (TCmillis >= 50)
    
      {
        Thermocouples1();
    
        TCmillis = TCmillis - 50;
    
      }
      else if (TFTmillis >= 255)
      {
    
        TFTscreen(Thermocouples1());
        TFTmillis = TFTmillis - 255;
    
      }
      firstsecond = firstsecond - 1000;
    }
    Last edited by Bastiaan; 10-01-2020 at 09:49 AM.

  25. #50
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,658
    Sorry, again I don't have your setup and I have disconnected what I had setup...

    But code in RED. Sounds like TH1 fails the begin... And then you tell it to hang there forever, with the line: while (1) delay(10);

    And if your statement is correct you tried it without the sensor and then added this function. The begin will for sure fail...

Posting Permissions

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