Hello there. I am building a dynamometer. I am using IR sensor for measuring rpm and HX711 load cell to measure the weight. Indivisually both of them are working great, but when i combine the code the IR sensor starts malfunctioning. It is not giving accurate rpm values. I have seen people posting similar problem in the forum before but i couldnt get proper solution. So if you have any way to fix the code, do help me.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Arduino.h>
#include "HX711.h"
LiquidCrystal_I2C lcd(0x27,20,4); //defining lcd pins
const int dataIN = 2; //IR sensor INPUT
unsigned long prevmillis = 0; // To store time
unsigned long duration; // To store time difference
unsigned long lcdrefresh; // To store time for lcd to refresh
unsigned long
int rpm; // RPM value
bool currentstate ; // Current state of IR input scan
bool prevstate = LOW; // State of IR sensor in previous scan
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 8;
const int LOADCELL_SCK_PIN = 9;
HX711 scale;
void setup()
{
Serial.begin(57600);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(-143.15); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare();
pinMode(dataIN,INPUT);
lcd.init();
lcd.backlight();
}
void loop()
{
// RPM Measurement
currentstate = digitalRead(dataIN); // Read IR sensor state
if( prevstate != currentstate) // If there is change in input
{
if( currentstate == HIGH ) // If input only changes from LOW to HIGH
{
duration = ( micros() - prevmillis ); // Time difference between revolution in microsecond
rpm = (60000000/duration); // rpm = (1/ time millis)*1000*1000*60;
prevmillis = micros(); // store time for next revolution calculation
}
}
prevstate = currentstate; // store this scan (prev scan) data for next scan
// LCD Display
if( ( millis()-lcdrefresh ) >= 100 )
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Speed of Motor");
lcd.setCursor(0,1);
lcd.print("RPM = ");
lcd.print(rpm);
Serial.print("Rpm: ");
Serial.println(rpm);
lcdrefresh = millis();
}
lcd.setCursor(1,2);
lcd.print("Weight: ");
Serial.print("Weight");
Serial.println(scale.get_units());
lcd.setCursor(8,3);
lcd.print(scale.get_units(),1);
}