Hi, firstly, I hope I've got this in the right category, please advise where to put it if not! I'm fairly new to the forum, although have spent a fair bit of time lurking, so excuse any faux pas.
As a bit of background, I'm working on a project making a speedometer to retrofit into my 1970's car, with the eventual plan of having lots of exciting functionality like rpm, mpg, engine temp, remote oil level etc.
For the first stage, I'm measuring speed by triggering a reed switch using magnet attached to the transmission brake drum (180 degrees opposite each other to increase resolution//help balance). I want to trigger an external interrupt when the pin goes high, as it would save missing passes of the magnets, and reduces the amount the processor has to do. (previously I've just looked at the status in the main loop, which worked after a fashion but seemed a bit cack handed).
Annoyingly I can't get the interrupt to work properly. I thought I'd try a simple example initially, so have been using the code below, which is intended to switch on and off an LED when the switch in the circuit changes:
int pbIn = 0; // Interrupt 0 is on DIGITAL PIN 2!
int ledOut = 4; // The output LED pin
volatile int state = LOW; // The input state togg
void setup()
{
Serial.begin(9600);
// Set up the digital pin 2 to an Interrupt and Pin 4 to an Output
pinMode(ledOut, OUTPUT);
pinMode(pbIn, INPUT); // set pin to input
digitalWrite(pbIn, HIGH); // turn on pullup resistors
//Attach the interrupt to the input pin and monitor for ANY Change
attachInterrupt(pbIn, stateChange, CHANGE);
}
void loop()
{
Serial.println(state);
//Simulate a long running process or complex task
for (int i = 0; i < 100; i++)
{
// do nothing but waste some time
delay(10);
}
}
with LED + resistor between D4 and GND, and 'switch' (touching wires together) between +5V and D2.
So in theory when interrupt pin 0 changes from low to high or vice versa it should trigger. In reality, the change has no effect, and it seems to trigger all by itself. I've had a search of forums but not had much luck. I've set the internal pull resistor for the interrupt pin high, but this has had no effect, could anyone advise me as to what I might be doing wrong?
Thanks in anticipation
Jake
(using a sainsmart mega 2560 if it's of any relevance)