App Inventor slider and bluetooth

I want to control a led with my phone.
I have an HC-05, slider in the app, to control the brightness.
I tried to send the slider position (like 1,2,3), but the app didn’t want to encode it.
I tried to have: If: slider.position is 0 > 1 and 1 < 0
then send 4
and then my Arduino received the 4 and did nothing.

// Constant Definitions
#define LEDPIN 6 // Pin on Arduino board to which status LED is connected
#define READ_RATE 100 // How often the serial link is read, in milliseconds
#define FLASH_RATE 100 // The on/off period in milliseconds, for the LED Flash status feedback

// Declarations
byte cmd;  // Stores the next byte of incoming data, which is a "command" to do something 
byte param; // Stores the 2nd byte, which is the command parameter

// Initialization
void setup() {

  pinMode(LEDPIN, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  digitalWrite(LEDPIN, LOW);
  Serial.begin(9600);       // start serial communication at 9600bpsű

// Arduino Execution Loop
void loop() {

  if ( Serial.available() )       // if data is available to read
    cmd =;         // read it and store it in 'cmd'
    // Data format is byte 1 = command, byte 2 = parameter
  switch ( cmd ) {
    case 1:
      // First byte contains a generic "command" byte. We arbitrarily defined '1' as the command to then check the 2nd parameter byte
      // User can additional commands by adding case 2, 3, 4, etc
         // read the parameter byte
         param =;
         switch (param)
           case 1: 
             // Android device requests the Arduino to send some data back to Android
             if (Serial)
           case 2:
             // C hang
           case 3: 
             // D hang

           case 4:
           case 5:

              case 6:

              case 7:

              case 8:

              case 9:

              case 10:

              case 11:

              case 12:

              case 13:

              case 14:


           default: break; // do nothing

         } // switch (param)
      } // switch (cmd) case 1
      default: break; // do nothing
  } // switch (cmd)
  delay(READ_RATE);                    // wait 100ms for next reading

  // Support routine
void flashLED (int n) {
  // Flash the LED n times, to provide an on board status indicator
  for (int i=1; i<=n; i++) {
    digitalWrite (LEDPIN, HIGH);
    delay (FLASH_RATE);
    digitalWrite (LEDPIN, LOW);

I suggest that you read the serial input basics tutorial for information on robust code for reading serial input.

Sorry. but I still don't understand. Can you help me?

Did you read the recommended tutorial?

Your data should be sent in the format “<cmd,value>”.

Read the entire message before you decide what to do. Don’t parse on the fly.

You will find it easier to debug and work with numbers expressed as text instead of bytes.

Use the Serial monitor to develop the program with keyboard input without ever using a BT app.

Get the program working with a standard serial terminal BT app before moving to a custom app.

What Arduino board are you using?

Using the Serial port for both the HC05 and serial monitor can be problematic. I use a separate serial port for the HC05. On an Uno i use SoftwareSerial for the HC05 and hardware serial (USB) for serial monitor. On a Mega I would use one of the extra hardware serial ports (Serial1, Serial2, Serial3).

The way that you are reading serial it is easy to get "out of sync". One extra character can throw the serial reads off and you don't know what you are getting. The serial input basics reads the whole message and then parses out the parts to the right places.

Nothing wrong with your approach, you don’t need to complicate it with < >
However choosing say A,B,C for the commands and 1,2,3 etc for parameter will make it easy to synchronize the input and reject invalid commands and parameters.

It you want a true command word driven input you can use my SafeString library (available from the library manager) which includes simple code for reading command words and acting on them.

See the User Command example

to analyse such a problem,
you simply print to the serial monitor what you really receieved

  if ( Serial.available() )       // if data is available to read
    cmd =;         // read it and store it in 'cmd'
    // Data format is byte 1 = command, byte 2 = parameter

the "inserted # will clearly indicate what is inside variable cmd
for example if it is a LF
the output will look like


best regards Stefan

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