This code allows me to check for serial monitor and movement of servo,the problem is when it is setting up it moves but when I use the app via Bluetooth to move the robot it doesn't move at all pls help #include <SoftwareSerial.h> #include <Servo.h>
void loop() {
//Read from bluetooth and write to usb serial
if (bluetooth.available() >= 2) {
String mode = bluetooth.readString();
String type = bluetooth.readString();
if (mode == 'm') {
if(bluetooth.available()>=1){
int servopos = bluetooth.read();
if (type == 'a') {
Serial.println(servopos);
Servo1.write(servopos);
delay(10); // Recommended delay after each Servo.write()
} else if (type == 'b') {
Serial.println(servopos);
Servo2.write(servopos);
} else if (type == 'c') {
Serial.println(servopos);
Servo3.write(servopos);
} else if (type == 'd') {
Serial.println(servopos);
Servo4.write(servopos);
} else if (type == 'e') {
Serial.println(servopos);
Servo5.write(servopos);
} else if (type == 'f') {
Serial.println(servopos);
Servo6.write(servopos);
}
} else if (mode == 'a') {
int action[6]; // Declare the array with a size
String command = bluetooth.readString();
int actionIndex = 0; // Index for storing tokens
if (command.length() > 0) {
int index = 0;
String token;
while ((index = command.indexOf('/')) != -1) {
token = command.substring(0, index);
if (actionIndex < 6) { // Check array bounds
action[actionIndex++] = token.toInt();
}
command.remove(0, index + 1);
}
// Handle the last token if any
if (command.length() > 0 && actionIndex < 6) {
action[actionIndex++] = command.toInt(); // Store the last token
}
for (int i = 0; i < actionIndex; i++) {
if (i < 6) {
bluetooth.println(action[i]);
switch (i) {
case 0: Servo1.write(action[i]); break;
case 1: Servo2.write(action[i]); break;
case 2: Servo3.write(action[i]); break;
case 3: Servo4.write(action[i]); break;
case 4: Servo5.write(action[i]); break;
case 5: Servo6.write(action[i]); break;
}
delay(1000);
}
}
}
command = "";
}
}
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Don't use pins 0 and 1for SoftwareSerial. They are used by the hardware Serial interface of most common Arduino boards although you have not told us which Arduino you are using
I'm using Arduino Uno ,the thing is I can see the info on serial monitor while using port 0,1 ,if I use other pwm the serial monitor won't show anything,the only problem right now is that I'm receiving inputs from my app but it's not running.it does move during the setup position
IO-pins 0,1 are used for the hardware-serial.
If you write code that makes software-serial use the same IO-pins 0,1 as hardware-serial this is basically the same as if you call two people on two different phones and then you want to talk to both persons at the same time: doesn't work. The words will interfere and you will understand nothing.
You will have to add code to your program that realises the "transportation" of the received data from software-serial to hardware-serial to still make the data send from bluetooth visible on the serial monitor.
example:
bluetooth-receiver is connected to IO-pin 8 and 9
your code receives the data with software-serial
as pseudo-code
myBluetoothData = receiveFrom-IO-Pin 9
take data that is stored into variable myBluetoothData and send it to the serial monitor
Serial.println(myBluetoothData);
You will have to learn quite some things to understand how it really works.
You can ask as many questions as you like even hundreds of questions.
If you are willing to take advice for serious and follow the advice of experienced users your flow of questions will always be answered.
If you insist on not learning by argueing against the advice your potential helpers will turn away and answer other threads.