Pulse Counting

Hi,. I am trying to get the arduino to count the pulses it gets from an input. Its from a inductive sensor sitting on the back of a 900rpm motor. I wan't to use it as an encoder, to be able to stop the motor after X rotations. I have tried a lot of different things, and read posts for hours, but i just cant get it working. It just counts all pulses at an instance... I am using interrupt.

int pin = 13;
volatile int state = LOW;
volatile int count = 0;
int pin1 =9;

void setup()
{
  pinMode(pin, OUTPUT);
  pinMode(pin1,OUTPUT);
  attachInterrupt(0, puls, RISING); //Interrupt indgang 0 som er pin2, aktiver "blink" når værdien falder på pin2, altså fra + -> -, kan også være RISING eller CHANGE (skifter ved hver ændring) Eller LOW for at trigger den hver gang den blive lav.
}

void loop()
{
  if(count>=20){
  digitalWrite(pin, HIGH);}
  else{digitalWrite(pin, LOW);}
  digitalWrite(pin1,HIGH);
}

void puls()
{

  state ? count--:  count++;
}

I have the sensor connected via 24v on two resistors to break it down to 5v. 2.4K to GND, a 10K from sensor signal. The two resistors are joined at the input on the arduino.

Any suggestions?

Regards Nic

Well, ‘state’ never changes, so this:

state ? count--:  count++;

is effectively this:

count++;

As well, once you hit 20, ‘pin’ is constantly set HIGH - is that what you want?

void loop()
{
  if(count>=20)
  {
    digitalWrite(pin, HIGH);
  }
  else
  {
    digitalWrite(pin, LOW);
  }
  digitalWrite(pin1,HIGH);
}

Maybe you want to kill the motor at 20:

  if(count>=20)
  {
    // kill motor somehow
    while (1) { } // and stay here forever
  }

Or just continue counting:

  if(count>=20)
  {
    digitalWrite(pin, HIGH);
    count=0; // start again
  }

Also - what does ‘pin1’ do, since you’re constantly setting it high?

How are 'pin' and 'pin1' supposed to behave, before and after the counter reaches 20?

Also, it's always a good idea to set the initial states of output pins in setup(), right after the pinMode() calls:

void setup()
{
  pinMode(pin, OUTPUT);
  pinMode(pin1,OUTPUT);
  digitalWrite(pin, ....);
  digitalWrite(pin1, ....);
  ....

Thanks for the reply.

Pin is just setting the led on pin 13 (onboard led).

Pin1 is my motor, which i wan't to stop at the count of 20.

But the big problem is that when i activate the sensor once it counts to 20, it doen't just give me one pulse. I wanted it to count +1 every time sensor goes from low to high or the other way around. The 10K resistor to GND should make sure the sensor doesn't flicker. Should i set a 1uf capacitor betwen the input and GND also? or is it my programming that is wrong??

Regards Nic

int pin = 13;
volatile int state = LOW;
volatile int count = 0;
int pin1 =9;

void setup()
{
  pinMode(pin, OUTPUT);
  pinMode(pin1,OUTPUT);
  attachInterrupt(0, puls, RISING); //Interrupt indgang 0 som er pin2, aktiver "blink" når værdien falder på pin2, altså fra + -> -, kan også være RISING eller CHANGE (skifter ved hver ændring) Eller LOW for at trigger den hver gang den blive lav.
}

void loop()
{
  if(count<=200){
  digitalWrite(pin1, HIGH);}
  else{digitalWrite(pin1, LOW);}
  digitalWrite(pin,HIGH);
}

void puls()
{

  state ? count--:  count++;
}
[/code

Damn it. It was just the 1uF capacitor on the input as i suggested.. The programmin is good, it works now.. 

But thank you so much for the reply!

Regards NIC

Damn it. It was just the 1uF capacitor on the input as i suggested.. The programmin is good, it works now..

But thank you so much for the reply!

Regards NIC

Good that you got it working. I was about to suggest that if the sensor is an analog one, outputting voltages in 0V ~ 24V, connecting it directly to a digital input may lead to quite an undetermined behavior. If it's a digital one, outputting just 0V or 24V, you should be fine.

It is a digital 0-24v. And i just have to resistors on its signal wire for breaking the 24 down to 5v. Now i just put a 1uF between GND and the input pin on the arduino, to take the flicker. And it looks like it is working... I am not all that in to circuits, but should i use a 10K resistor from the signal to the input and then a 1uF capacitor from input to GND or is it fine with just the capacitor?

I have seen the low-pass filter with a 10K and 1uF before, but it works with just the 1uF...

Regards Nic

It does not work with a resistor. It looses a lot of pulses. I'll stick with only the 1uF capacitor :)

There's more about debouncing switches over at all-electric.com. Hope that helps someone.

Note that you can also "debounce" in software by recording a "high" then not recording a transition to "low" until some milliseconds have passed. This can be done through various means such as recording millis when you receive the first transition, then ignoring transitions until a few milliseconds have passed.

But I'm wandering dangerously into territory I don't really know here. :zipper_mouth_face: