I have a linear actuator that has a built in hall effect sensor. Aside from the motor power wires (it's 12V and you just reverse polarity to change direction as it's made for automotive applications), it has three more wires. +VCC, GND, and PULSE. It says +VCC can be anything from 5-20VDC.
So I have it powered from the 5V out pin on an Arduino Leonardo. The PULSE line is connected to IO pin 2, and GND is connected to the Arduino. When I run the actuator (manually, it's not controlled by the Arduino yet), I see the following on my oscilloscope:
That leads me to believe, if I remember my theory properly, that I've got about a 20Hz pulse output (10ms per graduation would be about 50ms long for a full pulse which is 20Hz) as it runs (and thus I could measure distance over time and get a distance per pulse if I were so inclined).
Now, the problem is I need to sense and count these pulses. Seems like a perfect job for an interrupt. So I tried the following code and the LED appears to blink rapidly as I move the actuator as it probably should:
int pin = 13;
volatile int state = LOW;
int counter = 0;
void setup()
{
Serial.begin(9600);
attachInterrupt(1, talk, CHANGE);
}
void loop()
{
digitalWrite(pin, state);
}
void talk()
{
state = !state;
Serial.print(counter);
counter++;
}
20Hz is pretty fast, but it looks like about 20Hz.
So then I go a step further and try to see some serial output of what's going on because ultimately I want to control the actuator via serial (or the network, but my guess is that still means serial to some network device). So I do the following:
volatile byte half_revolutions;
void setup()
{
Serial.begin(115200);
attachInterrupt(1, magnet_detect, CHANGE); //Initialize the intterrupt pin (Arduino digital pin 2)
half_revolutions = 0;
}
void loop()
{
}
void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
{
half_revolutions++;
Serial.println("detect");
}
Now, yes, I realize that the serial speed is different in the above two examples, but I've tried 9600 and 115200 in BOTH and it seems to make no difference.
In the second code example, when I run the actuator once for maybe a half second, I see a few "detect" words spit to the serial terminal. If I run it a second time, I may or may not get any. And after that, nothing. No output at all.
What could be going on here? Is this interrupting too fast to reliably work with serial output since both use interrupts? Or am I missing something else?
--Donnie