I have a setup consisting of a magnet suspended above a sensor coil. It acts as a pendulum and creates a ~ 4 mV signal when it swings over the coil - this goes through two stages of an LM358 to generate a 5V signal. The signal is very messy with lots of 'ringing' This signal is fed into pin A0 of an Arduino Uno. I used MegunoLink.h and Filter.h which is an exponential filter library then applied constrain and map, finally converting to a clean 0 - 400 (units?) square wave of about 1 second with a varying duty cycle. This is the information I want as it tells me that the magnet is homing in on the sensor (the duty cycle increases as it gets close). But when I apply pulseInLong I only get a string of zeros on the serial monitor. What am I doing wrong? Here is the sketch:
// Coding by Jeremy 130520
// further development for ricochet module
//https://www.megunolink.com/documentation/arduino-libraries/exponential-filter/
//https://www.google.com/search?q=how+to+use+arduino+pulsein
#include "MegunoLink.h"
#include "Filter.h"
// Create a new exponential filter with a weight of 5 and an initial value of 0.
ExponentialFilter<long> ADCFilter(5, 0);
int coilPin = 8;
int analogIn = A0;
unsigned long pulseLength;
unsigned long signal_1;
void setup()
{
pinMode(coilPin, OUTPUT);
pinMode(analogIn,INPUT);
Serial.begin(9600);
}
void loop()
{
int RawValue = analogRead(analogIn);
ADCFilter.Filter(RawValue);
signal_1 =(ADCFilter.Current());
signal_1 = constrain(signal_1,80, 200);
signal_1 = map(signal_1, 80, 200,0,400);
if (signal_1 >= 200) {signal_1 = 400; digitalWrite(coilPin, HIGH);}
if (signal_1 < 200) {signal_1 = 0; digitalWrite(coilPin, LOW);}
Serial.println(signal_1); //using the plotter
//pulseLength = pulseInLong(coilPin, HIGH);
//Serial.println(pulseLength);
delay(10);
}