The code that I presented works but the sensor readings are not menud. are read only once at startup.
I use a standard K type thermocouple. I also know that it is not read quickly. The problem is that it is not read when calling millis, but only when delay.
Yes, I know about the problem of reading too often, that’s why I wanted to use Milis, but it didn’t work.
Yes Adafruit MAX6675 library.
Here's my sketch, maybe I'm missing something?
Code: Select all
#include <Arduino.h>
#include <Wire.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
#define TFT_GREY 0x5AEB
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
uint32_t targetTime = 0; // for next 1 second timeout
static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x
int hh = 0, mm = 0, ss = 0; // Get H, M, S from compile time
byte omm = 99, oss = 99;
byte xcolon = 0, xsecs = 0;
unsigned int colour = 0;
/////////
#include <STM32RTC.h>
STM32RTC& rtc = STM32RTC::getInstance();
const byte seconds = 0;
const byte minutes = 0;
const byte hours = 0;
///////////////
int pinPID_out = PA8;
int piddata = 50;
//////////////////////
unsigned long timer;
unsigned long timing1;
/////////////
#include <RotaryEncoder.h>
#define PIN_A PB4 //ky-040 clk pin, add 100nF/0.1uF capacitors between pin & ground!!!
#define PIN_B PB3 //ky-040 dt pin, add 100nF/0.1uF capacitors between pin & ground!!!
#define BUTTON PB5 //ky-040 sw pin, add 100nF/0.1uF capacitors between pin & ground!!!
uint8_t position = 0;
RotaryEncoder encoder(PIN_A, PIN_B, BUTTON);
//////////
#include <max6675.h>
int temp1 = 0;
int thermoDO = PB14;
int thermoCS = PA15;
int thermoCLK = PB13;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
/////////////////
#include <PID_v1.h>
double Setpoint, Input_pid, Output_pid;
double Kp=20, Ki=0.1, Kd=2;
PID myPID(&Input_pid, &Output_pid, &Setpoint, Kp, Ki, Kd, DIRECT);
///////////////
void encoderISR()
{
encoder.readAB();
}
void encoderButtonISR()
{
encoder.readPushButton();
}
void setup(void) {
Serial.begin(9600);
Serial.println("Serial start.");
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(1);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
rtc.setClockSource(STM32RTC::LSE_CLOCK);
rtc.begin(); // initialize RTC 24H format
// Set the time
rtc.setHours(hh);
rtc.setMinutes(mm);
rtc.setSeconds(ss);
/////////////////////
encoder.begin(); //set encoders pins as input & enable built-in pullup resistors
attachInterrupt(digitalPinToInterrupt(PIN_A), encoderISR, CHANGE); //call encoderISR() every high->low or low->high changes
attachInterrupt(digitalPinToInterrupt(BUTTON), encoderButtonISR, FALLING); //call pushButtonISR() every high->low changes
///////
myPID.SetMode(AUTOMATIC);
delay(500);
}
void loop() {
////////////////////
//Serial.println("loop");
/////////RTС/////////////////////
hh = rtc.getHours();
mm = rtc.getMinutes();
ss = rtc.getSeconds();
/////////
int xpos = 120;
int ypos = 10;
int ysecs = ypos + 24;
tft.drawString("Job time", 10, 10,4);
////////////
if (hh < 10)
{
xpos += tft.drawChar('0', xpos, ypos, 6);
}
xpos += tft.drawNumber(hh, xpos, ypos, 6);
xpos += tft.drawChar(':', xpos, ypos - 4, 6);
if (mm < 10)
{
xpos += tft.drawChar('0', xpos, ypos, 6);
}
xpos += tft.drawNumber(mm, xpos, ypos, 6);
xpos += tft.drawChar(':', xpos, ypos - 4, 6);
if (ss < 10)
{
xpos += tft.drawChar('0', xpos, ypos, 6);
}
xpos += tft.drawNumber(ss, xpos, ypos, 6);
/////////////
myPID.Compute();
Setpoint=position;
Input_pid=temp1;
position = encoder.getPosition();
int xpos1 = 120;
int ypos1 = 60;
tft.drawString("Set temp", 10, 70,4);
if (position <10)
{
xpos1 += tft.drawChar('0', xpos1, ypos1, 6);
}
if (position <100)
{
xpos1 += tft.drawChar('0', xpos1, ypos1, 6);
}
tft.drawNumber (position, xpos1,ypos1, 6);
/////////
tft.drawString("Current temp", 10, 120,4);
int xpos2 = 160;
int ypos2 = 120;
if (temp1 <10)
{
xpos2 += tft.drawChar('0', xpos2, ypos2, 6);
}
if (temp1 <100)
{
xpos2 += tft.drawChar('0', xpos2, ypos2, 6);
}
tft.drawNumber (temp1, xpos2,ypos2, 6);
///
tft.drawString("Current PWM", 10, 180, 4);
int xpos3 = 180;
int ypos3 = 180;
if (Output_pid < 10)
{
xpos3 += tft.drawChar('0', xpos3, ypos3, 6);
}
if (Output_pid < 100)
{
xpos3 += tft.drawChar('0', xpos3, ypos3, 6);
}
tft.drawNumber(Output_pid, xpos3, ypos3, 6);
//////
// if ((millis()-timer ) >= 500)
// {
// temp1 = thermocouple.readCelsius();
// ///Serial.println(thermocouple.readCelsius());
// timer = millis();
// }
Serial.println(temp1);
temp1 = thermocouple.readCelsius();
analogWrite(pinPID_out, Output_pid);
delay(1000);
it works unstable due to delay and PID library
and if you uncomment the millis block, the temperature is read only once.