Looking for suggestions to improve my BLE led sketch

I have managed to make a sketch that allows me to send a one character value over BLE from an app on my phone and then run some code depending on the character.

Right now, I have it set up with 10 buttons, with each sending a different character to the Arduino. One turns on blinking mode, a bunch set the different intervals, one sets the led to just be on permanently, and one turns the LED entirely off.

I am using a board called a 'BLE-Nano' that can be found on Amazon here.

There is definitely room for improvement in my code. While my setup now does work, the biggest thing wrong with it is that I can only send over one character messages. I have no way for the Arduino to differentiate each message, so currently, I just have it checking serial for the letter or number I have assigned to its action. My method of setting the interval uses this technique, but it's not ideal. Right now, I am just sending it a number that corresponds with an interval. Ideally, I would like to send it the actual interval, and have the Arduino receive it and set the variable to it.

The second thing that I think could be improved is my way of receiving messages. I am combining the one example sketch provided with the board found on their github here and another example from the Arduino project hub here.

The last thing is something that I would like to add, but I am not entirely sure how to implement it. I would like the LED to turn off if it has been on for say 30 min, but allow me to turn it back on afterwards like how I would normally. I was thinking maybe something like a timer that gets reset each time the LED 'blink' or 'solid on' mode is turned on, and after the interval of 30 min, it sets turnOn and ledBlink to false.

I'm not sure how to improve these points, as I have been unable to find any examples that are similar enough to what I am trying to achieve. If anyone has any ideas to contribute or could help me work on this code, I would really appreciate it.

This is what I am working with now. I've tried to comment it the best I can, but let me know if anything isn't clear.

/* 
 *  Based on Bluetooth Basic: LED ON OFF - Avishkar
 *  Original program creator's website - http://bit.do/Avishkar
 *  This program allows you to use BLE to control an LED connected to an Arduino.
 */
 
byte comdata;

char incomingValue = 0;   // Variable for storing incomingValue

unsigned long previousMillis = 0;   // will store last time LED was updated
long interval = 500;    // interval at which to blink (milliseconds)
bool ledBlink = false;
bool turnOn = false;
int ledPin = 5;

void(* resetFunc) (void) = 0; // Emergency shut off - if something glitches and I don't have access to it.

void setup() 
{
  Serial.begin(9600);   // Sets the data rate in bits per second (baud) for serial data transmission
  pinMode(5, OUTPUT);   // Sets digital pin 5 as output pin
}
void loop()
{                          

  if(Serial.available() > 0)  
  {
    Serial.write(comdata);
    incomingValue = Serial.read();   // Read the incoming data and store it into variable incomingValue
    Serial.print(incomingValue);   // Print Value of incomingValue in Serial monitor
    //Serial.print("\n");   //New line 
    if(incomingValue == 'a')   // Blink the LED
      ledBlink = true;
    else if(incomingValue == 'b')    // Turn off LED entirely
    {
      ledBlink = false;   // Set LED blink to off
      turnOn = false;   // Set 'LED solid on' to off
    }
    else if(incomingValue == 'r')    // Emergency shut off - if something glitches and I don't have access to it.
    {
      resetFunc();
    }
    else if(incomingValue == '0')    // 25ms
      interval = 25;
    else if(incomingValue == '1')    // 50ms
      interval = 50;
    else if(incomingValue == '2')    // 100ms
      interval = 100;
    else if(incomingValue == '3')    // 250ms
      interval = 250;
    else if(incomingValue == '4')    // 500ms
      interval = 500;
    else if(incomingValue == '5')    // 750ms
      interval = 750;
    else if(incomingValue == '6')    // 1000ms
      interval = 1000;
    else if(incomingValue == '7')    // Turn LED on solid
    {
      turnOn = true;    // Set LED to turn on solid
      ledBlink = false;   // Set LED blink to off
    }
  }
  
  unsigned long currentMillis = millis();

  if ( currentMillis - previousMillis >= interval )
  {
    previousMillis = currentMillis;   // save the last time you blinked the LED
    
    if ( ledBlink && !turnOn )    // If I am told to blink and I am not currently being told to stay on non-stop, toggle the LED
    {
      digitalWrite( ledPin, !digitalRead( ledPin ) );   // Toggle LED
    }
    else if ( !ledBlink )
    {
      digitalWrite( ledPin, LOW );
    }
  }

  if ( turnOn )   // If told to be on non-stop, set the ledPin to ON each loop
  {
    digitalWrite(ledPin, HIGH);
  }
}

your code reads OK besidesSerial.write(comdata);which is weird? what do you expect this to do, comdata is just declared as a global variable and never used elsewhere, so you'll be sending NULL values?

you could have interval as an unsigned (int or long) variable to keep the coherence with the other values

instead of the long if/else structure, you could use a switch/case and since all your commands are 1 char, you don't really need to check if available() has something for you. Just do a

int incomingValue; // int instead of char as this is what read() returns 
...

switch((incomingValue = Serial.read())) { // read returns -1 if nothing to read
  case 'a': ledBlink = true; break;
  case 'b': ledBlink = false; turnOn = false; break;
  case '0': interval = 25; break;
  case '1': interval = 50; break;
...
  case 'r': resetFunc(); break;
  default: break; // do nothing if it's an unknown command
}

(typed here, might have typos)

SamGurdus:
I have managed to make a sketch that allows me to send a one character value over BLE from an app on my phone and then run some code depending on the character.

What app are you using to send the data? Are you able to specify an arbitrary message (data and length) to send to the connected bluetooth device in the app?

SamGurdus:
There is definitely room for improvement in my code. While my setup now does work, the biggest thing wrong with it is that I can only send over one character messages. I have no way for the Arduino to differentiate each message, so currently, I just have it checking serial for the letter or number I have assigned to its action. My method of setting the interval uses this technique, but it's not ideal. Right now, I am just sending it a number that corresponds with an interval. Ideally, I would like to send it the actual interval, and have the Arduino receive it and set the variable to it.

Currently, your setup sends a command character to the arduino. Depending on the command the arduino does something. What you want is to send a command with payload. So for instance send the intervall command and put the interval into the payload of that command (much like an envelop being the command and containing the payload (the letter)).

The arduino will receive the command/envelepe and depending on the command, it knows what to do with the payload. In the case of the interval command, it should interpret the payload as an interval.

SamGurdus:
The last thing is something that I would like to add, but I am not entirely sure how to implement it. I would like the LED to turn off if it has been on for say 30 min, but allow me to turn it back on afterwards like how I would normally. I was thinking maybe something like a timer that gets reset each time the LED 'blink' or 'solid on' mode is turned on, and after the interval of 30 min, it sets turnOn and ledBlink to false.

Maybe separate your time and led logic? You'd have a state machine for the led and just throw in the time at a specific tick rate (or you tell the time component to tell the led component about an expired interval). The led statemachine should then process these time events (turn led on/off).

SamGurdus:
I'm not sure how to improve these points, as I have been unable to find any examples that are similar enough to what I am trying to achieve. If anyone has any ideas to contribute or could help me work on this code, I would really appreciate it.

These two could help you out:
Serial Input Basics
TimedAction library

LightuC:
What app are you using to send the data? Are you able to specify an arbitrary message (data and length) to send to the connected bluetooth device in the app?

I'm using this app to send over the commands. I can send anything I would like through the 'serial' tab.

Currently, your setup sends a command character to the arduino. Depending on the command the arduino does something. What you want is to send a command with payload. So for instance send the intervall command and put the interval into the payload of that command (much like an envelop being the command and containing the payload (the letter)).

The arduino will receive the command/envelepe and depending on the command, it knows what to do with the payload. In the case of the interval command, it should interpret the payload as an interval.

This is exactly what I would like to do. I'm taking a look at the Serial Input Basics link you included.

Maybe separate your time and led logic? You'd have a state machine for the led and just throw in the time at a specific tick rate (or you tell the time component to tell the led component about an expired interval). The led statemachine should then process these time events (turn led on/off).

This part is definitely above my level of understanding. I'll look into the TimedAction library, but I'm not sure I'll be able to figure it out at this point. Thanks for the reply.

SamGurdus:
This part is definitely above my level of understanding.

Think of it like a chat between the timer and the led module:

Main: @Timer here's an interval: 1000s
Timer: ok
[some time later]
Timer: @all Yoo, the 1000s interval timer expired
Led: k cool, I'm going to toogle my leds
Timer: @all Yoo, the 30s timer expired
Led: Well, let me turn off those led's then
Main: Okay. @timer new interval with 30s please.