MIT slider to Arduino through Bluetooth

Hello. My project is turning on and off a dc motor using Bluetooth. Now I want to add one slider to control the speed of the motor with PWM. I tried so many ways to print the value of the slider in the serial monitor but failed. So far I only can turn on and off the motor with Bluetooth. Please teach me how to read the slider value from MIT App Inventor?

#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);
}

Welcome to the forum

What sort type of value does App Inventor send as the slider value, how is it terminated and how have you tried to read it ?

What did you see in the Serial Monitor? This should show at the start, before the motors are running.

Immediately after this...

... place this:

 Serial.print(MOTOR);
 Serial.print(" ");
 Serial.write(MOTOR);

"print" and "write" might show two different things (a character and a value)

I have tried to read a value, but I'm not sure whether it's a string or an integer. I used Serial.println, but nothing is showing up.

The Serial Monitor only displays the word 'Ready,' and nothing after that. I tried the solution you suggested, but it didn't work for me.
This is the block design for the slider in MIT App Inventor.
WhatsApp Image 2023-11-18 at 10.48.24_925da506
Could you please help me figure out why I'm not receiving the value? And How can I determine the type of the value?

I do not know how to use ?codeblocks?... can you show the the whole program in codeblocks, or is there a way to post (here) the text equivalent of your codeblocks?

Try putting a "print" statement after each step - something like this (for example):

when Slider1.PositionChanged(thumbPosition);
  Serial.println("PositionChanged");
  do
      Serial.println("PosChange-do");
    call BluetoothClient1.Send1ByteNumber(round(number(get(thumbPosition))));
      Serial.println("BluetoothClient1");

I assume you have verified your Bluetooth connection is valid and you are just trying to find where the "move the slider" data is not being read.

That figure is programming for the slider in MIT App Inventor and is not coding for Arduino. It actually creates programs by dragging and dropping different blocks, snapping them together in a graphical interface. This is why it is hard to see what is the type of the value.
I just noticed that I sent a 1-byte number, but my code wrote this.

bluetooth.available() == 2

is it because of this?

Can you "look behind" the blocky stuff? The C++ code should be accessible.

Was it terminated by a Carriage Return or Linefeed ? If so, then you will receive 2 characters

Your title contains, "... Arduino..."

Maybe try this:

Found here:

How do you save your work? Examine the contents of the file.

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