0% found this document useful (0 votes)
11 views2 pages

RPM 2

Uploaded by

omareldahry03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

RPM 2

Uploaded by

omareldahry03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// Constants

const int sensorPin = 2; // IR sensor connected to digital pin 2

// Variables

volatile int rpmCount = 0;

unsigned long startTime;

volatile boolean rpmFlag = false;

// Interrupt service routine for counting RPM

void rpmIncrement() {

rpmCount++;

void setup() {

// Attach interrupt to the IR sensor pin

attachInterrupt(digitalPinToInterrupt(sensorPin), rpmIncrement, RISING);

// Start time

startTime = millis();

// Initialize the serial communication

Serial.begin(9600);

void loop() {

// Calculate RPM every second


if (millis() - startTime >= 1000) {

detachInterrupt(digitalPinToInterrupt(sensorPin));

// Calculate RPM value

float rpm = (rpmCount / 2.0) * 60.0;

// Print RPM value to the serial monitor

Serial.print("RPM: ");

Serial.println(rpm);

// Reset variables

rpmCount = 0;

startTime = millis();

attachInterrupt(digitalPinToInterrupt(sensorPin), rpmIncrement, RISING);

You might also like