Sketch won't count input from IR sensor

This is my first post and sorry, but I haven't a clue how to preformat the text. An example would be nice.

I'm trying to time the revolutions of a fan blade. I'm using an IR sensor with Nano, and the beam is broken by the blades as the fan rotates. The IR sensor board shows it is working, but I get no count increment. What's going on. It looks right to me. Sketch is below:

Thanks in advance for any help.

int startTime;
int irSense = 3;
int elapsedTime = 0;
int Count = 0;
int Sense;

void setup() {
pinMode(irSense, INPUT);
startTime = millis();
Serial.begin(9600);
}

void loop() {
Sense = digitalRead(irSense);
if (Sense = 0) {
Count = Count + 1;
Serial.println (Count);
if (Count = 11) {
Count = 0;
elapsedTime = millis() - startTime;
Serial.println("Elapsed Time");
Serial.println(elapsedTime);
startTime = millis();
}
}
delay (500);
}

Oopsi

if (Sense == 0) {
. . .
if (Count == 11) {


Look for a change in state not from the current state.


int startTime;
. . .
int elapsedTime = 0;

unsigned long


What do you think this does in your sketch ?
delay (500);

From the if structure reference:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

If you set the compiler warnings in the IDE, File, Preferences to All, that mistake will display a warning.

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

Thanks much. Not sure what your question refers to: unsigned long or delay (500).

Thanks much. Didn't notice the formatting icon before.

Hi lapl,

welcome to the Arduino-Forum.

delay(500) does what its name says
delay 500 milliseconds
makes the microcontroller twirl his thumbs for 0,5 seconds UNable to execute any code.

This means you are missing a lot of pulses.

checking if signal IS low does not catch each pulse.
depending on how fast loop() is iterating and what frequency the pulses have you might miss pulses or increment too often.

Here is a modified code that uses state-CHANGE-detection

unsigned long startTime;
const byte irSense_Pin = 5;
unsigned long elapsedTime = 0;
unsigned long Count = 0;
byte Sense;
byte lastSense;

void setup() {
  pinMode(irSense_Pin, INPUT);
  startTime = millis();
  Serial.begin(115200); // 9600 is too slow
}

void loop() {
  Sense = digitalRead(irSense_Pin);

  if (lastSense != Sense) {  // check if a state-CHANGE occured
    lastSense = Sense;       // then update lastSense
    if (Sense == LOW) {      // check if it is a falling-edge change from HIGH-to-low
      Count = Count + 1;
      //Serial.println (Count);
    }
  }

  if (Count == 1000) { // comparising uses DOUBLE-equationsign "=="
    Count = 0;
    elapsedTime = millis() - startTime;
    startTime = millis();
    Serial.print("Elapsed Time ");
    Serial.print(elapsedTime);
    Serial.println(" milliseconds");
  }
  //delay (500);
}

Measuring RPM is one of the rather rare cases where using an interrupt makes sense.
The part of the program that is executed when a state-change occurs just increments a variable by 1

There are a lot of demo-codes on the internet. Most of them that use an interrupt do it improperly. (no volatile variable declaration, using delay() etc.)

Paul Stoffregen has written a library for it. This library can be installed with the library-manager.

Here is a modified version of a demo-code which has additional comments and prints out more info to the serial monitor for easier understanding

/* FreqCount - Example with serial output
 * http://www.pjrc.com/teensy/td_libs_FreqCount.html
 *
 * This example code is in the public domain.
 */

On Arduino-Uno connect input-signal to IO-pin 5
#include <FreqCount.h>

const int measuringInterval = 500; 
float rpm;

void setup() {
  Serial.begin(115200); // make sure to adjust the baudrate in the serial monitor
  FreqCount.begin(measuringInterval); // start counting pulses. 
}

void loop() {
  if (FreqCount.available()) { // available becomes true once every measuringInterval 
    unsigned long count = FreqCount.read();
    Serial.print("counted ");
    Serial.print(count);
    Serial.print(" pulses in ");
    Serial.print(measuringInterval);
    Serial.println(" milliseconds");

    // 1000 / measuringInterval calulates number 
    // of pulses per 1000 milliseconds = 1 second
    // * 60 = pulses per minute = rpm
    // a float needs at least one variable or constant that is a float 
    rpm = count * (60.0 * 1000.0) / measuringInterval;
    Serial.print("rpm = ");
    Serial.print(rpm);
    Serial.print(" rev/min");    
  }
}

best regards Stefan

I can easily mix == and =, so I use my own words;

I call this "same same?" (or "same as?" or "are are?" because it is my smartphone alt-r)

I call this "becomes" as in "x becomes 10"

Thanks Stefan,
After running the sketch I realized I needed something to track the sensor change from 0 to 1.
This whole exercise has been to design an anemometer. I was taking a step at a time. Your last example code is what should work for me. I can then add some code to calculate mph.

Also 115200 baud rate just gives garbage output.
Thanks again.

this happends if you do not adjust the baudrate of the serial monitor.
There is a dropdown-menu at the bottom of the serial monitor-window that shows the chosen baudrate
grafik

As stated delay( xxx ) stops normal program execution while in this period.

As a result, nothing else can be serviced, hell has frozen :wink: .


Change these to unsigned long

unsigned long startTime;
. . .
unsigned long elapsedTime = 0;


Correct the = vs == mistakes.


Do you know the difference between change in state vs current state level ?

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