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()
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login