Arduino Mega Read a repeating Analog Signal and Count the instances

I am trying to read an analog signal. It shows a repeating pattern between 0-5 V. I just want to make the counts how many times it was 5 V.

My signal: I want to count the times it was 5 V, not the momentary spikes.

code:

// LED pin

int ledPin = 13;

float sig_Rain = 0; // Rain guage sensor variable

int tip_cnt = 0; // Count the tips

void setup ()

{

  Serial.begin(9600);   

  // Turn off onboard LED off

  // initialize the digital pin as an output.

  pinMode(ledPin, OUTPUT);

  digitalWrite(ledPin, LOW);   // turn the LED on (HIGH is the voltage level) 

   Serial.println("Rain_guage_signal");

}

void loop ()

{
  // Collect Raing guage sensor data

  sig_Rain = analogRead(sig_rain)*5.0/1024.0;

  // Print the raing guage 

  //Serial.print(sig_Rain,4);Serial.print(",");

  // Counter 

  if(sig_Rain<5.0){

    if(sig_Rain>4.9){

      if(sig_Rain<5.0){

        tip_cnt = tip_cnt+1;

      }

    }

  }

  Serial.print(tip_cnt);Serial.print(",");

  Serial.println();  

}

Present output:

Counter is zero. Looks like I am comparing present samples, but I have to compare previous samples. How do I do that?

Be very, very wary of comparing floating point values for equality.

...will never give 5V (but well-done for using the correct formula!)

So then, how many 5 volts in a row do you need to make it NOT a momentary spike?

Measure each HIGH duration (make a note of millis when you see a LOW to HIGH transition and when the reading goes to LOW calculate how long it stayed HIGH) and decide if it's a keep or not based on some threshold

Your suggestion seems to be made an impact. Now I edited the code and checking if it is >4.9, not =5.0. This is gives the following output:

                 Rain, Count
12:27:21.609 -> 4.9951,3559,

12:27:21.609 -> 4.9951,3560,

12:27:21.609 -> 4.9951,3561,

12:27:21.620 -> 4.9951,3562,

12:27:21.620 -> 4.9951,3563,

12:27:21.756 -> 4.9951,3564,

12:27:21.756 -> 4.9951,3565,

12:27:21.829 -> 4.8633,3565,

12:27:21.829 -> 4.0430,3565,

12:27:21.829 -> 3.3545,3565,

12:27:21.833 -> 3.2910,3565,

The problem is, it counting each instance. I want consider this whole duration as 1 count. The reason is, my sensor gives 5 V output when bucket is full, and it tips automatically by itself and next time it gives 5 V output when bucket is full again. This continues. I am counting here the number of times bucket tips.

Take a look at the state change detection example in the IDE, and see if you can adapt it to your needs

so basically it's like a momentary button that get pressed from time to time and bounces and you don't want to count the bounces

there are tons of libraries for buttons

1. Can you give an idea of 5V duration in ms, which will not be considered as a spike? For example: a signal between 4.5 V - 5V of > 1 ms duration would be considered as 5V and hence 1 count.

2. According to my above definition, there are 3 counts in the signal of Fig-1.
levSig
Figure-1:

3. Create a sketch to scan the signal of Fig-1 and show 3 on the Serial Monitor.

Thanks for referring it. I implemented it as it is with no changes. Looks like it stops at 1 because it compares only previous state. In my case, I may have to compare many samples before.

Nitpicking:

The second condition never can be true thus counter stays zero.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.