Diferencia entre revisiones de «Medidor de parpadeo de luz»
| Línea 18: | Línea 18: | ||
#include "I2C_LCD.h" | #include "I2C_LCD.h" | ||
const int COLLECTION_MS = | const int COLLECTION_MS = 100; | ||
const int SENSOR_LIMIT = 850; | const int SENSOR_LIMIT = 850; | ||
const int SENSOR_BAJO = 200; | const int SENSOR_BAJO = 200; | ||
const int STOP_PIN = 3; | |||
const int INTERRUPT_SETTLE_MS = 1000; | |||
I2C_LCD lcd(39); | I2C_LCD lcd(39); | ||
volatile bool run = true; | |||
void stop() { | |||
if (millis() > INTERRUPT_SETTLE_MS) run = false; | |||
} | |||
void setup() { | void setup() { | ||
| Línea 28: | Línea 36: | ||
Wire.setClock(100000); | Wire.setClock(100000); | ||
lcd.begin(16, 2); | lcd.begin(16, 2); | ||
pinMode(STOP_PIN, INPUT_PULLUP); | |||
attachInterrupt(digitalPinToInterrupt(STOP_PIN), stop, LOW); | |||
} | } | ||
| Línea 34: | Línea 44: | ||
unsigned long stop_ms = millis() + COLLECTION_MS; | unsigned long stop_ms = millis() + COLLECTION_MS; | ||
int sample_count = 0; | int sample_count = 0; | ||
while (millis() < stop_ms) { | while (millis() < stop_ms) { | ||
int x = analogRead(A0); | int x = analogRead(A0); | ||
| Línea 39: | Línea 50: | ||
if (x < x_min) x_min = x; | if (x < x_min) x_min = x; | ||
sample_count++; | sample_count++; | ||
} | } | ||
if (!run) return; | |||
lcd.clear(); | lcd.clear(); | ||
| Línea 52: | Línea 66: | ||
lcd.center(1, ("samples = " + String(sample_count)).c_str()); | lcd.center(1, ("samples = " + String(sample_count)).c_str()); | ||
} | } | ||
} | } | ||
</pre> | </pre> | ||