HELP: NE555 and interrupt function

Hello everyone,

I am a mechanical engineering student, and recently discovered arduino. You can not imagine, how happy I was, that I will not have to buy commercial products to do simple tasks, but actually do it myself, the way I want it to perform.

I watched and read a lot of tutorials, and had no problems monitoring sensors and doing simple tasks with servos.

BUT not I am stuck.

I try to monitor a single cylinder 4 stroke IC motor and strugling to register RPM with ne 555 and interrupt function.

The schematic is attached to the post. It is based on a very popular in this forum sport devices capacitive spark pickup (found here SportDevices. Chassis and Engine Dynamometers)

The code I am using is very simple, which sends the data back to the seial port.

int sensePin = 2;
int rpm = 0;
unsigned long oldTime = 0;
unsigned long newTime = 0;
unsigned long interval = 0;

void spark()
{
  newTime = micros();
  interval = newTime - oldTime;
  if (interval > 500)
  {
    rpm = 60000000 / interval; 
  }
}

void setup ()
{
  Serial.begin(9600);
  pinMode(sensePin, INPUT);
  attachInterrupt(0, spark, RISING);    // analog2 input 
}

void loop ()
{  
  noInterrupts();
  unsigned long rpmDisplay = rpm;
  interrupts();
  Serial.println(rpmDisplay);
  delay(1000);
}

and I constantly get zero values back to com monitor :frowning:

I tried FALLING and RISING, tried different interval comparison, even to the 1 microsecond and still getting zeroes.

There are some slight variations of NE555 wiring posted over internet, so I think I might have done something wrong with wiring the chip NE555.

Also, is there any arduino based osciloscope I could try using, to see if there is any signal coming to arduino. I tried googlig, but xciloscope does not work for me, due to being old version and SimpleArduinoOsciloscope freezes after a few seconds :frowning:

Thank ou for al lthe input.

You have a few problems:

You are using "pin 2" for reading but your schematic shows your input hooked up to analog pin 2. That is not the same as "D2", or digital pin 2. Move your input from A2 to D2.

Second, nowhere in your code do you write to the oldTime variable. You must say something like:

oldTime = newTime;
newTime = micros();

-- The Rugged Audio Shield: Line In, Mic In, Headphone Out, microSD socket, potentiometer, play/record WAV files

The Rugged Audio Shield: Line In, Mic In, Headphone Out, microSD socket, potentiometer, play/record WAV files

"rpm" really should be qualified "volatile", though it's probably OK with your code as it is now.

RuggedCircuits: You have a few problems:

You are using "pin 2" for reading but your schematic shows your input hooked up to analog pin 2. That is not the same as "D2", or digital pin 2. Move your input from A2 to D2.

Second, nowhere in your code do you write to the oldTime variable. You must say something like:

oldTime = newTime;
newTime = micros();

thank you very much! for some reason I thought that the interrupt uses analog pins 2 and 3. and the oldTime assignment to the newTime had in writen algorithm on paper, but forgot to copy it :doh:

now the rpm meter works!

AWOL: "rpm" really should be qualified "volatile", though it's probably OK with your code as it is now.

thanks for the input.

I do not really grasp the volatile. I understand that it uses different memory. but how tu use it?

do i just write in definition:

volatile int rpm = 0;

thanks.

There are some slight variations of NE555 wiring posted over internet, so I think I might have done something wrong with wiring the chip NE555.

When you boil it all down there really are only two basic 555 configurations: (1) Astable - typically has a resistor between pins 6 and 7 (2) Monotable - typically has pins 6 and 7 tied together

I use a common configuration when I draw my 555 circuits and that makes the similarities (and differences) easily discernible. My configuration is very similar to yours except I have two connections on each side of the box. I draw pin 5 on the right, under pin 3 and I put pins 2 and 5 on the bottom, with pin 2 on the left and pin 5 on the right.

Your 555 circuit looks OK except you should probably tie or pull pin 4 (reset) high and you should put a small capacitor (0.01uF) between pin 5 and ground.

Don

I do not really grasp the volatile. I understand that it uses different memory. but how tu use it?

No, it doesn't use different memory. Instead, it forces the compiler to generate code to read the memory (or register) whenever it is referenced, otherwise, the compiler could simply "optimise-out" the reference.

Imagine you had some code in a loop in "loop", inspecting a variable that is changed in an interrupt.

while (globalVariable == 0) {
  // do nothing, wait for "globalVariable" to be non-zero
}

Here, imagine "globalVariable" is a global variable in-scope, declared simply "int globalVariable;"

In the code above, there's nothing in the body of the while loop that changes "globalVariable", so the compiler could, legitimately, interpret the "while" as an "if", simply examining the value of "globalVariable" just once, and making a yes/no decision just once. The volatile qualifier forces the compiler to form a proper loop, because the variable could be changed by an outside influence, like an interrupt.

Thank you AWOL.

your explanation is so much clearer than arduino reference here

http://arduino.cc/en/Reference/Volatile

:)

this is a great forum. tried to solve my problem a whole sunday. An hour in the forum, and I learned more, than the whole day tyring to sort it myself :)