To use analogue reading instead of interrupts

I have two sensors that are switching from low to high when they detect. According to the analogue signal it works fine, but using a digital interrupt they count it wrong. My question is how to make a good counting function without interrupt. The sensors working freq. is 70 Hz.

Is it wise to do so (continues analogue reading)? Following code is an example when only using one sensor.

int analogPin = 0;  
int val = 0;        
int count = 0; 
int flag = 0; 

void setup()

{
  Serial.begin(9600);  
}

void loop()
{

  val = analogRead(analogPin);   
  if(val == 1023 && flag == 0){
    count = count + 1;
    flag = 1;
  }
   if(val == 0 && flag == 1){
     flag = 0;
   }
  Serial.println(count);   
}

Is this really a safe way to solve this problem? Or should I use two arduino, one for each sensor?

What problem? What does flag do?

You can do around 10,000 analogReads in one second, so I think doing 2 x 70 (140) should not be too big a problem.

Is it wise to do so (continues analogue reading)?

Certainly it is wise ... er, define "wise".

Thank you for your reply!

The problem is that the digital interrupt routine does't work. I really don't understand why. Flag is for making the count variable possible. The first input that is 1023 it will add 1 to count (detects about 4 samplings of 1023 when the object is passing by, depending on the velocity). When it goes back to low, flag will tell the system that the object has passed and makes it ready to count the next one.

With "wise" I mean if this is a good way to do it or if there are better...

Analog reads will not necessarily be 0 or 1023, it depends on noise and other factors.

If you are just trying to read "on" or "off" you are better off with a digitalRead.

What do you think is the problem then? Looking at the graph for one of the sensors, the analogue signal detects the signal very well (every pulse represent one object), but as soon as I change it to a simple digital interrupt routine, the logic calculate the number of object wrong, often to many. If you look at the output from the serial communication, the values is really good.

output.txt (7.31 KB)

graph.jpg

And this is the simple interrupt routine for only one sensor:

const int SensorPin1 = 2;

volatile int scount1=0;

void setup()
{
  Serial.begin(9600);
  pinMode(SensorPin1, INPUT_PULLUP);      // Pin 2 is input to which a sensor is connected = INT0
  attachInterrupt(0, count1, RISING);  //Interrupt when the object passing by first sensor
}

void loop() {
  Serial.println(scount1);
}  

void count1(){              // Interrupt service routine

  scount1 = scount1 + 1;    // Counting 

}

Don't do serial prints in an interrupt routine.

You are right, but that is not the problem (I even forgot to start the serial communication). The code is changed now. But I have also tried without serial communication, but with the same result.

What do you mean "it is changed"?

If you have new information please post it in a new post. Changing early posts is like rewriting history ... confusing.

Now my reply looks meaningless. Grrrr.

void count1(){              // Interrupt service routine
  noInterrupts();    // <----- not needed
  scount1 = scount1 + 1;    // Counting 
  interrupts();   // <----- not needed
}

http://www.gammon.com.au/interrupts

Your code works for me. What happens when you run it ? By the way, you don't need the noInterrupts()/interrupts() in there because they are automatically disabled during an ISR.

About the noInterrupts() and interrupts(), thank you!

If you look at analogue signal (input.txt or graph.jpg) you see how perfect it reads the sensor. But using interrupt for counting it counts to [u]many[/u] object... I really don't understand why. Any suggestion?

Are people afraid of downloading the attached files? Is there any other better way to show results?

Megaman80: The noInterrupts() and interrupts() it's from when I am using 2 interrupts, sorry for not removing that but thank you for notice!

That doesn't really explain. It doesn't matter how many interrupts you have, it's still pointless to disable them inside an interrupt, because they are automatically disabled by the processor.

Hi,

What is the application? What are the sensors, spec? What are they sensing? How often do they need to detect the object?

Tom..... :)

aarg: That doesn't really explain. It doesn't matter how many interrupts you have, it's still pointless to disable them inside an interrupt, because they are automatically disabled by the processor.

You are right and I realized that just before u posted ur reply! But thanx!

[quote author=Nick Gammon date=1431418179 link=msg=2228452] Now my reply looks meaningless. Grrrr. [/quote] Not if ppl are reading the whole topic. I don't want ppl to reply on the same thing, that's why I change the code...

TomGeorge: Hi,

What is the application? What are the sensors, spec? What are they sensing? How often do they need to detect the object?

Tom..... :)

The sensors are inductive and are detecting metal objects, The detecting range is 5 cm and the working freq. is 70 Hz. If you look at the attached input.txt and graph.jpg you see how often they are detecting. The input.txt is a copy from the serial monitor. So the miscalculation should not be because of the sensors.

Megaman80: Are people afraid of downloading the attached files? Is there any other better way to show results?

I'm not afraid. I downloaded one. I am still alive.

Hi.

working freq. is 70 Hz

When the sensor detects a piece of metal does it output a HIGH until the metal as passed, or 70Hz pulses until the metal has passed?

Tom.... :)