Arduino Shift Meter Lag Problem ELM327 ESP32 NeoPixel FastLED

Hello everyone, I am new to arduino and I'm trying to make a shift light meter for my car, I am using ESP32 and ELM327 as my hardware, and I am using the ELMduino and also FastLED Neopixel library in my software. The LEDs work fine on their own, but after adding a buzzer it lags the whole operation and rpm detection. I kindly ask help from people who are more informed on this topic to analyze what could be the problem, I suspect that it has something to do with my code, below are the codes I used in this project (code works fine no error codes just lags):

Regular Code No Buzzer using NeoPixel (no lag)

#include "BluetoothSerial.h"
#include "ELMduino.h"
#include <Adafruit_NeoPixel.h>

BluetoothSerial SerialBT;
#define ELM_PORT SerialBT
#define DEBUG_PORT Serial

#define PIN 13

int jumlah = 8;
int value;
int buzzerPin = 12;
ELM327 myELM327;

uint32_t rpm = 0;
uint8_t address[6] = {0x00, 0x10, 0xCC, 0x4F, 0x36, 0x03}; //ELM327 MAC address

Adafruit_NeoPixel strip = Adafruit_NeoPixel(jumlah, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
#if LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif

  DEBUG_PORT.begin(38400);
  SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
  strip.begin();
  strip.setBrightness(10);
 pinMode(buzzerPin, OUTPUT);
  strip.show();
 
  
  bool connected = false; // Flag to track connection status

  while (!connected) {
    if (!ELM_PORT.connect(address))
    {
      DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
      // Red LED indication
      strip.clear();
      strip.setPixelColor(0, 255, 0, 0); // Red
      strip.setPixelColor(7, 255, 0, 0); // Red
      strip.show();
      delay(100);
       DEBUG_PORT.begin(38400);
  SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
    tone(buzzerPin, 2600);
      delay(100);
      noTone(buzzerPin);
  
    }
    else {
      connected = true; // Connection successful, set the flag
      // Green LED indication
      strip.clear();
      strip.setPixelColor(0, 255, 255, 0); // Yellow
      strip.setPixelColor(7, 255, 255, 0); // Yellow
      strip.show();
    }
  }
  
  // If connected to ELM327, proceed with the rest of the setup
  ELM_PORT.begin("ArduHUD", true);

  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    Serial.println("Couldn't connect to OBD scanner - Phase 2");
    // Red LED indication for failure
    strip.clear();
    strip.setPixelColor(0, 0, 255, 0); // Red
    strip.setPixelColor(7, 255, 255, 0); // Yellow
    strip.show();
    while (1);
  }
  else {
    // Green LED indication for successful connection
    strip.clear();
    strip.setPixelColor(0, 0, 255, 0); // Green
    strip.setPixelColor(7, 0, 255, 0); // Green
    strip.show();
      tone(buzzerPin, 2600);
      delay(100);
      noTone(buzzerPin);
        tone(buzzerPin, 2600);
      delay(100);
      noTone(buzzerPin);
  }

  Serial.println("Connected to ELM327");
}

void loop()
{
  
  float tempRPM = myELM327.rpm();
float redline = 3000;
 float one = floor(redline/2.7);
float two = floor(redline/2.5);
float three = floor(redline/2);
float four =  floor(redline/1.7);
float five = floor(redline/1.5);
float six = floor(redline/1.3);
float seven = floor(redline/1.1);

    rpm = (uint32_t)tempRPM;
    Serial.print("RPM: ");
    Serial.println(rpm);
    
    // RPM-based LED control
    if (rpm < 1000 && rpm!=0) {
      strip.clear();
      strip.show();
    
    } else if (rpm >= 1000 && rpm < one && rpm!=0) {
       strip.clear();
      strip.setPixelColor(0, 0, 255, 0);
     
      strip.show();
    }

    else if (rpm >= one && rpm < two && rpm!=0) {
       strip.clear();
      strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
     
      strip.show();
    }
    else if (rpm >= two && rpm < three && rpm!=0) {
       strip.clear();
      strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
      strip.setPixelColor(2, 0, 255, 0);
     
      strip.show();
    }
    
     else if (rpm >= three && rpm < four && rpm!=0) {
       strip.clear();
   strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
      strip.setPixelColor(2, 0, 255, 0);
      strip.setPixelColor(3,255, 255, 0);
      
      strip.show();
    }
    else if (rpm >= four && rpm < five && rpm!=0) {
       strip.clear();
 strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
      strip.setPixelColor(2, 0, 255, 0);
      strip.setPixelColor(3,255, 255, 0);
       strip.setPixelColor(4,255, 255, 0);
       
      strip.show();
    }
     else if (rpm >= five && rpm < six && rpm!=0) {
       strip.clear();
 strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
      strip.setPixelColor(2, 0, 255, 0);
      strip.setPixelColor(3,255, 255, 0);
       strip.setPixelColor(4,255, 255, 0);
        strip.setPixelColor(5,255, 255, 0);
       
      strip.show();
    }
     else if (rpm >= six && rpm< seven) {
      // Blinking Red
         strip.clear();
      strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
      strip.setPixelColor(2, 0, 255, 0);
      strip.setPixelColor(3,255, 255, 0);
       strip.setPixelColor(4,255, 255, 0);
        strip.setPixelColor(5,255, 255, 0);
              strip.setPixelColor(6,255, 0, 0);
              
                
        strip.show();
    
       
      
      
    }
     else if (rpm >= seven && rpm < redline) {
      // Blinking Red
         strip.clear();
      strip.setPixelColor(0, 0, 255, 0);
      strip.setPixelColor(1, 0, 255, 0);
      strip.setPixelColor(2, 0, 255, 0);
      strip.setPixelColor(3,255, 255, 0);
       strip.setPixelColor(4,255, 255, 0);
        strip.setPixelColor(5,255, 255, 0);
              strip.setPixelColor(6,255, 0, 0);
              strip.setPixelColor(7,255, 0, 0);
              
                
        strip.show();
    
       
      
      
    }
    else if (rpm >= redline && rpm!=0) {
      // Blinking Red
        for (int blinkCount = 0; blinkCount < 3; blinkCount++) {
        strip.clear();
        strip.setPixelColor(0, 255, 0, 0); // Red
        strip.setPixelColor(1, 255, 0, 0);
        strip.setPixelColor(2, 255, 0, 0);
        strip.setPixelColor(3, 255, 0, 0);
        strip.setPixelColor(4, 255, 0, 0);
        strip.setPixelColor(5, 255, 0, 0);
        strip.setPixelColor(6, 255, 0, 0);
        strip.setPixelColor(7, 255, 0, 0);
       
       
        strip.show();
        delay(100); // Half-second delay for each blink

       

        strip.clear();
        strip.show();
        delay(100); // Half-second delay for each blink
    }
     
    
       
      
      
    }

  
}

Code with Buzzer and FastLED (LAGS) note: I decided to change to FastLED because people say its faster but still lags

#include "BluetoothSerial.h"
#include "ELMduino.h"
#include <FastLED_NeoPixel.h>

BluetoothSerial SerialBT;
#define ELM_PORT SerialBT
#define DEBUG_PORT Serial
#define NUM_LEDS 8
#define DATA_PIN 13
#define buzzerPin  12

ELM327 myELM327;

uint32_t rpm =0;
uint8_t address[6] = {0x00, 0x10, 0xCC, 0x4F, 0x36, 0x03}; //ELM327 MAC address
CRGB leds[NUM_LEDS];

void setup()
{
#if LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif

  DEBUG_PORT.begin(38400);
  SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  pinMode(buzzerPin, OUTPUT);

 
  
  bool connected = false; // Flag to track connection status

  while (!connected) {
    if (!ELM_PORT.connect(address))
    {
      DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");

      // Red LED indication
      FastLED.clear();
       leds[0] = CRGB::Red; 
        leds[7] = CRGB::Red; 
    FastLED.show();
      delay(100);
       DEBUG_PORT.begin(38400);
  SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);
  tone(buzzerPin, 2600);
      delay(100);
      noTone(buzzerPin);
    }
    else {
      connected = true; // Connection successful, set the flag
      // Green LED indication
            FastLED.clear();
     leds[0] = CRGB::Yellow; 
        leds[7] = CRGB::Yellow; 
    FastLED.show();
      

    }
  }
  
  // If connected to ELM327, proceed with the rest of the setup
  ELM_PORT.begin("ArduHUD", true);

  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    Serial.println("Couldn't connect to OBD scanner - Phase 2");
    // Red LED indication for failure
          FastLED.clear();
   leds[0] = CRGB::Yellow; 
        leds[7] = CRGB::Red; 
    FastLED.show();
    while (1);
  }
  else {
    // Green LED indication for successful connection
          FastLED.clear();
 leds[0] = CRGB::Green; 
        leds[7] = CRGB::Green; 
    FastLED.show();
    tone(buzzerPin, 2600);
      delay(100);
      noTone(buzzerPin);
      tone(buzzerPin, 2600);
      delay(100);
      noTone(buzzerPin);
  }

  Serial.println("Connected to ELM327");
}

void loop()
{
  
  float tempRPM = myELM327.rpm();
float redline = 3000;
 float one = floor(redline/2.7);
float two = floor(redline/2.5);
float three = floor(redline/2);
float four =  floor(redline/1.7);
float five = floor(redline/1.5);
float six = floor(redline/1.3);
float seven = floor(redline/1.1);

    rpm = (uint32_t)tempRPM;
    Serial.print("RPM: ");
    Serial.println(rpm);
    
    // RPM-based LED control
    if (rpm < 1000 && rpm!=0) {
      FastLED.clear();
      FastLED.show();
    
    } else if (rpm >= 1000 && rpm < one && rpm!=0) {
       FastLED.clear();
    leds[0] = CRGB::Green; 
     
      FastLED.show();
    }

    else if (rpm >= one && rpm < two && rpm!=0) {
       FastLED.clear();
     leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     
      FastLED.show();
    }
    else if (rpm >= two && rpm < three && rpm!=0) {
       FastLED.clear();
     leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     leds[2] = CRGB::Green; 
     
      FastLED.show();
    }
    
     else if (rpm >= three && rpm < four && rpm!=0) {
       FastLED.clear();

      leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     leds[2] = CRGB::Green; 
      leds[3] = CRGB::Yellow; 
   
      FastLED.show();
    }
    else if (rpm >= four && rpm < five && rpm!=0) {
       FastLED.clear();
 leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     leds[2] = CRGB::Green; 
      leds[3] = CRGB::Yellow; 
      leds[4] = CRGB::Yellow;
       
      FastLED.show();
    }
     else if (rpm >= five && rpm < six && rpm!=0) {
       FastLED.clear();
 leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     leds[2] = CRGB::Green; 
      leds[3] = CRGB::Yellow; 
      leds[4] = CRGB::Yellow;
         leds[5] = CRGB::Yellow;
       
      FastLED.show();
    }
     else if (rpm >= six && rpm< seven) {
      // Blinking Red
         FastLED.clear();
     
               leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     leds[2] = CRGB::Green; 
      leds[3] = CRGB::Yellow; 
      leds[4] = CRGB::Yellow;
         leds[5] = CRGB::Yellow;
            leds[6] = CRGB::Red;
                
        FastLED.show();
    
       
      
      
    }
     else if (rpm >= seven && rpm < redline) {
      // Blinking Red
         FastLED.clear();
     leds[0] = CRGB::Green; 
     leds[1] = CRGB::Green; 
     leds[2] = CRGB::Green; 
      leds[3] = CRGB::Yellow; 
      leds[4] = CRGB::Yellow;
         leds[5] = CRGB::Yellow;
            leds[6] = CRGB::Red;
             leds[7] = CRGB::Red;
                
                
        FastLED.show();
    
       
      
      
    }
    else if (rpm >= redline && rpm!=0) {
      // Blinking Red
        for (int blinkCount = 0; blinkCount < 3; blinkCount++) {
        FastLED.clear();
    leds[0] = CRGB::Red; 
     leds[1] = CRGB::Red; 
     leds[2] = CRGB::Red; 
      leds[3] = CRGB::Red; 
      leds[4] = CRGB::Red;
         leds[5] = CRGB::Red;
            leds[6] = CRGB::Red;
             leds[7] = CRGB::Red;
       tone(buzzerPin, 2600);
        FastLED.show();
        delay(100); // Half-second delay for each blink

       

        FastLED.clear();
        FastLED.show();
         noTone(buzzerPin);
        delay(100); // Half-second delay for each blink
    }
     
    
       
      
      
    }

  
}

Thankyou in advance and sorry if there was any mistake I made in this post since I am new to the forum.

Hello, @maveishungry. I'm guessing it might have something to do with the interrupts being generated for the tone function to work, or more likely because you have 100 millisecond delays when using the buzzer.

hello @flashko , that could be a factor, but what's weird is that as seen in the code, the buzzer function is only activated when the rpm reaches redline (3000 RPMS), but it lags even when the rpm is below that (when the led is showing green-yellow). As for the interrupts, is there any way i can get rid of that? Thankyou for your reply.

If you do not have requirements for the frequency of the buzzer, you can try an active buzzer, which is only turned on and off by the microcontroller. I can't think of anything else. Maybe someone else will help you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.