Hi guys, I am learning to make a temperature and rpm monitoring project on an electric motor with esp32, and the program code is all done separately. but when the code is combined, the rpm sensor doesn't work or 0 while the temperature is working. Please help me guys. sorry my english is bad. Thanks
#include <Wire.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27 ,2,1,0,4,5,6,7,3, POSITIVE);
int encoder_pin = 2; // pulse output from the module
int rpm1;
int rpm2;
unsigned int rpm; // rpm reading
volatile byte pulses; // number of pulses
unsigned long timeold;
// number of pulses per revolution
// based on your encoder disc
unsigned int pulsesperturn = 12;
#define DHTPIN 4
byte Simbol_derajat = B11011111;
//our sensor is DHT22 type
#define DHTTYPE DHT22
//create an instance of DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void counter()
{
//Update count
pulses++;
}
void setup()
{
Serial.begin(9600);
lcd.begin(16,2);
dht.begin();
pinMode(encoder_pin, INPUT);
//Interrupt 0 is digital pin 2
//Triggers on Falling Edge (change from HIGH to LOW)
attachInterrupt(2, counter, RISING);
// Initialize
pulses = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
//use the functions which are supplied by library.
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
delay(2500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Thermometer ");
lcd.setCursor(0,1);
lcd.print("Suhu: ");
lcd.setCursor(12, 1);
lcd.write(Simbol_derajat);
lcd.setCursor(6,1);
lcd.print(t,0);
lcd.setCursor(13,1);
lcd.print("C");
lcd.print(" ");
if (millis() - timeold >= 1000) {
//Don't process interrupts during calculations
detachInterrupt(2);
rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
timeold = millis();
pulses = 0;
rpm1 = rpm*6.5;
delay(2500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Tachometer ");
lcd.setCursor(0,1);
lcd.print("Speed: ");
lcd.setCursor(6,1);
lcd.print(rpm1);
lcd.setCursor(13,1);
lcd.print("RPM");
lcd.print(" ");
attachInterrupt(2, counter, FALLING);
//Restart the interrupt processing
}
}
