//#include #define HALL_SIG A3 //singal pin for hall effect #define TRIG 50 #define HALL_PWR 7 //power pin for hall effect #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) #define SAMPLE_TIME 10// number of seconds to measure rain sensor in one sample !!!change this to the number you need!!! char cedges[8]; float edges = 0; int16_t mag; int16_t l_mag = -1; int8_t rising = -1; void setup() { Serial.begin(9600); pinMode(HALL_SIG, INPUT); pinMode(HALL_PWR, OUTPUT); digitalWrite(HALL_PWR, LOW); Serial.flush(); } void loop() { float edges = count_edges(SAMPLE_TIME); dtostrf(edges,3,2,cedges); Serial.write(cedges); } uint32_t count_edges(uint32_t time_s){ uint32_t time_ms = 1000*time_s; uint32_t edge_count = 0; uint32_t tik = millis(); uint32_t sdel = 160; //l_mag = -1; //rising = -1; while(millis()-tik < time_ms){ digitalWrite(HALL_PWR, HIGH); delayMicroseconds(100); int16_t mag = analogRead(HALL_SIG); digitalWrite(HALL_PWR, LOW); edge_count += is_edge(mag); //Serial.print(edge_count); // sdel = MIN(sdel, time_ms - (millis() - tik)); // xdelay(sdel); } return edge_count; } uint8_t is_edge(int16_t mag){ //Serial.println(mag); //Serial.println(l_mag); if(l_mag == -1){ l_mag = mag; return 0; }else{ int16_t change = mag - l_mag; if((change > 0) == rising){ l_mag = mag; return 0; }else{ if (abs(change) > TRIG){ l_mag = mag; rising = !rising; return 1; } } } return 0; }