This is the code I currently have.
This code can currently track the number of revolutions made by the motor shaft. When I implement the pulses = o after the delay in the loop everything just seems to become zero. I need a way to get rpm or rps.
#include <Wire.h> //Libraries for i2c communication
#include <LiquidCrystal_I2C.h> //Libraries for LCD
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// The pins the quadrature encoder is connected.
// We connect the second signal of the encoder to pin 4.
// int.1 is free for other use.
int in_a = 2;
int in_b = 4;
long rpm = 0;
// The number of pulses per revolution
// depends on your index disc!!
unsigned int pulsesperturn = 300;
// The total number of revolutions
long revolutions = 0;
long c = 1;
// Initialize the counter
volatile long pulses = 0;
void count() {
// This function is called by the interrupt
// If in_b is HIGH increment the counter
// otherwise decrement it
if (digitalRead(in_b)) {
pulses++;
}
else {
pulses--;
}
}
void setup() {
pinMode(in_a, INPUT);
Serial.begin(9600); //Starts serial communication at 9600 bps
pinMode(in_b, INPUT);
attachInterrupt(0, count, RISING);
lcd.init();// initialize the lcd
lcd.backlight(); //turn on the backlight
}
void loop() {
revolutions = pulses/pulsesperturn;
rpm = revolutions;
Serial.println(pulses);
Serial.println(revolutions);
lcd.setCursor(0,0); //Sets line 1 cursor position
lcd.print("RPM"); //Displays line 1 text
lcd.setCursor(1,1); //Sets line 2 cursor postion
String line = String(rpm);
lcd.print(line+ " "); //Display line 2 text
delay(1000);
//pulses =0;
}