rewriting the functions to closed void functions

Status
Not open for further replies.
Same as void loop principle with timing

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...

This means I have to repeat what I did at the void loop kind of the same....?
 
Probably not. But just simply walk yourself thought he code to see what it does..

That is, simply walk yourself though the code.
Code:
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.");
}
So again in your code it says: if (!TH1.begin())
Which if you don't have the sensor or something is configured wrong or??? than the begin will probably fail...
Then in your code you tell it to print an error message: Serial.println("ERROR.");
And loop here forever: while (1) delay(10);

If you don't want your code to hang here, than don't hang here... i.e. simply remove that while loop...
Or course any calls off to other TH1 methods will likely fail and/or maybe hang depending on how robust that class is, but that is another story.
 
Probably not. But just simply walk yourself thought he code to see what it does..

That is, simply walk yourself though the code.
Code:
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.");
}
So again in your code it says: if (!TH1.begin())
Which if you don't have the sensor or something is configured wrong or??? than the begin will probably fail...
Then in your code you tell it to print an error message: Serial.println("ERROR.");
And loop here forever: while (1) delay(10);

If you don't want your code to hang here, than don't hang here... i.e. simply remove that while loop...
Or course any calls off to other TH1 methods will likely fail and/or maybe hang depending on how robust that class is, but that is another story.


Thanks Kurt,

I feel like a toddler.

Suppose that is true, how would you explain if the part from the max31855 in setup is written above tft.begin(); because then the values Zero show up, and the max isn’t working but the tft does
I clearly get zeros on the display but no temperatures.

I’ll try tomorrow and also have a look at the Begin function and the tft.begin() function.
Maybe there is a clash between pins.
 
hi Kurt,

i removed for the fun all of the code referring to the TH1.begin();
and I am not initializing anything.

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>

#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 

#define MAX31855_1 14
#define MAX31855_2 15
#define MAX31855_3 16
#define MAX31855_4 17

SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
SPISettings SettingsA(1000000, MSBFIRST, SPI_MODE0); //just a check, max speed is 5MHz

// 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;



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);

  Serial.println(tft.println("test"));
  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);

  return 0;
}





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(115200);
  //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();

 [COLOR="#FF0000"]  //if this is set here no values are shown in the Serial Monitor. why? I thought that the SPIsettings are intelligent as you described in the previous post?[/COLOR]
  if (!TH1.begin()) //this calls the begin function and is part of the initialized = spi_dev.begin();
  {
    //do something if it is untrue.
    Serial.println("Error");
  }

 [COLOR="#FF0000"]  //this counts also for the tft SPIsettings as in the previous post? [/COLOR]
  tft.begin(); 
[COLOR="#FF0000"]
as a proof I attached a recording with the Scope from all the CS Pins from the four sensors and the big TFTCS pin being HIGH.
[/COLOR][ATTACH=CONFIG]21947._xfImport[/ATTACH]


  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);

 

}

void loop()
{
elapsedMillis firstsecond;
elapsedMillis TCmillis;
elapsedMillis TFTmillis;
elapsedMillis Delaymillis;
//
  if (TCmillis <= 25) //during the first 25ms read the sensor.
  {
    Thermocouples1();
    TCmillis = TCmillis - 25;
  }
  else if (TFTmillis >= 75) 
  {
    TFTscreen(Thermocouples1());
    tft.fillScreen(C_RED);
    TFTmillis = TFTmillis - 75; 

  }
}
I get Zeros on the screen. So I had a look at the max31855.cpp. both devices need to be initialised.
no way around it. that with the while loop didnt matter at all. I dont understand.
-----------------------------------------

Code:
bool Adafruit_MAX31855::begin(void) //this looks fine and returns me "initialized as a Bool" that its 1==true.
{
  initialized = spi_dev.begin();

  return initialized;
}

SPI_dev.begin()
--------------------
[COLOR="#FF0000"]uint32_t Adafruit_MAX31855::spiread32(void) //interesting Function what does it do?[/COLOR]
{
  uint32_t d = 0;//set registervalue to 0.
  uint8_t buf[4]; //create a buffer array

[COLOR="#FF0000"]  // backcompatibility! //doesnt do anything if commented out makes sense[/COLOR]
  if (!initialized) 
 {
    begin();
  }
[COLOR="#FF0000"]  spi_dev.read(buf, 4); // it probably reads here continuously or checks the device is there.[/COLOR]

  d = buf[0];
  d <<= 8;
  d |= buf[1];
  d <<= 8;
  d |= buf[2];
  d <<= 8;
  d |= buf[3];

  // Serial.println(d, HEX); // gives me 65 on the Serial monitor.

  return d;

}

furthermore there are three more SPIread32s which are called in the Void Loop function by double *Thermocouples();

Code:
uint8_t Adafruit_MAX31855::readError() { return spiread32() & 0x7; } // if there is an error.

Code:
double Adafruit_MAX31855::readCelsius(void) //hence TH1.readCelsius();
{

  int32_t v;

[COLOR="#FF0000"]  v = spiread32(); //checks how many times the loop?[/COLOR]

  // Serial.print("0x"); Serial.println(v, HEX);

  /*
  float internal = (v >> 4) & 0x7FF;
  internal *= 0.0625;
  if ((v >> 4) & 0x800)
    internal *= -1;
  Serial.print("\tInternal Temp: "); Serial.println(internal);
  */

  if (v & 0x7) 
 {
    // uh oh, a serious problem!
    return NAN;
  }

  if (v & 0x80000000) {
    // Negative value, drop the lower 18 bits and explicitly extend sign bits.
    v = 0xFFFFC000 | ((v >> 18) & 0x00003FFF);
  } else {
    // Positive value, just drop the lower 18 bits.
    v >>= 18;
  }
   //Serial.println(v, HEX); [COLOR="#FF0000"]gives me 65 on the Serialmonitor[/COLOR]

  double centigrade = v;

  // LSB = 0.25 degrees C
  centigrade *= 0.25;
  return centigrade;
}

Code:
double Adafruit_MAX31855::readInternal(void) 
{
  uint32_t v;

  v = spiread32();[COLOR="#FF0000"]//again continious Read[/COLOR]

  // ignore bottom 4 bits - they're just thermocouple data
  v >>= 4;

  // pull the bottom 11 bits off
  float internal = v & 0x7FF;
  // check sign bit!
  if (v & 0x800) {
    // Convert to negative value by extending sign and casting to signed type.
    int16_t tmp = 0xF800 | (v & 0x7FF);
    internal = tmp;
  }
  internal *= 0.0625; // LSB = 0.0625 degrees
  // Serial.print("\tInternal Temp: "); Serial.println(internal);
  return internal;
}
 
Last edited:
I also tried the following to understand it better so that I can see if it is run in consecutive order:
by removing the Private function in the max31855 will allow me to talk to the spi_dev.read(buf,4);
and use SPIread32.

I removed in the Void Setup the TH1.begin() to see if there is a fault. there isnt.
to actualize it I have to use TH1.SPIread32(), and TH1.spi_dev.read(buf,4) in the void loop which allows me to communicate with the Thermocouple. after access, internal and temprature readout the TFT program is accessed. all data is transferred or accessed over global variables to the TFTfunction();
the values are shown in the Serial monitor.



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 <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

SPISettings settingsTFT(25000000, MSBFIRST, SPI_MODE3);
SPISettings SettingsA(1000000, 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);

//arrays for the temperature
double TC1[12];
double *pArray_From_TC;
double  pArray_Screen;

static int a = 0; // loop function count

float internal;
double centigrade;


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(115200);
  //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();



  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);

//  if (!TH1.begin())
//  {
//    //dont do anything
//    Serial.println("Error");
//  }

}

void loop()
{
  
  //
  a++;


  
  if ((a >= 0) && (a <= 5))
  {
    uint32_t d = 0;
    uint8_t buf[4];

      TH1.spi_dev.read(buf, 4);

    d = buf[0];
    d <<= 8;
    d |= buf[1];
    d <<= 8;
    d |= buf[2];
    d <<= 8;
    d |= buf[3];

    // Serial.println(d, HEX);

    //return d;
    Serial.println("test = ");
    Serial.println(d);
  }


  else  if ((a > 5) && (a < 10)) //during the first 50ms read the sensor.
  {
    uint32_t v;
    v = TH1.spiread32();

    // ignore bottom 4 bits - they're just thermocouple data
    v >>= 4;

    // pull the bottom 11 bits off
    float internal = v & 0x7FF;
    // check sign bit!
    if (v & 0x800) {
      // Convert to negative value by extending sign and casting to signed type.
      int16_t tmp = 0xF800 | (v & 0x7FF);
      internal = tmp;
    }
    internal *= 0.0625; // LSB = 0.0625 degrees
    // Serial.print("\tInternal Temp: "); Serial.println(internal);
   
    Serial.print("Internal Temp ==");
    Serial.println(internal);

  }
  else if ((a >= 10) && (a <= 15))
  {
       uint32_t v;
       v = TH1.spiread32();
    
    if (v & 0x7) {
      // uh oh, a serious problem!
      return NAN;
    }

    if (v & 0x80000000) {
      // Negative value, drop the lower 18 bits and explicitly extend sign bits.
      v = 0xFFFFC000 | ((v >> 18) & 0x00003FFF);
    } else {
      // Positive value, just drop the lower 18 bits.
      v >>= 18;
    }
    // Serial.println(v, HEX);

    double centigrade = v;

    // LSB = 0.25 degrees C
    centigrade *= 0.25;
    
    Serial.print("centigrade");
    Serial.println(centigrade);
  }

  else if ((a >= 15) && (a <= 20) )
  {
  //other TFT function
    
    
    //TFTscreen(Thermocouples1());
    TFTfunction();
    tft.fillScreen(C_RED);
    //TFTmillis = TFTmillis - 200; //it calls the screen 4x in a second


  } else
  {
    a = 0;
    b = 0;
  }
  

}


double TFTfunction()
{
  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);

  return 0;
}




Serial Monitor puts out:
Code:
test = 
26483392
test = 
26483392
test = 
26483392
test = 
26483392
test = 
26483392
Internal Temp ==26.75
Internal Temp ==26.75
Internal Temp ==26.75
Internal Temp ==26.75
centigrade25.25
centigrade25.25
centigrade25.25
centigrade25.25
centigrade25.25
centigrade25.25
inside the TFT function
inside the TFT function
inside the TFT function
inside the TFT function
inside the TFT function
Temperature in the TFTfunction
25.00
Temperature in the TFTfunction
25.00
Temperature in the TFTfunction
25.00
Temperature in the TFTfunction
25.00
Temperature in the TFTfunction
25.00
 
Last edited:
Status
Not open for further replies.
Back
Top