Sampling rate for sensor when combined
-
I have combined Relay and MQ+DHT
What i would like to do is set a sampling period for a sensor at say 5 seconds.
Is this supposed to be done with millis? Can somebody help with the code sample?Let's presume for MQ as an exxample
float mq_reading = analogRead(MQ_Pin); if (isnan(mq_reading)) { Serial.println("Failed mq_reading"); } else if (mq_reading != last_mq_reading) { last_mq_reading = mq_reading; gw.send(msgMQ.set(mq_reading, 1)); Serial.print("MQ: "); Serial.println(mq_reading); } }```
-
Hi!
Yes - im using millis() if i dont sleep the node (which seems overkill if you only wants 5 seconds).
What you need to find out if you sensors have the possibility to read that fast. For the dht i think thats about when you just run the normal reading without delay in the loop.
-
Can you give a code example? Currently it reports every second almost
-
long previousMillis = 0; // will store last time something happened long interval = 5000; // interval at which to blink (milliseconds)
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { // save the last time something happened previousMillis = currentMillis; Insert your code here... } }
Note millis() does not work if you are using gw.sleep()
4 out of 4
Suggested Topics
-
Update RF24 library to latest version
Bug Reports • 23 Mar 2014, 23:37 • andriej 24 Mar 2014, 22:52 -
ESP-NOW
General Discussion • 22 Apr 2018, 05:58 • NeverDie 17 Feb 2025, 22:24 -
No merge into master in the last 5 years, should we use development?
General Discussion • 23 Sept 2024, 17:48 • kiesel 6 days ago -
Why is the output of ACS712 current measurement module unchanged?
General Discussion • 19 Jul 2021, 09:09 • Tessie T 2 days ago -
Meet in Malmö, Summer 2016?
General Discussion • 1 Feb 2016, 15:34 • bjacobse 28 days ago -
Running out of nodeId's
General Discussion • 3 Sept 2024, 07:33 • NielBierman 25 Jan 2025, 17:19