Hi fellow engineers
good morning,
I am one step further, after a hint from the Maxim Integrated guys. The MAX31855 needs to be set to SPI_MODE 0.
The PDF_TOTAL is the total schematic how it is wired on the PCB.
The upper left corner shows the DCDC converter,
pins out pump
header to front panel with all the voltages 24,12,5,3.3V with leds, RGB Encoder with a push button which is connected with 4 10K resistors and 2x 10nF caps.
The upper right corner shows all 8 OPA365, voltage follower and amplification from 140mV to 3.3V all calculated and sometimes pulled up by a resistor are read out by the Internal ADC.
The lower right shows the 18V25, VNH5019 Hbridge drivers for 3x Peltier groups of each 16.7A.
here is the pdf schematic. Ino file, recording oscilloscope (CLK looks ackward),
View attachment Snippet from the Serial monitor with both Functions running after each other.txt View attachment 2_2.pdf
so I have followed the manual as https://www.pjrc.com/better-spi-bus-design-in-3-steps/
TC0 pin14 INT 25.88 NOT 23.75
TC1 pin15 INT 26.50 NOT 25.75
TC2 pin16 INT 26.31 NOT 25.00
TC3 pin17 INT 25.38 NOT 24.25
Total Code: if Thermocouples1_4(); is commented out I get the Data on the Screen. if not commented out all data is still visible on the Serial Monitor, screen freezes
I know this is childsplay, but I would like to understand why it isn’t working properly and solve it, i tried all my tricks up my sleeve, and I am desperately in need of help.
good morning,
I am one step further, after a hint from the Maxim Integrated guys. The MAX31855 needs to be set to SPI_MODE 0.
The PDF_TOTAL is the total schematic how it is wired on the PCB.
The upper left corner shows the DCDC converter,
pins out pump
header to front panel with all the voltages 24,12,5,3.3V with leds, RGB Encoder with a push button which is connected with 4 10K resistors and 2x 10nF caps.
The upper right corner shows all 8 OPA365, voltage follower and amplification from 140mV to 3.3V all calculated and sometimes pulled up by a resistor are read out by the Internal ADC.
The lower right shows the 18V25, VNH5019 Hbridge drivers for 3x Peltier groups of each 16.7A.
here is the pdf schematic. Ino file, recording oscilloscope (CLK looks ackward),
View attachment Snippet from the Serial monitor with both Functions running after each other.txt View attachment 2_2.pdf
so I have followed the manual as https://www.pjrc.com/better-spi-bus-design-in-3-steps/
- hooked up the CS Pins with a 4.7K resistor to VCC at the MAX31855
- hooked up the MISO line to a voltage divider 10K on the breadboard. Check: no action on the MISO it floats to 1.697V on the oscilloscope, so that’s good.
- Changed the SPI settings for the MAX31855 to SPI_MODE 0, with MSBFIRST, the values such as temperatures and words are displayed on the screen but no refresh of the screen.
- The CS Pins I am using are: 14,15,16,17 on the Teensy3.6 for the MAX31855
- I am getting getting read out at the serial monitor if both functions are uncommented, and also serial prints at the end of the TFT function that are shown in the Snippet.txt
- In total there are 4 functions in void loop():
- a InternalADC
- b MenuCheck()
- c MenuCountdown()
- d Thermocouple1_4()
- The TFT MenuCountdown() program runs parallel to the menuCheck() function. By rotating the knob of the encoder I can change the value , let’s say 0…100 and by clicking again I can go to menu2 3,4, and so on, this is put on the TFT Screen in the MenuCountDown()
- These values are then copied to the same TFT function ( setting the Setpoint of the PID control )//momentarily it is commented out.
- For a Test I checked: void loop the program Thermocouple1_4(); has been commented out. I get data on the tft screen from the Currents (which is jibberish) because there is not current yet, and the Internal ADC is working in the teensy.
- If both programs are run in the void loop I get: a diagnostics check from the Screen that everything is a OK.
- The TC0,TC1 and so on are the Internal and External Temperatures from each of the Thermocouples. NOT is not calibrated the max31855 has to undergo a calibration if it goes below Zero.
TC0 pin14 INT 25.88 NOT 23.75
TC1 pin15 INT 26.50 NOT 25.75
TC2 pin16 INT 26.31 NOT 25.00
TC3 pin17 INT 25.38 NOT 24.25
Total Code: if Thermocouples1_4(); is commented out I get the Data on the Screen. if not commented out all data is still visible on the Serial Monitor, screen freezes
I know this is childsplay, but I would like to understand why it isn’t working properly and solve it, i tried all my tricks up my sleeve, and I am desperately in need of help.
Code:
/* constants ================================================================ */
const int ledPin = LED_BUILTIN;
const int ssPin = 10;
#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>
/* INPUTS and OUTPUTS
* DIR= direction of the magnet, peltier (HOT COOL) =OUTPUTS
* FLOWsensor = INPUT
* PWM= Output TEC1,TEC2,EMAGNET,PUMP different speeds for pwm.
* FLT= Faults from the Hbridge Drivers
* CS=Currentsense from the 4 HBridge drivers
*/
const int DIRmagnetLeft=0;
const int DIRmagnetRight=33;
const int DIR_TEC_Cool=2;
const int DIR_TEC_HOT=3;
const int PWM_DRV_TEC1 = 4;
const int PWM_DRV_TEC2 =5;
const int PWM_PUMP =6;
const int PWM_EMAGNET =7;
const int PWM_FAN =36;
const int FLOW_Sensor =25;
const int FLT_Peltier1 =29;
const int FLT_Peltier2 =30;
const int FLT_PMP =31;
const int FLT_EMAG =32;
const int CSTEC1=A9;
const int CSTEC2=A8;
const int CSPUMP=A7;
const int CSEMAG=A6;
const byte buttonPin = 20;
//SPI defines
#define MAXCLK 13 //max31855 nothing else.
#define MAXDO 9
#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(26000000, MSBFIRST, SPI_MODE3);
SPISettings SettingsA(5000000,MSBFIRST,SPI_MODE0);
unsigned long endmicros; //speed check variables
unsigned long currentmicros;
//menu Check values to check consistency of the menu loop.
byte menuCount = 1;
byte IntCount= 1;
byte dir = 0;
bool runState = false;
bool runState2 = false;
unsigned int count = 0; // how many times has it changed to low
unsigned long countAt = 0; // when count changed
unsigned int countPrinted = 0;
unsigned int icount = 0; // how many times has it changed to low
unsigned long icountAt = 0; // when count changed
unsigned int icountPrinted = 0;
long oldPosition = -100;
long newPosition;
long Position;
long oldPWMPosition=-100;
long PWMPosition=1;
//---------- values for the ad7124
const double Gain = 16;
const double Rf = 5.11E3;
const long Zero = 1L << 23;
const long FullScale = 1L << 24;
//variables returned from analog inputs
int CSRAWTEC1_value;
int CSRAWTEC2_value;
int CSRAWPUMP_value;
int CSRAWEMAG_value;
float Currentfactor1=5.06;
float Currentfactor2=5.06;
float Currentfactor3=5.06;
float Currentfactor4=5.06;
float TEC1Current;
float TEC2Current;
float PumpCurrent;
float EMAGCurrent;
int PMPSpeed_screen;
int EMAGNET_Screen;
long SBraw[7];
double SBvolt[7];
long dataWord;
byte statusWord;
byte ch;
/* constants ================================================================ */
int filterWord = 100;
// Sample Speed Setting (Full Power Sinc3 Filter Mode)
// FW | SPS | SPS/Ch
// 600 | 6 | 1
// 100 | 60 | 10
// 40 | 156 | 26
// 20 | 300 | 50
float j;
unsigned long StartTime, EndTime;
char str[30];
Adafruit_MAX31855 TH1(MAXCLK, 14, MAXDO);
Adafruit_MAX31855 TH2(MAXCLK, 15, MAXDO);
Adafruit_MAX31855 TH3(MAXCLK, 16, MAXDO);
Adafruit_MAX31855 TH4(MAXCLK, 17, MAXDO);
Adafruit_MAX31855 *all_sensors[] ={&TH1,&TH2,&TH3,&TH4};
//encoder Values
float TempValue=0;
int PMPSpeed=0;
int EMAGNET=0;
int SampleTime=1000; // 1 second
int Repitition_rate=1;
unsigned int Duration_ms_ON=1000; // 1 second default
unsigned int Duration_ms_OFF=1000;//default
unsigned long Time_NOW = 0;
int ON_LT3760=LOW;
int minimumLT3760=10; //minimum span needed to switch on off the lt3760
unsigned int ON_Time1=1000; // preliminary values at start up
unsigned int OFFTime1=5000;
unsigned long previousMillis=0;
unsigned long DISPLAY_REFRESH1=1000;
unsigned long Interval=1000;
unsigned long nowMillis;//important
elapsedMillis ElapsedOFF;
elapsedMillis ElapsedON;
int repeats=1;
int Intensity=0;
int i=0;
//default Configuration Current Direction Bipolar to Unipolar
boolean Bipolar=true;
boolean Unipolar=false;
int ConfigMin=2048;
int ConfigMax=4095;
int EmagnetConfig;
double c[4];
double c1[4];
double intern[4] ;
//using namespace Ad7124;
//Ad7124Chip adc;
//debounce code//
ButtonEvents myButton; // Instantiate a Bounce object
Encoder enc(27,28); //instance Encoder Rotary
ADC *IADC = new ADC(); //class ADC from the teensy read in Currents
uint8_t Stat, TFT_Stat;
//PID Parameters
double Setpoint, Input, Output;
double aggKp=4, aggKi=0.2, aggKd=1;
double consKp=1, consKi=0.05, consKd=0.25;
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
long mmap0(long x, long mn1,long mx1, long mn2, long mx2)
{
return mn2+(x-mn1)*(mx2-mn2)/(mx1-mn1);
}
long mmap1(long x, long mn1,long mx1, long mn2, long mx2)
{
return mn2+(x-mn1)*(mx2-mn2+1)/(mx1-mn1+1);
}
int InternalADC()
{
IADC->adc0->startContinuous(CSTEC1);
IADC->adc0->startContinuous(CSTEC2);
IADC->adc0->startContinuous(CSPUMP);
IADC->adc0->startContinuous(CSEMAG);
CSRAWTEC1_value = IADC->adc0->analogRead(CSTEC1);
CSRAWTEC2_value = IADC->adc0->analogRead(CSTEC2);
CSRAWPUMP_value = IADC->adc0->analogRead(CSPUMP);
CSRAWEMAG_value =IADC->adc0->analogRead(CSEMAG);
TEC1Current=CSRAWTEC1_value*Currentfactor1;
TEC2Current=CSRAWTEC1_value*Currentfactor2;
PumpCurrent=CSRAWPUMP_value*Currentfactor3;
EMAGCurrent=CSRAWEMAG_value*Currentfactor4;
}
void setup()
{
pinMode(buttonPin,INPUT);
pinMode(FLT_Peltier1,INPUT);
pinMode(FLT_Peltier2,INPUT);
pinMode(FLT_PMP,INPUT);
pinMode(FLT_EMAG,INPUT);
pinMode(CSTEC1,INPUT);
pinMode(CSTEC2,INPUT);
pinMode(CSPUMP,INPUT);
pinMode(CSEMAG,INPUT);
pinMode(DIRmagnetLeft,OUTPUT);
pinMode(DIRmagnetRight,OUTPUT);
pinMode(DIR_TEC_Cool,OUTPUT);
pinMode(PWM_DRV_TEC1,OUTPUT);
pinMode(PWM_DRV_TEC2,OUTPUT);
pinMode(PWM_PUMP,OUTPUT);
pinMode(PWM_EMAGNET,OUTPUT);
//SPI
// pinMode(AD7124_CS, OUTPUT);
pinMode(14,OUTPUT);
pinMode(15,OUTPUT);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);
pinMode(TFT_CS, OUTPUT);
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(SampleTime);
analogWriteResolution(12);
analogWriteFrequency(PWM_DRV_TEC1,25000);
analogWriteFrequency(PWM_DRV_TEC2,25000);
analogWriteFrequency(PWM_PUMP,200);
analogWriteFrequency(PWM_EMAGNET,10000);
IADC->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED);
IADC->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED); // change the sampling speed
IADC->adc0->setAveraging(4);
IADC->adc0->setResolution(12);
IADC->startSynchronizedContinuous(CSTEC1,ADC_0);
IADC->startSynchronizedContinuous(CSTEC2,ADC_0);
IADC->startSynchronizedContinuous(CSPUMP,ADC_0);
IADC->startSynchronizedContinuous(CSEMAG,ADC_0);
myButton.attach(buttonPin);
myButton.activeHigh();
myButton.debounceTime(5); //apply 15ms debounce
myButton.doubleTapTime(250);//apply 250ms Double click detection
SPI.begin();
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial)
{
;
}
for(int i=0;i>4;i++)
{
SPI.beginTransaction(SettingsA);
digitalWrite(i+14,LOW);
if (all_sensors[i]->begin())
{
Serial.println("ERROR.");
while (1) delay(10);
}
Serial.println("DONE.");
digitalWrite(i+14,HIGH);
SPI.endTransaction();
}
SPI.beginTransaction(settingsTFT);
digitalWrite(TFT_CS,LOW);
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);
digitalWrite(TFT_CS,HIGH);
SPI.endTransaction();
}
void loop()
{
nowMillis=millis();
InternalADC();
Menu1Check();
Menu1Countdown();
Thermocouples1_4();
//
}
//save value
int Menu1Check()
{
if (myButton.update()==true)
{
switch(myButton.event())
{
// things to do if the button was tapped (single tap)
case (tap) :
{
Serial.print("Single Click event detected = ");
menuCount=menuCount+1;
Serial.print("MenuCountUp = ");
Serial.println(menuCount);
break;
}
case (doubleTap) :
{
Serial.print(" DOUBLE-TAP event detected ");
menuCount=menuCount-1;
Serial.print(" DB TAP Count Down ");
Serial.println(menuCount);
break;
}
}
}
if (menuCount>8)
{
Serial.print("menu larger as 8");
menuCount = 1;
}
if (menuCount == 1)
{
Serial.println("MENU1 set SampleTime ");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
// newPosition=newPosition/4;
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>0&&newPosition<100)
{
SampleTime = newPosition;
Serial.print(" SampleTime in ms ");
Serial.print(SampleTime);
}
}
if (menuCount == 2)
{
Serial.println("MENU2 set Temperature");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
newPosition=newPosition/4;
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>-10&&newPosition<60)
{
TempValue = constrain(newPosition,-10,60);
Serial.print(" Temperature ");
Serial.print(TempValue);
//input=value from the thermocouple sensor
Setpoint=TempValue;
}
}//menuCount2
// use menuCount2 for setting 10/100th degree , add both values together after menucount2.
if (menuCount == 3)
{
Serial.println("MENU3 set Pump Speed");
/*signal is amplified with an extra circuit under the board with a OPA365, Inverted
* 3.3V to 5V the pump requires 5V pwm at 25KHz green wire.
* OPA365 8 SOIC pins 1,2,3,4. onesided dual Opamp.OPA2365.
* 6.9K and 3.7K are connected to the - pin. and 10K over the opamp.
* -- 10K-----
* 3.3V --6.9K --| - | output
* | -------------------
* |--3.7K
* 5V--- 3.9K---- +
* |
* |
* 1K
* |
* -
*/
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
}
if(newPosition>0&&newPosition<100)
{
Serial.print(newPosition);
PMPSpeed=constrain(newPosition,0,100);
newPosition=map(newPosition,0,100,0,4095);
}
}//menuCount3
if(menuCount==4)
{
Serial.print("MENU4 set Bipolar or Unipolar");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>=0&&newPosition<3)
{
Repitition_rate = newPosition;
if(newPosition==0)
{
Serial.println("Bipolar selected");
Bipolar=true;
}
if(newPosition==1);
{
Serial.println("Unipolar selected");
}
Bipolar=false;
}
}
if (menuCount == 5)
{
Serial.print("MENU5 set Emagnet Current");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>0&&newPosition<100)
{
EMAGNET=constrain(newPosition,0,100);
EmagnetConfig=map(newPosition,0,100,ConfigMin,ConfigMax);
EMAGNET=map(EMAGNET,0,100,0,17);
// digitalWrite(DIRmagnetLeft,HIGH);
// digitalWrite(DirmagnetRight,LOW);
}
else
{
analogWrite(4, 0);
}
}
if (menuCount == 6)
{
Serial.print("MENU6 set On time Duration");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
newPosition=newPosition/4;
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>0&&newPosition<100)
{
Duration_ms_ON = newPosition*1000;
Serial.print(" Duration_ms On ");
Serial.println(Duration_ms_ON);
//input=value from the thermocouple sensor
}
//out of boundary set to zero.
}
if (menuCount == 7)
{
Serial.print("MENU6 set Off Time Duration");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
newPosition=newPosition/4;
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>0&&newPosition<100)
{
Duration_ms_OFF = newPosition*1000;
Serial.print(" Duration_ms Off= ");
Serial.println(Duration_ms_OFF/1000);
//input=value from the thermocouple sensor
}
//out of boundary set to zero.
}
if (menuCount == 8)
{
Serial.print("MENU8 Repitition in Hz");
if(oldPosition>=0&&oldPosition<100)
oldPosition=0;
long newPosition = enc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
Serial.print(newPosition);
}
if(newPosition>0&&newPosition<25)
{
Repitition_rate = newPosition;
Serial.print(" Repitition_Rate in Hz ");
Serial.println(Repitition_rate);
}
//out of boundary set to zero.
}
}
int Thermocouples1_4() //see instanciation pointer to array class TH1,TH2,TH3,TH4
{
/*
* five steps to calulate from a linear voltage to temperature to non linear max31855 which truely resembles the Temperature in 5 Steps
* 1 substract the min temperature - internal temperature.
*
* 2 multiply min temperature x this Thermocouple K voltage (check max31855 datasheet on ThermocoupleK voltage which is 0.041276mV/C)
* 3 Calculate Cold Junction equivalent thermocouple voltage using the formula below with NIST
* 𝑚𝑉=𝐴0+𝐴1∗𝑇𝑒𝑚𝑝+𝐴2∗𝑇𝑒𝑚𝑝'^'2+𝐴3∗𝑇𝑒𝑚𝑝'^'3+⋯+𝐶0∗𝑒𝐶1∗(𝑇𝑒𝑚𝑝−𝐶2)'^'2
* 4 add cold junction equiv calculated in step 3.
* 5 use the result of step 4 and the NIST voltage to tempeature (inverse) coefficients B0,B1,B2 to calculate the cold junction compensation, lineariszed value
* 𝑇𝑒𝑚𝑝=𝐵0+𝐵1∗𝑚𝑉+𝐵2∗𝑚𝑉2+𝐵3∗𝑚𝑉3+⋯
* 0+(2.52∗101∗−3.528157)+(−1.17∗−3.5281572)+(−1.08∗−3.5281573)+⋯=−99.16
*/
for(int i=0;i<4;i++)
{
SPI.beginTransaction(SettingsA);
digitalWrite(14+i,LOW);
Serial.print("TC");
Serial.print(i);
Serial.print(" pin");
Serial.print(14+i);
Serial.print(" INT ");
Serial.print(all_sensors[i]->readInternal());
Serial.print(" NOT ");
Serial.println(all_sensors[i]->readCelsius());
if(all_sensors[i]->readCelsius()<0)//if temperature is lower than 0 ////Thermocouple 1
{
intern[i]=all_sensors[i]->readInternal();
c1[i] = all_sensors[i]->readCelsius();
double SubC1_int=c1-intern;
double SubCalc=SubC1_int*0.041276; /*mV*/
double E1=(-0.176*pow(10,-1));
double E2=(0.389*pow(10,-1)*pow(intern[i],1));
double E3=(0.186*pow(10,-4)*pow(intern[i],2));
double E4=(-0.995*pow(10,-7)*pow(intern[i],3));
double E5=(0.318*pow(10,-9)*pow(intern[i],4));
double E6=(-0.561*pow(10,-12)*pow(intern[i],5));
double E7=(0.561*pow(10,-15)*pow(intern[i],6));
double E8=(-0.320*pow(10,-18)*pow(intern[i],7));
double E9=(0.972*pow(10,-22)*pow(intern[i],8));
double E10=(-0.121*pow(10,-25)*pow(intern[i],9));
double E11=(0.119*pow(exp(-0.118*pow(10,-3)*(intern[i]-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 ");
Serial.println(R1_9/100000000);
if (isnan(c1[i])) {
Serial.print("Something wrong with Thermocouple ");
}
else {
Serial.print("C = ");
Serial.println(c1[i]);
}
}
else //this is the 0...500°
{
intern[i]=all_sensors[i]->readInternal();
c[i]= all_sensors[i]->readCelsius();
// double SubC1_int=c1-intern;
// double SubCalc=SubC1_int*0.041276; /*mV*/
// double H1=(-0.17600413686*pow(10,-1));
// double H2=(0.389212049750*pow(intern,1));
// double H3=(0.185587700320*pow(10,-4)*pow(intern,2));
// double H4=(-0.99457592874*pow(10,-7)*pow(intern,3));
// double H5=(0.318409457190*pow(10,-9)*pow(intern,4));
// double H6=(-0.56072844889*pow(10,-12)*pow(intern,5));
// double H7=(0.560750590590*pow(10,-15)*pow(intern,6));
// double H8=(-0.320200720003*pow(10,-18)*pow(intern,7));
// double H9=(0.971511471520*pow(10,-22)*pow(intern,8));
// double H10=(-0.121047212750*pow(10,-25)*pow(intern,9));
// double H11=(0.1185976*pow(exp(-0.118343200*pow(10,-3)*(intern-0.126986*pow(10,3))),2));
// double H1_11=H1+H2+H3+H4+H5*H6+H7+H8+H9+H10+H11;
// double AddCJError=H1_11+SubCalc;
//// double E1=(-1.318058*pow(10,2)*pow(AddCJError,0));//inverse coefficients probably not the right one.
//// double E2=(4.830222*pow(10,1)*pow(AddCJError,1));
//// double E3=(-1.64603*pow(10,0)*pow(AddCJError,2));
//// double E4=(5.46473*pow(10,-2)*pow(AddCJError,3));
//// double E5=(-9.65071*pow(10,-4)*pow(AddCJError,4));
//// double E6=(8.802193*10*pow(10,-6)*pow(AddCJError,5));
//// double E7=(-3.11081*pow(10,-8)*pow(AddCJError,6));
//// double E8=(0*pow(10,0)*pow(AddCJError,7));
//// double E9=(0*pow(10,0)*pow(AddCJError,8));
//// double E10=(0*pow(10,0)+pow(AddCJError,9));
//
// double E1=1;
// double E2=(2.508355*pow(10,1)*pow(AddCJError,1));
// double E3=(7.86010*pow(10,-2)*pow(AddCJError,2));
// double E4=(-2.50313*pow(10,-1)*pow(AddCJError,3));
// double E5=(-8.3152702*pow(10,-2)*pow(AddCJError,4));
// double E6=(-1.228034*10*pow(10,-2)*pow(AddCJError,5));
// double E7=(-9.804036*pow(10,-4)*pow(AddCJError,6));
// double E8=(-4.413030*pow(10,-5)*pow(AddCJError,7));
// double E9=(1.057734*pow(10,-6)*pow(AddCJError,8));
// double E10=(-1.052755*pow(10,-8)+pow(AddCJError,9));
// double E1_9=E1+E2+E3+E4+E5+E6+E7+E8+E9+E10;
// double CorVal=(E1_9/100000000);
//
// Serial.print(" Sensor ");
// Serial.print(i);
// Serial.print(" Real Temp ");
// Serial.println(CorVal);
//Serial.print("F = ");
//Serial.println(TH1.readFahrenheit());
digitalWrite(14+i,HIGH);
SPI.endTransaction();//----------------------------------------------------------------------------------
}
}//for
}//millis
//thermocuple function
int Menu1Countdown() //Menu 1 //Display
{
// get some data
j += 0.0013;
currentmicros = micros();
long value;
double voltage;
Serial.print("test TC0");
Serial.println(c[0]);
Serial.print("test TC1");
Serial.println(c[1]);
Serial.print("test TC2");
Serial.println(c[2]);
Serial.print("test TC3");
Serial.println(c[3]);
/* all graphics commands have to appear within the loop body. */
SPI.beginTransaction(settingsTFT);
digitalWrite(TFT_CS,LOW);
Data1.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.print("Emagnet program");
Data1.print(j,2);
// // now let's see the tft update with FlickerFree
tft.setTextSize(1);
tft.setCursor(0,20);
Data1.setTextColor(C_WHITE, C_BLACK);
tft.print("Temperature readout");
tft.print(" Voltages from Thermocouples ");
tft.setTextSize(2);
Data1.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(20,45);
Data1.print(j,2);
sprintf(str,"TempEmag: %lu",c[0]);
Data1.print(str);
tft.setCursor(20,60);
Data1.print(c[0]); //C is a global variable!
Data1.print(j,2);
tft.setCursor(20,75);
sprintf(str,"TempWater: %lu",c[1]);
Data1.print(str);
Data1.print(j,2);
tft.setCursor(20,90);
sprintf(str,"TempTEC2: %lu",c[2]);
Data1.print(str);
Data1.print(j,2);
tft.setCursor(20,105);
sprintf(str,"Temp: %lu",c[3]);
Data1.print(str);
tft.print(c[3]);
tft.print(" C");
tft.setCursor(20,120);
tft.setTextSize(2);
tft.setCursor(250,45);
tft.print("Currents");
tft.setTextSize(2);
tft.setCursor(250,60);
Data1.print(TEC1Current);
tft.print("A");
tft.setCursor(250,75);
Data1.print(TEC2Current);
tft.print("A");
tft.setCursor(250,90);
Data1.print(PumpCurrent);
tft.print("A");
tft.setCursor(250,105);
Data1.print(EMAGCurrent);
tft.print("A");
if(menuCount>0&&menuCount==1)
{ Serial.println("Starting MENU1");
//Sample Time
tft.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(2);
tft.setCursor(10,130);
tft.print("Variables to SET");
tft.setTextSize(1);
tft.setCursor(10,150);
tft.print(" Setting Variables by rotating enc and press button to save");
tft.setCursor(10, 170);
tft.print("1* Samples/sec ");
tft.print(SampleTime);
}
if (menuCount > 1 && menuCount == 2)
{ Serial.println("Starting MENU2");
//change PWMTEC2 Value on screen
tft.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(1);
tft.setCursor(10,190);
tft.print("2* Temp Setpoint= ");
tft.print(TempValue);
//Data1.print(SBvolt[0]);
Input=0;//readin the temperature value from the sensor AD7124
//out of boundary set to zero.
double gap = abs(Setpoint-Input); //distance away from setpoint
if (gap < 10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
}
if (menuCount > 2 && menuCount == 3)
{ Serial.println("Starting MENU3");
//set pump speed on the screen in PWM in 100%
tft.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(1);
tft.setCursor(10,210);
tft.print("3* PMPSPD_100% = ");
tft.print(PMPSpeed);
}
if(menuCount>3 &&menuCount==4)
{ Serial.println("Starting MENU4");
//Emagnet current
tft.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(1);
tft.setCursor(10,230);
tft.print("4* Set HALF or FULL Square Waveform ");
if(Bipolar==true)
{
tft.print(" Bipolar");
}
else
{
tft.print("Unipolar");
}
}
if(menuCount>4 &&menuCount==5)
{
Serial.println("Starting MENU5");
//select Bipolar or Unipolar
tft.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(1);
tft.setCursor(10,250);
tft.print("5* Current Emagnet ");
tft.print(EMAGNET);
}
if(menuCount>5 &&menuCount==6)
{Serial.println("Starting MENU6");
//set on Duration ms for the Emagnet
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(10, 270);
tft.setTextSize(1);
tft.print("5* OFFTime in ms ");
tft.print(Duration_ms_ON);
}
if(menuCount>6 &&menuCount==7)
{ Serial.println("Starting MENU7");
analogWrite(PWM_DRV_TEC1, Output);
analogWrite(PWM_DRV_TEC2, Output);
//set off Duration ms for the Emagnet
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(10, 290);
tft.setTextSize(1);
tft.print("6* ON_Time1 in ms ");
tft.print(Duration_ms_OFF);
}
if(menuCount>7&&menuCount==8)
{ Serial.println("Starting MENU8");
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(10,310);
tft.setTextSize(1);
tft.print("7* Rep-Rate in Hz ");
tft.print(Repitition_rate);
for(int i=0;i<Repitition_rate;i++)
{
if(ElapsedOFF>=Duration_ms_OFF) //OFFTime first
{
if(Bipolar==false)
{
tft.fillRect(100,200,10, 10, C_RED);
//ON_LT3760=LOW;
// ElapsedOFF=ElapsedOFF-OFFTime1;
ElapsedOFF=0; ElapsedON=0;
analogWrite(4, 0);
digitalWrite(2,LOW);//DIREMAGNETLEFT;
}
else if(ElapsedON>=Duration_ms_ON)//ONTime Second
{
//add repetition rate Bipolar and Unipolar are taken into the timing.
if(Bipolar==true)
{
tft.fillRect(100,200,10, 10, C_GREEN);
//ON_LT3760=HIGH;
// delay(ON_Time1+minimumLT3760);
digitalWrite(2,HIGH);//DIREMAGNETLEFT;
analogWrite(4, EmagnetConfig);
digitalWrite(33,HIGH);//DIREMAGNETRIGHT;
}
else
{
analogWrite(4, EmagnetConfig);
digitalWrite(2,HIGH);//DIREMAGNETLEFT;
digitalWrite(33,LOW);//DIREMAGNETRIGHT switched off.
}
}
}
}
}
if(menuCount>8&&menuCount==9)
{Serial.println("Starting MENU9");
tft.setTextColor(C_WHITE, C_BLACK);
tft.setCursor(120,290);
tft.setTextSize(1);
tft.print(" ");
}
if(menuCount>9&&menuCount==10)
{
Serial.println("Starting Last MENU");
menuCount=1;
}
tft.setTextColor(C_WHITE, C_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 300);
tft.print("M");
tft.print(menuCount);
// set PWM and Temperature
// Data1.setTextColor(C_WHITE, C_BLACK);
// tft.setTextSize(2);
// tft.setCursor(240, 45);
// tft.print(" Set Setpoint TEMP EMAG");
// tft.setCursor(240, 60);
// tft.print(" Temp Setpoint TEC2 100%");
// tft.setCursor(240, 75);
// tft.print(" PWM PMP 100%");
// you must call the text colors for the object
//
// StartTime = millis();
//
// // one call and flicker free paint is done
// tft.setCursor(300, 200);
// tft.setTextSize(2);
// Data1.print(j, 2);//for digits behidn teh comma
//
// // lets see how fast
// EndTime = millis() - StartTime;
// sprintf(str, "=%lu", EndTime);
// Data1.print(str);
digitalWrite(TFT_CS,HIGH);
SPI.endTransaction();
}