//MEEM1803 Assignment 1: Reading encoder RPM using push button
//Darren Tan (MEE221009), Jiang Haoyu (MEE221006)
//The push button is used to represent the encoder reading
#define button1 27 //Define push button as digital pin 27
int counter1;
int speed1;
const long interval = 1000;
const long interval1 = 100;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long previousMillis1 = 0;
void setup()
{
Serial.begin(115200);
pinMode(button1, INPUT); //Set push button as input
attachInterrupt(button1, detect, RISING); //Detact the rising edge of push button
}
void detect()
{
// Counter the pulse and add to main counter
if (currentMillis-previousMillis1>=interval1)
{
counter1++;
previousMillis1 = currentMillis;
}
}
void loop()
{
currentMillis=millis();
if(currentMillis-previousMillis>=interval) //
{
// To calculate speed of wheel rotate
speed1 = counter1*60; //RPM = ((Total pules/Pulse per rotation)/sec) * 60
Serial.print(speed1); //Print the speed in Serial Monitor
Serial.println(" Rotation per Minute");
previousMillis = currentMillis;
counter1 = 0; // To default and intialize the counter
}
}