Analogread functions with different time intervals

Hi!,
which is the best way to run “two parallel loop functions”, each one for adquiring different values from an analogread functionwith different time intervals between samples? thanks

I prefer to put the code in loop(), using millis() as the timer. While there are various libraries and things like software timers, there are often restrictions on what you can do from the timer callbacks and I find this solution to be the most trouble-free.

#include "Particle.h"

const int SENSOR_A_PIN = A2;
const unsigned long SENSOR_A_INTERVAL = 10000; // milliseconds
unsigned long lastSensorA = 0;

const int SENSOR_B_PIN = A3;
const unsigned long SENSOR_B_INTERVAL = 30000; // milliseconds
unsigned long lastSensorB = 0;

void setup() {
}

void loop() {
	if (millis() - lastSensorA >= SENSOR_A_INTERVAL) {
		lastSensorA = millis();
		String str(analogRead(SENSOR_A_PIN));
		Particle.publish("Sensor_A", str, 60, PRIVATE);
	}
	if (millis() - lastSensorB >= SENSOR_B_INTERVAL) {
		lastSensorB = millis();
		String str(analogRead(SENSOR_B_PIN));
		Particle.publish("Sensor_B", str, 60, PRIVATE);
	}
}
2 Likes