Hi to the members of the group! I am trying to use interrupts to count the rpm of my small car robot with esp 32 to the multi tasking program. I took readings from both two wheels of the smalll robot car with optocoupler sensor.The goal is every second to have new speed data and to keep distance value.I write this code and seems to work fine but now without doing nothing crash. Please help me to find the errors. CODE:
//#include <LiquidCrystal_I2C.h> FOR NODE32S MCU
//LiquidCrystal_I2C lcd(0x27,16,2);
#define PIN_D36 36
#define PIN_D39 39
unsigned int interruptPin1 = PIN_D36;
unsigned int interruptPin2 = PIN_D39;
unsigned long start_time; // Δημιούργησε μια μεταβλητή τύπου unsigned long integer με όνομα start_time
unsigned int counter1,circles1 = 0;
unsigned int counter2,circles2 = 0;
// Float for number of slots in encoder disk
float diskslots = 40; // Change to match value of encoder disk
float Sdistance=0;
volatile float rotation1;
volatile float rotation2;
volatile float speed1;
volatile float speed2;
//void LCD1602_init(void)
//{
//lcd.init();
//delay(10);
//lcd.backlight();
//lcd.clear();
//}
hw_timer_t * timer = NULL;
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR handleInterrupt1() {
portENTER_CRITICAL_ISR(&mux);
counter1++;
circles1++;
portEXIT_CRITICAL_ISR(&mux);
}
void IRAM_ATTR handleInterrupt2() {
portENTER_CRITICAL_ISR(&mux);
counter2++;
circles2++;
portEXIT_CRITICAL_ISR(&mux);
}
void IRAM_ATTR count() {
timerAlarmDisable(timer);
rotation1 = (counter1 / diskslots) * 60.00; // calculate RPM for Motor 1
counter1 = 0; // reset counter to zero
rotation2 = (counter2 / diskslots) * 60.00; // calculate RPM for Motor 2
counter2 = 0; // reset counter to zero
Sdistance=(((circles1+circles2)/2) / diskslots)*0.208; //0.208=π*δ , δ=diameter=2*radius
speed1 = 2* rotation1 * 0.208; //*2 because of 0,5secinterrupt
speed2 = 2* rotation2 * 0.208; //radius=0.0333m
timerAlarmEnable(timer);
}
#define RXD2 16
#define TXD2 17
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
Serial.println("Monitoring interrupts: ");
pinMode(interruptPin1, INPUT);
pinMode(interruptPin2, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin1), handleInterrupt1, CHANGE);
attachInterrupt(digitalPinToInterrupt(interruptPin2), handleInterrupt2, CHANGE);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &count, true);
timerAlarmWrite(timer, 500000, true);
timerAlarmEnable(timer);
//LCD1602_init();
//lcd.setCursor(0, 0);
//lcd.print(" Wait Signal ");
}
void loop() {
//start_time=millis();
//if (millis()-start_time>1000UL)
// {
Serial2.print(speed2);
Serial2.print(",");
Serial2.print(speed1);
Serial2.print(",");
Serial2.println(Sdistance);
//}
delay(500);
}