I feel like I am somehow overcomplicating this, but I am making a fairly basic weather station and am using an analog Hall Effect sensor (Specifically the DRV5056-Q1, as it is what I currently have) to make an anemometer, and I want it to trigger only once when the value detected is over 350 (RP2040 board, but it appears to be reading in 10 bit?) and not be able to be triggered until 20ms after it is turned off, so able to detect ~50mph max, which the 3D printed parts probably will fail before sensor. I have tried a few different methods, and even tried asking ChatGPT for help but nothing works, the best I have got is either it triggers every 20ms while above the threshold, or triggering a few times once it is past, and a then again after it is below the threshold. Can someone help me with this? Thanks!
Here is my code (sorry for not commenting it, HE means Hall Effect and everything else should be somewhat legible.):
#include <SPI.h>
#include <RH_ASK.h>
#include <DHT.h>
#include <DHT_U.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define POWER_LED 0
#define TEMP_PIN 1
#define DHT11_PIN 2
#define TX_PIN 3
#define TX_EN_PIN 4
unsigned long int blinkTimer = millis();
unsigned long int readTimer = millis();
unsigned long int transmitTimer = millis();
unsigned long int HEtimer = millis();
OneWire oneWirePin(TEMP_PIN);
DallasTemperature tempSense(&oneWirePin);
RH_ASK transmitter(2000, -1, 3, 4, false);
DHT DHT_SENSE(DHT11_PIN, DHT11);
int count;
float sumTemp;
float sumRH;
int sumLA;
int HEcount; // Marking related things with a comment
float avgTemp;
float avgRH;
int avgLA;
bool HEBool = false;
char msg[27];
void setup() {
pinMode(POWER_LED, OUTPUT);
tempSense.begin();
tempSense.setResolution(12);
DHT_SENSE.begin();
transmitter.init();
Serial.begin(115200);
}
void loop() {
if (analogRead(28) >= 400 && !HEBool && HEtimer >= 20) { //
HEcount++; //
HEBool = true; //
} else { //
HEBool = false; //
HEtimer = millis(); //
} //
if (millis() - readTimer >= 5000) {
tempSense.requestTemperatures();
count++;
sumTemp += tempSense.getTempCByIndex(0);
sumRH += DHT_SENSE.readHumidity(0);
sumLA += map(analogRead(29), 0, 1023, 0, 100);
readTimer = millis();
}
if (millis() - transmitTimer >= 60000) {
avgTemp = sumTemp / (float)count;
avgRH = sumRH / (float)count;
avgLA = sumLA / (float)count;
char tempStr[6], rhStr[6];
dtostrf(avgTemp, 4, 2, tempStr);
dtostrf(avgRH, 4, 2, rhStr);
char laStr[6], heStr[6];
itoa(avgLA, laStr, 10);
itoa(HEcount, heStr, 10); //
snprintf(msg, sizeof(msg), "T%sH%sL%sW%s", tempStr, rhStr, laStr, heStr);
size_t len = strlen(msg);
if (len < 27) {
for (size_t i = len; i < 27; i++) {
msg[i] = ' ';
}
}
Serial.println(msg);
transmitter.send((uint8_t *)msg, 27);
transmitter.waitPacketSent();
count = 0;
sumTemp = 0;
sumRH = 0;
sumLA = 0;
HEcount = 0; //
transmitTimer = millis();
}
if (millis() - blinkTimer >= 1000) {
blinkTimer = millis();
digitalWrite(POWER_LED, !digitalRead(POWER_LED));
Serial.println(HEcount); // Debugging purposes
Serial.println(analogRead(28)); // Debugging purposes
}
}
Other parts I am using:
Knockoff WaveShare RP2040 Zero
DHT11 Module
Dallas 18B20
Photoresistor (cloud cover)
433Mhz ASK transmitter
Power LED
Reading the sensor digitally, not analog fixed it, thanks! I am pretty sure an interrupt would work just fine too but this was simpler.