#define MEASURE A0 #define ACTION 8 #define TS 20 bool bAction = 0; float action = 0.0; float measure = 0.0; float lastMeasure = 0.0; float Mo = 0; long T = 10000; long newT = T; long t_1 = millis(); long t_0 = millis(); long lastSample = millis(); void setup() { Serial.begin(115200); pinMode(ACTION, OUTPUT); } void loop() { // Actuation t_0 = millis(); if ((t_0 - t_1) * 2 > T) { t_1 = t_0; bAction = !bAction; action = 5 * bAction; digitalWrite(ACTION, bAction); Serial.print("Mi=5,Mo="); Mo = abs(lastMeasure - measure); Serial.println(Mo, 4); lastMeasure = measure; if (newT != T) { T = newT; } } // Measuring measure = analogRead(MEASURE); measure = measure * 5 / 1023; // Sampling long sample = millis(); if ((sample - lastSample) > TS) { lastSample = sample; Serial.print(action); Serial.print(","); Serial.println(measure); } } void serialEvent() { while (Serial.available()) { long periodo = Serial.parseFloat() * 1000; if (periodo > 0) { newT = periodo; } } }