Conflict with RGB led and Buzzer

Hello, I am using a esp8622-12e nodemcu to build a base station for my sensor network. When activated the station displays message on lcd, starts buzzing alarm and flashes an led. Then I had the idea to replace my series of led's (specific color led for each event) with a RGB led. That worked great but introduced a problem with the buzzer, namely the buzzer doesnt work when using the rgb led.

I think the problem is that the rgb led uses analogWrite() and the buzzer uses tone() and the esp8266 will not support more than 1 analog pin at a time. I think...

I've experimented with using noTone() in different ways to free up the pin but to no avail.

If there are any suggestions on how this could work I would be greatly appreciative. If I cannot come to a solution I may integrate an mp3-tf-16p which -I believe- would eliminate any analog conflicts with the rgb led but would be overkill when all I want is annoying buzzing. If that is in fact the problem.

void HallwayTrip(){  //this function called when hall sensor sends a signal
  while(HallwayMotionState == 1){  //while alarm is active
    if (digitalRead(buttonApin) == LOW) {   //clear alarm upon pushing button
      HallwayMotionState = 0;
      analogWrite(RLedPin, 0);
      analogWrite(BLedPin, 0);
      analogWrite(GLedPin, 0);
      lcdDisplay("                ",0,0);
      lcdDisplay("                ",0,1);
      lcd.setBacklight(0); //off
    }
    if (millis() - lastHallwayMillis > 1000) {  //buzzes and flashes during alarm
      lastHallwayMillis = millis();
      buzzBuzzer(650,750);   //buzz the buzzer
      flashLed(1);   //flash the led
    } 
    client.loop();   //keeps station connection alive during alarm
    delay(10);  
  }
}


void buzzBuzzer(int buzzFreq, int buzzLength){  //buzzes differently for each alarm event
  tone(BuzzPin, buzzFreq, buzzLength);
}


void flashLed(int LedID){  //experimental implementation of the rgb led, LedID is used to differentiate alarm events with patterns and colors
  switch(LedID){
    case 1: //Hallway motion LED
      if(flipSwitch == 0){analogWrite(RLedPin, 0);analogWrite(BLedPin, 255);analogWrite(GLedPin, 0); flipSwitch = 1; break;} //blue
      if(flipSwitch == 1){analogWrite(RLedPin, 0);analogWrite(BLedPin, 0);analogWrite(GLedPin, 0); flipSwitch = 2; break;}  //off
      if(flipSwitch == 2){analogWrite(RLedPin, 0);analogWrite(BLedPin, 255);analogWrite(GLedPin, 128); flipSwitch = 3; break;}  //teal
      if(flipSwitch == 3){analogWrite(RLedPin, 0);analogWrite(BLedPin, 0);analogWrite(GLedPin, 0); flipSwitch = 0; break;}  //off
  }
}

I've grown frustrated and know it is either super simple and I just don't get it or I am on the wrong track. Any advice or insight is most welcome.

To add:
I have tried:
-changing the order buzzer->led to led->buzzer.
-various length delays to give buzzer time to finish and calling noTone(BuzzPin) afterwards
-noTone() on the rgb led pins in varied ways
-checked speaker connections and verified it works by commenting out Led code

The problem began upon implementation of the rgb led.

Here is the entire base station code

#include <ESP8266WiFi.h>
#include <MQTT.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

const char ssid[] = "xxxxxx";
const char pass[] = "xxxxxxx";

const int RLedPin = 13;
const int BLedPin = 12;
const int GLedPin = 14;

const int BuzzPin = 5;
const int buttonApin = 4;

WiFiClientSecure net;
MQTTClient client;

unsigned long lastHallwayMillis = 0;

int HallwayMotionState = 0;

int flipSwitch;

LiquidCrystal_I2C lcd(0x27,16,2); // Check I2C address of LCD, normally 0x27 or 0x3F


////////////////////////////////////////////////////////////////////////
//////////////////////////--CONNECT TO WIFI--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void connect() {

  lcd.setBacklight(1);  //on
  
  lcdDisplay("  xxxxxx HOUSE  ",0,0);
  lcdDisplay("checking wifi...",0,1);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }

  lcdDisplay("connecting...",0,1);

  while (!client.connect("Base Station", "xxxxxx", "xxxxxxxxxxxxxxx")) {
    delay(1000);
  }

  lcdDisplay("connected!      ",0,1);

  client.subscribe("xxxxxxxx");
  delay(1000);
  
  lcdDisplay("                ",0,0);
  lcdDisplay("                ",0,1);
  lcd.setBacklight(0); //off
}


////////////////////////////////////////////////////////////////////////
//////////////////////////--MESSAGE RECIEVED--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void messageReceived(String &topic, String &payload) {

//hallway motion sensor
  if((payload == "Hallway") && (HallwayMotionState == 0)){
    lcd.setBacklight(1); //on
    lcdDisplay("     HALLWAY    ",0,0);
    lcdDisplay("     MOVEMENT   ",0,1);
    
    HallwayMotionState = 1;
    HallwayTrip();
  }
}


////////////////////////////////////////////////////////////////////////
//////////////////////////--HALLWAY SENSOR--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void HallwayTrip(){
  while(HallwayMotionState == 1){
    if (digitalRead(buttonApin) == LOW) {
      HallwayMotionState = 0;
      analogWrite(RLedPin, 0);
      analogWrite(BLedPin, 0);
      analogWrite(GLedPin, 0);
      lcdDisplay("                ",0,0);
      lcdDisplay("                ",0,1);
      lcd.setBacklight(0); //off
    }
    if (millis() - lastHallwayMillis > 1000) {
      lastHallwayMillis = millis();
    //  buzzBuzzer(650,750);
      flashLed(1);
    } 
    client.loop();   //keeps station connection alive during alarm
    delay(10);  // <- fixes some issues with WiFi stability 
  }
}


////////////////////////////////////////////////////////////////////////
//////////////////////////--BUZZER--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void buzzBuzzer(int buzzFreq, int buzzLength){
//  noTone(BLedPin); noTone(GLedPin); noTone(RLedPin);
  tone(BuzzPin, buzzFreq, buzzLength);
}


////////////////////////////////////////////////////////////////////////
//////////////////////////--LED LIGHTS--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void flashLed(int LedID){
  switch(LedID){
    case 1: //Hallway motion LED
      if(flipSwitch == 0){analogWrite(RLedPin, 0);analogWrite(BLedPin, 255);analogWrite(GLedPin, 0); flipSwitch = 1; break;} //blue
      if(flipSwitch == 1){analogWrite(RLedPin, 0);analogWrite(BLedPin, 0);analogWrite(GLedPin, 0); flipSwitch = 2; break;}  //off
      if(flipSwitch == 2){analogWrite(RLedPin, 0);analogWrite(BLedPin, 255);analogWrite(GLedPin, 128); flipSwitch = 3; break;}  //teal
      if(flipSwitch == 3){analogWrite(RLedPin, 0);analogWrite(BLedPin, 0);analogWrite(GLedPin, 0); flipSwitch = 0; break;}  //off
  }
}



////////////////////////////////////////////////////////////////////////
//////////////////////////LCD DISPLAY////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void lcdDisplay(String m, int col, int row){
  lcd.setCursor(col, row);
  lcd.print(m);
}




////////////////////////////////////////////////////////////////////////
//////////////////////////--SETUP--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void setup() {

  pinMode (RLedPin, OUTPUT);
  pinMode (BLedPin, OUTPUT);
  pinMode (GLedPin, OUTPUT);
  pinMode (BuzzPin, OUTPUT);
  
  pinMode(buttonApin, INPUT_PULLUP);

  lcd.begin(0,2);                    
  lcd.setBacklight(0); //off
 

  Serial.begin(115200);
  WiFi.begin(ssid, pass);

  client.begin("broker.shiftr.io", 8883, net);
  client.onMessage(messageReceived);

  connect();
}



////////////////////////////////////////////////////////////////////////
//////////////////////////--MAIN--////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void loop() {
  client.loop();
  delay(10);  // <- fixes some issues with WiFi stability

  if (!client.connected()) {
    connect();
  }
}

I don't have a solution to your problem but if you end up needing a workaround you should consider an active piezo buzzer. An active buzzer has the frequency generating circuit built in so you don't need to use tone(). You can just use digitalWrite(). In fact, you might check to make sure you don't already have an active buzzer since they are very common.

pert:
I don’t have a solution to your problem but if you end up needing a workaround you should consider an active piezo buzzer. An active buzzer has the frequency generating circuit built in so you don’t need to use tone(). You can just use digitalWrite(). In fact, you might check to make sure you don’t already have an active buzzer since they are very common.

Cheers pert! I had gotten tunnel vision and your post showed me the light!
I am using a old alarm clock to house the device. Re-using the 8ohm 1watt speaker and buttons already built into it. digitalWrite() is working perfect with the speaker and I can still change the pattern and freq AND it works as expected with the rgb led. Awesome I just needed a bump, Thanks!!

void buzzBuzzer(int buzzFreq, int buzzLength){
  for (i = 0; i <buzzLength; i++){
    digitalWrite (BuzzPin, HIGH);
    delay (buzzFreq) ;
    digitalWrite (BuzzPin, LOW);
    delay (buzzFreq) ;
  }
  for (i = 0; i <buzzLength; i++){
    digitalWrite (BuzzPin, HIGH);
    delay (buzzFreq + 1) ;
    digitalWrite (BuzzPin, LOW);
    delay (buzzFreq + 1) ;
  }
}

I'm glad to hear you found a solution. Enjoy! Per