Help with a timer in my code

Hi, I have a TOF sensor I am using to at the top of a roll up door. I am wanting to add a line to the loop sketch that will only run D7 if the sensor is picking up 140mm and its like this for 10 minutes.

thanks in advancel

/////

unsigned char ok_flag;
unsigned char fail_flag;
unsigned short lenth_val = 0;
unsigned char i2c_rx_buf[16];
unsigned char dirsend_flag=0;
int LID = D7;


void setup() {
  Wire.begin(); 
  Serial.begin(9600); 
  pinMode(LID, OUTPUT);
  }

void loop() {
   int x=ReadDistance();
   Serial.print(x);
   Serial.println(" mm");
   
  
       if (lenth_val < 140) {  /* looking to add a timer here before tripping the relays, <140mm for 10 minutes then digitalwrite High..../*
       digitalWrite(LID, HIGH);
       delay(1000); 
       digitalWrite(LID, LOW);

       }
}
int serial_putc( char c, struct __file * )
{
  Serial.write( c );
  return c;}
  
void SensorRead(unsigned char addr,unsigned char* datbuf,unsigned char cnt) 
{
  unsigned short result=0;
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(82); // transmit to device #82 (0x52)
  // the address specified in the datasheet is 164 (0xa4)
  // but i2c adressing uses the high 7 bits so it's 82
  Wire.write(byte(addr));      // sets distance data address (addr)
  Wire.endTransmission();      // stop transmitting
  // step 2: wait for readings to happen
  delay(1);                   // datasheet suggests at least 30uS
  // step 3: request reading from sensor
  Wire.requestFrom(82, cnt);    // request cnt bytes from slave device #82 (0x52)
  // step 5: receive reading from sensor
  if (cnt <= Wire.available()) { // if two bytes were received
    *datbuf++ = Wire.read();  // receive high byte (overwrites previous reading)
    *datbuf++ = Wire.read(); // receive low byte as lower 8 bits
  }
}

int ReadDistance(){
    SensorRead(0x00,i2c_rx_buf,2);
    lenth_val=i2c_rx_buf[0];
    lenth_val=lenth_val<<8;
    lenth_val|=i2c_rx_buf[1];
    delay(300); 
    return lenth_val;
}

Not exactly sure what your issue is, but going by the title you may want to look at this
https://docs.particle.io/reference/device-os/firmware/argon/#software-timers

I wouldn’t use an actual timer. I’d use something like this:

Global variables:

const std::chrono::milliseconds lowLevelDuration = 10min;
uint64_t lowLevelStartTime = 0;
bool lowLevelNotified = false;

In your loop code:

if (lenth_val < 140) {
    if (lowLevelStartTime == 0) {
        lowLevelStartTime = System.millis();
    }
}
else {
    lowLevelStartTime = 0;
    lowLevelNotified = false;
}

if ((lowLevelStartTime != 0) && ((lowLevelStartTime - System.millis()) >= lowLevelDuration.count()) && !lowLevelNotified) {
    lowLevelNotified = true;
    digitalWrite(LID, HIGH);
    delay(1000); 
    digitalWrite(LID, LOW);
}

thanks! Rick was exactly what I needed

cool tip about the chrono literals.

I corrected a bug in the code so it only notifies once now, instead of continuously.

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