I have probleme in my switch/case when i add to statement

switch(receivedValue){
          case 0:
          digitalWrite(led1, LOW); 
          break;
          case 1:
          digitalWrite(led1, HIGH); 
          break;
          case 2:
          digitalWrite(led2, HIGH); 
          break;
          case 3:
          digitalWrite(led2, LOW);  
          break;
          case 4:
          digitalWrite(led3, HIGH); 
          break;
          case 5:
          digitalWrite(led3, LOW); 
          break;
          case 6:
          digitalWrite(led4, HIGH); 
          break;
          case 7:
          digitalWrite(led4, LOW); 
          break;
        }

when my code is like up there it works normally but when i add a second statement like the code below it stops working

switch(receivedValue){
          case 0:
          digitalWrite(led1, LOW); 
          break;
          case 1:
          digitalWrite(led1, HIGH); 
          digitalWrite(buzz, HIGH); 
          break;
          case 2:
          digitalWrite(led2, HIGH); 
          digitalWrite(buzz, HIGH); 
          break;
          case 3:
          digitalWrite(led2, LOW);  
          break;
          case 4:
          digitalWrite(led3, HIGH); 
          digitalWrite(buzz, HIGH); 
          break;
          case 5:
          digitalWrite(led3, LOW); 
          break;
          case 6:
          digitalWrite(led4, HIGH); 
           digitalWrite(buzz, HIGH); 
          break;
          case 7:
          digitalWrite(led4, LOW); 
          break;
        }

the complete code is

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-web-bluetooth/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer* pServer = NULL;
BLECharacteristic* pSensorCharacteristic = NULL;
BLECharacteristic* pLedCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

int led1 = 13;
int led2 = 12;
int led3 = 14;
int led4 = 27;
int btn = 25;
int buzz = 26;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "19b10000-e8f2-537e-4f6c-d104768a1214"
#define SENSOR_CHARACTERISTIC_UUID "19b10001-e8f2-537e-4f6c-d104768a1214"
#define LED_CHARACTERISTIC_UUID "19b10002-e8f2-537e-4f6c-d104768a1214"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic* pLedCharacteristic) {
        std::string value = pLedCharacteristic->getValue();
        if (value.length() > 0) {
            

            int receivedValue = static_cast<int>(value[0]);
            Serial.println(receivedValue);
          
        switch(receivedValue){
          case 0:
          digitalWrite(led1, LOW); 
          break;
          case 1:
          digitalWrite(led1, HIGH); 
          digitalWrite(buzz, HIGH); 
          break;
          case 2:
          digitalWrite(led2, HIGH); 
          break;
          case 3:
          digitalWrite(led2, LOW);  
          break;
          case 4:
          digitalWrite(led3, HIGH); 
          break;
          case 5:
          digitalWrite(led3, LOW); 
          break;
          case 6:
          digitalWrite(led4, HIGH); 
          break;
          case 7:
          digitalWrite(led4, LOW); 
          break;
        }
        
    }
    if(digitalRead(btn)== HIGH){
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(buzz, LOW); 
    }
  }
};



void setup() {
  Serial.begin(115200);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(buzz, OUTPUT);
  pinMode(btn, INPUT);
  // Create the BLE Device
  BLEDevice::init("ESP32");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pSensorCharacteristic = pService->createCharacteristic(
                      SENSOR_CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  // Create the ON button Characteristic
  pLedCharacteristic = pService->createCharacteristic(
                      LED_CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_WRITE
                    );

  // Register the callback for the ON button characteristic
  pLedCharacteristic->setCallbacks(new MyCharacteristicCallbacks());

  // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  // Create a BLE Descriptor
  pSensorCharacteristic->addDescriptor(new BLE2902());
  pLedCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
  BLEDevice::startAdvertising();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
    // notify changed value
    if (deviceConnected) {
        pSensorCharacteristic->setValue(String(value).c_str());
        pSensorCharacteristic->notify();
        value++;
        Serial.print("New value notified: ");
        Serial.println(value);
        delay(3000); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
    }
    // disconnecting
    if (!deviceConnected && oldDeviceConnected) {
        Serial.println("Device disconnected.");
        delay(500); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("Start advertising");
        oldDeviceConnected = deviceConnected;
    }
    // connecting
    if (deviceConnected && !oldDeviceConnected) {
        // do stuff here on connecting
        oldDeviceConnected = deviceConnected;
        Serial.println("Device Connected");
    }
}

What exactly do you mean by that ?

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