Slider to Arduino Uno through bluetooth

I am currently facing an issue with transferring data from a slider to an Arduino Uno through Bluetooth.
Please help me identify what is wrong with my code or my application.
This is my Arduino coding:

#include <SoftwareSerial.h>

int In3 = 7;
int In4 = 8;
int ENB = 5;
int SPEED = 100;

SoftwareSerial bluetooth(10, 11); // RX, TX

void setup()
{
  Serial.begin(9600);
  bluetooth.begin(9600);
  bluetooth.print("AT+NAME=Handsome"); // Change "MyDevice" to your desired name
  delay(500); // Delay for command execution

  pinMode(2, OUTPUT);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);
  pinMode(ENB, OUTPUT);

  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);
  Serial.print("Ready");
}

void loop()
{
  if (bluetooth.available() == 1) {
    String MOTOR = bluetooth.readString(); // Read the incoming data
    
    if (MOTOR == "N") {
      Serial.println("N");
      SPEED = 200;
    }

    if (MOTOR == "F") {
      SPEED = 0;
      Serial.println("F");
    }
    analogWrite(ENB, SPEED);
  }

  if (bluetooth.available() == 2) {
    SPEED = bluetooth.read();
    analogWrite(ENB, SPEED);
    
    Serial.println(SPEED);
  }
  delay(50);

Mixing data types in the same data stream is a nightmare to coordinate. Stick to text and use message delimiters like \n for end of message.

Bytes available will almost never equal exactly 1 or 2. The data piles up in the BT incoming data buffer, increasing that count.

Don't send data from the slider event. It fires fast like a machine gun. Instead, have a clock timer check the thumb value, and only send if it has changed since the last time.
That will throttle your data flow.

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