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