trouble getting analog sensor to trigger interrupt

Hi all,

My circuit consists of a hall effect sensor and 16 LED's with associated bitshift chips. Everything seems to be working fine hardware wise and i've run a sketch that lights different amounts of LED's depending on a magnets distance from the sensor by using the analogRead method.

I am now trying to count the passes of a magnet past the sensor. My research seems to say that using an interrupt is the best course of action. As such i've connected my sensor to the digital 2 pin (interrupt 0) but its here that i've run into problems. I've set the interrupt to register a change in this pin and to increment a variable value and display the corresponding number of LED's but it isn't working as planned.

On switching the circuit on it lights 1 LED with no magnets anywhere near it (it shouldn't light any) and wont respond when i bring magnet closer (which should increment the RPMcount).

Below is the pertinent part of my code:

volatile byte RPMcount;

//Pin connected to ST_CP of 74HC595 int latchPin = 8; //Pin connected to SH_CP of 74HC595 int clockPin = 12; ////Pin connected to DS of 74HC595 int dataPin = 11;

//holders for infromation you're going to pass to shifting function byte dataRED; byte dataGREEN; byte dataArrayRED[10]; byte dataArrayGREEN[10];

void setup() { pinMode(latchPin, OUTPUT); pinMode(2, INPUT); // Make digital 2 an input digitalWrite(2, HIGH); // Enable pull up resistor attachInterrupt(0, RPMfun, CHANGE); RPMcount = 0; }

void RPMfun() { RPMcount++; //Each rotation, this interrupt function is run twice }

void loop() {

if (RPMcount=0) { digitalWrite(latchPin, 0); shiftOut(dataPin, clockPin, 0); shiftOut(dataPin, clockPin, 0); digitalWrite(latchPin, 1); } else if (RPMcount=1) { digitalWrite(latchPin, 0); shiftOut(dataPin, clockPin, 0); shiftOut(dataPin, clockPin, 1); digitalWrite(latchPin, 1); } else if (RPMcount=2) { digitalWrite(latchPin, 0); shiftOut(dataPin, clockPin, 0); shiftOut(dataPin, clockPin, 3); digitalWrite(latchPin, 1);

...etc etc including the working shiftOut routine ...

Any help will be greatly appreciated as i'm not sure what i'm doing wrong.

Many thanks,

Stuart

Can you post your circuit?

It may be something as simple as a missing pull-up.

I'd be inclined to simplify your sketch to just the bare-bones of getting your interrupt working, perhaps blinking the on-board LED on pin 13 to show the hall-effect working. First try it in a loop, then try attaching the pin to an interrupt.

I'm afraid my iPhone camera is a bit rubbish at close up photos so i'm not sure that any photo of my circuit would be that clear!

I don't have a pull up resistor attached to the interrupt pin, i thought that

digitalWrite(2, HIGH); // Enable pull up resistor

did that?

if not then what size of resistor should i put between my interrupt pin and the +5v?

My hall effect sensor varies between about 400 and 700 when i'm reading the analogRead method if that helps at all.

Thanks so much for your help, i'm very new to this but loving every minute!

Stuart

I didn't ponder your problem fully on the hope that the simple change from:

if (RPMcount=0) {

to

if (RPMcount==0) {

(etc.) will make everything better ;)

I don't have a pull up resistor attached to the interrupt pin, i thought that

Oops, missed that. You're quite right.

But, yes, what the Rugged One said!

(Hint: if writing a comparison-with-constant, always write the constant on the left-hand side. That way, if you fail to write "==" instead of "=", the compiler will catch it for you: e.g. "if (3 = rpmCount)" will fail to compile, whereas "if (rpmCount = 3)" won't fail to compile, and will always be true. Sadly, if comparing two variables, it's all down to you to get it right)

Ahh! Now it works fine. That’s brilliant, thanks for spotting that guys.

Stu