Help with time keeping and loops

NOOB QUESTION: Hi everyone . Going nuts trying to figure something out. I have a load sensor on top of a box. Leave your phone on the box for X minutes and the box pops open. So I need to measure the start time, then wait until some time passes, and then open the box while flashing a LED. I can't seem to find how to do it without resetting the start time inside the loop(). this is as far as I got... Anyone?

#include "HX711.h"
#include <Servo.h>

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
Servo myservo;  // create servo object to control a servo

HX711 scale;

int led = 13; // the pin the LED is connected to
const int openTime = 10;   // seconds to open box
unsigned long currentMillis; 


void setup() {
  Serial.begin(38400);
// myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo.write(70);
  
  pinMode(led, OUTPUT); // Declare the LED as an output
  digitalWrite(led, LOW); // Make sure LED is off
    
  Serial.println("HX711 Demo");

  Serial.println("Initializing the scale");

  // Initialize library with data output pin, clock input pin and gain factor.
  // Channel selection is made by passing the appropriate gain:
  // - With a gain factor of 64 or 128, channel A is selected
  // - With a gain factor of 32, channel B is selected
  // By omitting the gain factor parameter, the library
  // default "128" (Channel A) is used here.
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);


}

void loop() {
  Serial.print("one reading:\t");
  Serial.print(scale.get_units()*2.53, 1);
  Serial.print("\t| average:\t");
  Serial.println(scale.get_units(10), 1);

  if (scale.get_units()*2.53 > 100)
      {
            currentMillis = millis()/1000;
            digitalWrite(led, HIGH); // Turn the LED on  
            blink();
      }
  scale.power_down();			        // put the ADC in sleep mode
  delay(1000);
  scale.power_up();
}

void blink()  
  {         
  if (millis()/1000 == currentMillis + openTime) {
  //myservo.write(170);
  digitalWrite(led, LOW); // Turn the LED off          
  delay(1000);// Wait for 1000 milliseconds (1 second) // 1 h = 3600000 ms
  digitalWrite(led, HIGH); // Turn the LED on  
  delay(1000);// Wait for 1000 milliseconds (1 second) // 1 h = 3600000 ms
    }
  }

Save the start time when the start condition becomes true rather than when it is true

See the StateChangeDetection example in the IDE

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