Ultrasonic sensor cannot read with timer library

Hi
I use A02YYUW ultrasonic sensor from DFrobot company.
The sample code provided by this company works well for distance measurement.
But it doesn't work well for the project I want.

I'm trying to use the Thread library to work with other sensors. Adding the provided sample code using this sensor doesn't work.

ultdis() ;

please check.

#include <Thread.h>
#include <ThreadController.h>

#include <ModbusMaster.h>
#include <SoftwareSerial.h>
#include <DFRobot_MAX31855.h>

ThreadController control1 = ThreadController();

Thread T_yaw = Thread();
Thread T_dis = Thread();
Thread T_temp = Thread();
Thread T_print = Thread();

SoftwareSerial Yaw(6,7);      
ModbusMaster Yaw_sensor;      
float Yaw_temp;             
float Yaw_x;               
float Yaw_y;              
float Yaw_z;                  

SoftwareSerial dis(10,11);
float distance;
unsigned char data_dis[4]={};

DFRobot_MAX31855 max31855;
float temp1;

/*--------------------------------------------------------------------------------------------------------------------*/
void setup() {
   
  Serial.begin(9600);

  Yaw.begin(9600);           
  Yaw_sensor.begin(80,Yaw);  

  T_yaw.onRun(yawdata);      T_yaw.setInterval(100);  
  T_temp.onRun(tempdata);    T_temp.setInterval(100);
  T_print.onRun(dataprint);  T_print.setInterval(1000);
  T_dis.onRun(ultdis);       T_dis.setInterval(300);
  
  control1.add(&T_yaw);
  control1.add(&T_temp);
  control1.add(&T_print);
  control1.add(&T_dis);

}
/*--------------------------------------------------------------------------------------------------------------------*/
void loop(){
  control1.run();
}
/*--------------------------------------------------------------------------------------------------------------------*/
void yawdata(){
  uint8_t result;
  uint16_t data[6];
  result = Yaw_sensor.readHoldingRegisters(0x3d,4);
  if (result == Yaw_sensor.ku8MBSuccess)
  {
    Yaw_x =  Yaw_sensor.getResponseBuffer(0x00)/ 32768.0*180.0;
    Yaw_y =  Yaw_sensor.getResponseBuffer(0x01)/ 32768.0*180.0;
    Yaw_z =  Yaw_sensor.getResponseBuffer(0x02)/ 32768.0*180.0;
    Yaw_temp =  Yaw_sensor.getResponseBuffer(0x03)/100.0;
  }
}
/*--------------------------------------------------------------------------------------------------------------------*/
void tempdata(){
  temp1 = max31855.readCelsius();
}
/*--------------------------------------------------------------------------------------------------------------------*/
void ultdis(){
  do
  {
    for(int j=0;j<4;j++)
    {
      data_dis[j]=dis.read();
    }
  }while(dis.read()==0xff);
  
  dis.flush();
  
  if(data_dis[0]==0xff)
    {
      int sum_dis;
      sum_dis=(data_dis[0]+data_dis[1]+data_dis[2])&0x00FF;
      if(sum_dis==data_dis[3])
      {
        distance=(data_dis[1]<<8)+data_dis[2];
      }
     } 
}
/*--------------------------------------------------------------------------------------------------------------------*/

void dataprint(){
  Serial.print(F("x="));
  Serial.print(Yaw_x);
  Serial.print(F(", y="));
  Serial.print(Yaw_y);
  Serial.print(F(", z="));
  Serial.print(Yaw_z);
  Serial.print(F(", yaw_temp="));
  Serial.print(Yaw_temp);
  Serial.print(F(", temp1="));
  Serial.print(temp1); 
  Serial.print(F(", distance"));
  Serial.println(distance/10);
}

Perhaps you run out of timers with so many specialized libraries?

Using two instances of SoftwareSerial usually doesn't work as expected. You can receive data only on one instance.

Using multiple threads isn't a clever idea on an MCU too even though the Arduino doesn't allow for real threads anyway. Call the routines directly from the loop() your library does the same, it just make the code more complex without a real win.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.