sstaub / Ticker

Ticker library for Arduino
MIT License
192 stars 37 forks source link

run times error #56

Closed morestart closed 2 years ago

morestart commented 2 years ago

this is my code

I want to print pt100 data 5 times every seconds, but this timer just printed 5 times after it will not print anything

#include <Arduino.h>
#include "DHTesp.h"
#include "Ticker.h"
#include <Wire.h>
#include <Adafruit_MAX31865.h>
#include <SPI.h>
#include <pt100rtd.h>
#ifdef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)
#error Select ESP8266 board.
#endif
#define C2F(c) ((9 * c / 5) + 32)

DHTesp dht;
int a;
unsigned long nowtime;
// CS, DI, DO, CLK D0, D1, D2, D3
Adafruit_MAX31865 max1 = Adafruit_MAX31865(16, 5, 4, 0);
pt100rtd PT100 = pt100rtd();
#define RREF      430.0
#define RNOMINAL  100.0

void send_hum_tem_data();
void read_rt_100_tem();

// Ticker timer1(send_hum_tem_data, 1000, 5);
Ticker timer2(read_rt_100_tem, 1000, 5);

void send_hum_tem_data(){
  char str[100];

  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();

  //用sprintf来格式化字符串,给n0的val属性赋值
  sprintf(str, "t16.txt=\"%.2f\"\xff\xff\xff", humidity);
  //把字符串发送出去
  Serial.print(str);

  //用sprintf来格式化字符串,给t0的txt属性赋值
  // sprintf(str, "t15.txt=\"%.2f\"\xff\xff\xff", temperature);
  //把字符串发送出去
  // Serial.print(str);
}

void setup() {
  // put your setup code here, to run once:
  //初始化串口
  Serial.begin(115200);
  max1.begin(MAX31865_4WIRE);
  dht.setup(D4, DHTesp::DHT11);

  //因为串口屏开机会发送88 ff ff ff,所以要清空串口缓冲区
  while (Serial.read() >= 0); //清空串口缓冲区
  // 启动定时器1
  // timer1.start();
  timer2.start();
}
void checkFault(void)
{
    // Check and print any faults
    uint8_t fault = max1.readFault();
    if (fault)
    {
        Serial.print("Fault 0x"); Serial.println(fault, HEX);
        if (fault & MAX31865_FAULT_HIGHTHRESH)
        {
            Serial.println("RTD High Threshold"); 
        }
        if (fault & MAX31865_FAULT_LOWTHRESH)
        {
            Serial.println("RTD Low Threshold"); 
        }
        if (fault & MAX31865_FAULT_REFINLOW)
        {
            Serial.println("REFIN- > 0.85 x Bias"); 
        }
        if (fault & MAX31865_FAULT_REFINHIGH)
        {
            Serial.println("REFIN- < 0.85 x Bias - FORCE- open"); 
        }
        if (fault & MAX31865_FAULT_RTDINLOW)
        {
            Serial.println("RTDIN- < 0.85 x Bias - FORCE- open"); 
        }
        if (fault & MAX31865_FAULT_OVUV)
        {
            Serial.println("Under/Over voltage"); 
        }
        max1.clearFault();
    }
}

void read_rt_100_tem()
{
  char str[100];
  uint16_t rtd, ohmsx100 ;
    uint32_t dummy ;
    float ohms, Tlut ;  
    float Tcvd, Tcube, Tpoly, Trpoly ;

    rtd = max1.readRTD();

    // fast integer math:
    // fits in 32 bits as long as (100 * RREF) <= 2^16,
    //  i.e., RREF must not exceed 655.35 ohms (heh).
    // TO DO: revise for 4000 ohm reference resistor needed by Pt1000 RTDs

    // Use uint16_t (ohms * 100) since it matches data type in lookup table.
    dummy = ((uint32_t)(rtd << 1)) * 100 * ((uint32_t) floor(RREF)) ;
    dummy >>= 16 ;
    ohmsx100 = (uint16_t) (dummy & 0xFFFF) ;

    // or use exact ohms floating point value.
    ohms = (float)(ohmsx100 / 100) + ((float)(ohmsx100 % 100) / 100.0) ;

    // Serial.print("rtd: 0x") ; Serial.print(rtd,HEX) ;
    // Serial.print(", ohms: ") ; Serial.println(ohms,2) ;

  // compare lookup table and common computational methods

    Tlut    = PT100.celsius(ohmsx100) ;         // NoobNote: LUT== LookUp Table
  sprintf(str, "t15.txt=\"%.2f\"\xff\xff\xff", Tlut);
  Serial.print(str);
    // Tcvd = PT100.celsius_cvd(ohms) ;             // Callendar-Van Dusen calc
    // Tcube    = PT100.celsius_cubic(ohms) ;           // Cubic eqn calc
    // Tpoly    = PT100.celsius_polynomial(ohms) ;          // 5th order polynomial
    // Trpoly   = PT100.celsius_rationalpolynomial(ohms) ;  // ugly rational polynomial quotient

    // // report temperatures at 0.001C resolution to highlight methodological differences
    // Serial.print("Tlut   = ") ; Serial.print(Tlut  ,3) ; Serial.println(" C (exact)") ;
    // Serial.print("Tcvd   = ") ; Serial.print(Tcvd  ,3) ; Serial.println(" C") ;
    // Serial.print("Tcube  = ") ; Serial.print(Tcube ,3) ; Serial.println(" C") ;
    // Serial.print("Tpoly  = ") ; Serial.print(Tpoly ,3) ; Serial.println(" C") ;
    // Serial.print("Trpoly = ") ; Serial.print(Trpoly,3) ; Serial.println(" C") ;
    // Serial.println();

    // checkFault() ;
}
void loop() {
  // timer1.update();
  timer2.update();
    // delay(5000) ;
}
sstaub commented 2 years ago

Wrong configuration you must use Ticker timer2(read_rt_100_tem, 200); // call every 200ms

morestart commented 2 years ago

Wrong configuration you must use Ticker timer2(read_rt_100_tem, 200); // call every 200ms

sure, i change this place, but i find it jsut print when i reset my board...

morestart commented 2 years ago

i see the doc write this: Ticker timer3(printCountdown, 1000, 5); // 5 times, every second

morestart commented 2 years ago

well, i use this, it works very good

Ticker timer1(send_hum_tem_data, 200); Ticker timer2(read_rt_100_tem, 200);

sstaub commented 2 years ago

Sorry, I don't understand the problem "but i find it jsut print when i reset my board...." The doc is ok, You can count infinity or a limited number.