ESP32TimerInterrupt RPM Sensor Code

Hello friends :)

I'm trying to get some programming skills. I am beginning to understand what is happening in the code, but I do not understand the ejection ... It's about this code, which is below. I only connected the naked Esp32 v4 and just wanted to test the code.

When I start the serial monitor, it throws me out completely: ⸮⸮ 5⸮⸮⸮ 5⸮⸮⸮⸮⸮i It goes on and on. Why and what does that mean?

Shouldn't that actually appear as a result ?: Serial.println ("Can't set ITimer0. Select another freq. Or timer");

The other question is ... where can I find the result so that I can use it further? here? -> TimerHandler0 ()

 RPM_Measure.ino
   For ESP32 boards
   Written by Khoi Hoang
   Built by Khoi Hoang https://github.com/khoih-prog/ESP32TimerInterrupt
   Licensed under MIT license
   The ESP32 has two timer groups, each one with two general purpose hardware timers. All the timers are based on 64 bits
   counters and 16 bit prescalers. The timer counters can be configured to count up or down and support automatic reload
   and software reload. They can also generate alarms when they reach a specific value, defined by the software. The value
   of the counter can be read by the software program.
   Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
   unsigned long miliseconds), you just consume only one ESP32 timer and avoid conflicting with other cores' tasks.
   The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
   Therefore, their executions are not blocked by bad-behaving functions / tasks.
   This important feature is absolutely necessary for mission-critical tasks.
   Based on SimpleTimer - A timer library for Arduino.
   Author: mromani@ottotecnica.com
   Copyright (c) 2010 OTTOTECNICA Italy
   Based on BlynkTimer.h
   Author: Volodymyr Shymanskyy
   Version: 1.1.0
   Version Modified By   Date      Comments
   ------- -----------  ---------- -----------
    1.0.0   K Hoang      23/11/2019 Initial coding
    1.0.1   K Hoang      27/11/2019 No v1.0.1. Bump up to 1.0.2 to match ESP8266_ISR_TimerInterupt library
    1.0.2   K.Hoang      03/12/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
    1.0.3   K.Hoang      17/05/2020 Restructure code. Add examples. Enhance README.
    1.1.0   K.Hoang      27/10/2020 Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
*****************************************************************************************************************************/
/*
   Notes:
   Special design is necessary to share data between interrupt code and the rest of your program.
   Variables usually need to be "volatile" types. Volatile tells the compiler to avoid optimizations that assume
   variable can not spontaneously change. Because your function may change variables while your program is using them,
   the compiler needs this hint. But volatile alone is often not enough.
   When accessing shared variables, usually interrupts must be disabled. Even with volatile,
   if the interrupt changes a multi-byte variable between a sequence of instructions, it can be read incorrectly.
   If your data is multiple variables, such as an array and a count, usually interrupts need to be disabled
   or the entire sequence of your code which accesses the data.
   RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
   then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
   Asssuming LOW is active.
   For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
   If the time between active state is less than 8ms => consider noise.
   RPM = 60000 / (rotation time in ms)
   We use interrupt to detect whenever the SW is active, set a flag then use timer to count the time between active state
   RPM Measuring uses high frequency hardware timer 1Hz == 1ms) to measure the time from of one rotation, in ms
   then convert to RPM. One rotation is detected by reading the state of a magnetic REED SW or IR LED Sensor
   Asssuming LOW is active.
   For example: Max speed is 600RPM => 10 RPS => minimum 100ms a rotation. We'll use 80ms for debouncing
   If the time between active state is less than 8ms => consider noise.
   RPM = 60000 / (rotation time in ms)
   You can also use interrupt to detect whenever the SW is active, set a flag then use timer to count the time between active state
*/
#ifndef ESP32
  #error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
#endif

// These define's must be placed at the beginning before #include "ESP32TimerInterrupt.h"
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG    1

#include "ESP32TimerInterrupt.h"

#define PIN_D23                   23        // Pin D23 mapped to pin GPIO23/VSPI_MOSI of ESP32

unsigned int SWPin = PIN_D23;

#define TIMER0_INTERVAL_MS        1
#define DEBOUNCING_INTERVAL_MS    80

#define LOCAL_DEBUG               1

// Init ESP32 timer 0
ESP32Timer ITimer0(0);

volatile unsigned long rotationTime = 0;
float RPM       = 0.00;
float avgRPM    = 0.00;

volatile int debounceCounter;

void IRAM_ATTR TimerHandler0()
{
  static bool started = false;

  if (!started)
  {
    started = true;
    pinMode(SWPin, INPUT_PULLUP);
  }

  if ( !digitalRead(SWPin) && (debounceCounter >= DEBOUNCING_INTERVAL_MS / TIMER0_INTERVAL_MS ) )
  {
    //min time between pulses has passed
    RPM = (float) ( 60000.0f / ( rotationTime * TIMER0_INTERVAL_MS ) );

    avgRPM = ( 2 * avgRPM + RPM) / 3,

    Serial.println("RPM = " + String(avgRPM) + ", rotationTime ms = " + String(rotationTime * TIMER0_INTERVAL_MS) );

    rotationTime = 0;
    debounceCounter = 0;
  }
  else
  {
    debounceCounter++;
  }

  if (rotationTime >= 5000)
  {
    // If idle, set RPM to 0, don't increase rotationTime
    RPM = 0;
    Serial.println("RPM = " + String(RPM) + ", rotationTime = " + String(rotationTime) );
    rotationTime = 0;
  }
  else
  {
    rotationTime++;
  }
}

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  delay(100);
  
  Serial.println("\nStarting RPM_Measure on " + String(ARDUINO_BOARD));

  // Using ESP32  => 80 / 160 / 240MHz CPU clock ,
  // For 64-bit timer counter
  // For 16-bit timer prescaler up to 1024

  // Interval in microsecs
  if (ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0))
    Serial.println("Starting  ITimer0 OK, millis() = " + String(millis()));
  else
    Serial.println("Can't set ITimer0. Select another freq. or timer");

  Serial.flush();   
}

void loop()
{

}

Thank you and stay Healthy!

https://github.com/khoih-prog/ESP32TimerInterrupt/blob/master/examples/RPM_Measure/RPM_Measure.ino

www.google.de/search?lr=&newwindow=1&as_qdr=all&ei=x2G5X8DcIqPMgwfGhYrgAQ&q=arduino+using+interrupt+service+routines

www.google.de/search?lr=&newwindow=1&as_qdr=all&ei=fGK5X7nIG_yijLsP0YSLyAE&q=arduino+measuring+frequency+with+interrupt+service+routine

www.google.de/search?lr=&newwindow=1&as_qdr=all&ei=2mK5X4uIK6CDjLsPkaGWeA&q=serial+print+in+interrupt+service+routine

www.google.de/search?lr=&newwindow=1&as_qdr=all&ei=EWO5X6DYNoi8UobmuMAP&q=arduino+isr+volatile&oq=arduino+isr+volatile

Changing the "float" to "int" did not change the output.

the problem is www.google.de/search?lr=&newwindow=1&as_qdr=all&ei=2mK5X4uIK6CDjLsPkaGWeA&q=serial+print+in+interrupt+service+routine

This is an example why

always just looking for code that looks similar to what you want to do

without learning the basics thoroughly will be more time-consuming than learning the basics thoroughly

But I guess some people just find it too boring to learn basics. Some people seem to prefer to be "in action" by wild guessings and testing this and testing that

Electronics and programming are new to me. I've learned a lot for being there for 3 weeks. Nobody says I don't want to learn! They talk a lot, maybe too much. It's about the code, I want to understand why such an error occurs. The code was uploaded to Gitbub and here in the forum too. So I want to know if this code is buggy and what the reason is. Without an explanation, things get a little difficult. I can also use Google myself, and I've already read the link. If you are that experienced, instead of throwing smart words around, explain it to me. Not meant badly, but that annoys me in a forum. All super intelligent, but none can provide a reasonable answer.

If you are that experienced, instead of throwing smart words around, explain it to me. Not meant badly, but that annoys me in a forum. All super intelligent, but none can provide a reasonable answer.

my posting style reflects on your behaviour in the other thread.

"Oh thank you all I found another code (I can copy and paste) which works for me.

This is the opposite of throroughly learning. This is quick jumping forward. So I post quick jumping forward postings.

the first line of this link says it all.

http://www.ee.nmt.edu/~rison/ee308_spr99/supp/990311/isr_print.html

and in the middle of the webiste the very long comment line says how it should be done.

Same thing on this website first answer says it all https://www.reddit.com/r/arduino/comments/6gyl41/using_serialprintln_inside_an_isr/

a real friendly and cooperative style of posting would be to post a link to the code you are using

and describing in detail what you have done

and do some more googling
https://www.google.de/search?lr=&newwindow=1&as_qdr=all&ei=wpe6X-SnIqvlgwf72KDABQ&q=arduino+serial+monitor+shows+garbage&oq=arduino++serial+monitor+shows

you might say: How should I know that this is the reason for showing ⸮⸮ 5⸮⸮⸮ 5⸮⸮⸮⸮⸮i in the serial monitor?

— … — … You could have known by learning the basics thoroughly.

So before any future question read this:
https://forum.arduino.cc/index.php?topic=712201.0

and this Site Maintenance

best regards Stefan

When I start the serial monitor, it throws me out completely:
⸮⸮ 5⸮⸮⸮ 5⸮⸮⸮⸮⸮i
It goes on and on. Why and what does that mean?

You have to set the Terminal Baud Rate equals to the Sketch baud rate, which is 115,200 bauds.

Serial.begin(115200);

I try to implement it and make a few attempts. A big thank you for this code and your time!