sender is uno
reciever is uno ( nano for testing )
code is from www.
hardware serial is working but we need software serial as we are creating a user product.
you say hardware serial is working - do you mean the USB Serial to the serial monitor?
the UNO does not have any other available hardware ports?
if you are building a user product using Bluetooth or BLE I recommend you move to a ESP32 which has onboard WiFi and Bluetooth
from arduino-uno-pins-a-complete-practical-guide Note that the Serial used by USB is the same as the one used with pins 0 and 1. So, if you want to connect another device to the RX/TX pins of your board, remember not to use the Serial through USB.
does not work for my needs, i need all digital pins as i am making a bluetooth controller robot.....and using motor driver shield v1 which uses almost all digital pins except 0,1,2,10,9
Your code is spitting out 5 bytes as fast as possible and on the receiving end you print what you get but the baud rate for printing out is the same as the reception so your buffers probably end up full and blocking code / missing data
Does the code work if you do this?
void loop()
{
bt.write("12333");
delay(500);
}
Also change the Serial baud rate to 115200 on the receiver side
please, please don't post pictures of text... Not only they are not readable nor usable directly for copy&paste but they use up lots of storage and internet bandwidth which contributes to polluting the planet.
well you send the message only twice per second.
Also readString has a builtin timeout at 1s so that's what you wait for.
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
Servo servo1;
Servo servo2;
Servo servoClaw;
String command = "";
void setup()
{
// Set initial speed of the motors & stop
motor1.setSpeed(200);
motor1.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
motor3.setSpeed(200);
motor3.run(RELEASE);
motor4.setSpeed(200);
motor4.run(RELEASE);
servo1.attach(9); // Attach servo1 to pin 9
servo2.attach(6); // Attach servo2 to pin 6
servoClaw.attach(5); // Attach servoClaw to pin 5
Serial.begin(9600); // Initialize hardware serial communication
}
void loop()
{
if (Serial.available())
{
char c = Serial.read();
// Check for end of command
if (c == '\n')
{
processCommand(command);
command = ""; // Clear the command string
}
else
{
command += c; // Append character to command string
}
}
delay(100);
}
void processCommand(const String& command)
{
Serial.print("Received command: ");
Serial.println(command);
// Compare the received command with predefined commands and take action accordingly
if (command.equals("MF"))
{
moveForward();
}
else if (command.equals("MR"))
{
moveRight();
}
else if (command.equals("MB"))
{
moveBackward();
}
else if (command.equals("ML"))
{
moveLeft();
}
else if (command.equals("serleft"))
{
moveLeftServo();
}
else if (command.equals("serright"))
{
moveRightServo();
}
else if (command.equals("serup"))
{
moveUpServo();
}
else if (command.equals("serdown"))
{
moveDownServo();
}
else if (command.equals("serclaw"))
{
openCloseClaw();
}
else
{
stopMotors();
}
}
void moveForward()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
Serial.println("Moving forward");
}
void moveRight()
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
Serial.println("Moving right");
}
void moveBackward()
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
Serial.println("Moving backward");
}
void moveLeft()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
Serial.println("Moving left");
}
void moveLeftServo()
{
int pos = servo1.read();
if (pos < 180)
{
pos += 5;
servo1.write(pos);
}
Serial.println("Moving left servo");
}
void moveRightServo()
{
int pos = servo1.read();
if (pos > 0)
{
pos -= 5;
servo1.write(pos);
}
Serial.println("Moving right servo");
}
void moveUpServo()
{
int pos = servo2.read();
if (pos < 180)
{
pos += 5;
servo2.write(pos);
}
Serial.println("Moving up servo");
}
void moveDownServo()
{
int pos = servo2.read();
if (pos > 0)
{
pos -= 5;
servo2.write(pos);
}
Serial.println("Moving down servo");
}
void openCloseClaw()
{
int pos = servoClaw.read();
if (pos == 0)
{
servoClaw.write(90);
Serial.println("Opening claw");
}
else
{
servoClaw.write(0);
Serial.println("Closing claw");
}
}
void stopMotors()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
Serial.println("Stopping motors");
}
which is needed to work with this transmitter code
#include <SoftwareSerial.h>
SoftwareSerial bluetoothSerial(8, 7); // RX, TX pins of HC-05
const int joystick1XPin = A0; // Analog input pin for Joystick 1 X-axis
const int joystick1YPin = A1; // Analog input pin for Joystick 1 Y-axis
const int joystick2XPin = A2; // Analog input pin for Joystick 2 X-axis
const int joystick2YPin = A3; // Analog input pin for Joystick 2 Y-axis
const int joystickButtonPin = 2; // Digital input pin for Joystick button
const int joystickThreshold = 50; // Threshold value to consider joystick movement
void setup()
{
Serial.begin(9600);
bluetoothSerial.begin(9600); // Initialize Bluetooth serial communication
pinMode(joystickButtonPin, INPUT_PULLUP); // Set the joystick button pin as INPUT_PULLUP
}
void loop()
{
int joystick1XValue = analogRead(joystick1XPin);
int joystick1YValue = analogRead(joystick1YPin);
int joystick2XValue = analogRead(joystick2XPin);
int joystick2YValue = analogRead(joystick2YPin);
int joystickButtonValue = digitalRead(joystickButtonPin);
int joystick1XCommand = getJoystickCommand(joystick1XValue);
int joystick1YCommand = getJoystickCommand(joystick1YValue);
int joystick2XCommand = getJoystickCommand(joystick2XValue);
int joystick2YCommand = getJoystickCommand(joystick2YValue);
// Print the raw joystick values to the serial monitor
//Serial.print("Joystick 1 X: ");
//Serial.print(joystick1XValue);
//Serial.print("\tJoystick 1 Y: ");
//Serial.print(joystick1YValue);
//Serial.print("\tJoystick 2 X: ");
//Serial.print(joystick2XValue);
//Serial.print("\tJoystick 2 Y: ");
//Serial.println(joystick2YValue);
// Print the joystick commands to the serial monitor
//Serial.print("Joystick 1 X Command: ");
//Serial.print(joystick1XCommand);
//Serial.print("\tJoystick 1 Y Command: ");
//Serial.print(joystick1YCommand);
//Serial.print("\tJoystick 2 X Command: ");
//Serial.print(joystick2XCommand);
//Serial.print("\tJoystick 2 Y Command: ");
//Serial.println(joystick2YCommand);
// Check Joystick 1 movement
if (joystick1XCommand == -1)
{
Serial.println("Move Right");
sendBluetoothCommand("MR"); // Move Right
}
else if (joystick1XCommand == 1)
{
Serial.println("Move Left");
sendBluetoothCommand("ML"); // Move Left
}
else if (joystick1YCommand == -1)
{
Serial.println("Move Backward");
sendBluetoothCommand("MB"); // Move Backward
}
else if (joystick1YCommand == 1)
{
Serial.println("Move Forward");
sendBluetoothCommand("MF"); // Move Forward
}
else
{
Serial.println("STOP");
sendBluetoothCommand("gasd");
}
// Check Joystick 2 movement
if (joystick2XCommand == -1)
{
Serial.println("Left Servo Movement");
sendBluetoothCommand("serleft"); // Left servo movement
}
else if (joystick2XCommand == 1)
{
Serial.println("Right Servo Movement");
sendBluetoothCommand("serright"); // Right servo movement
}
else if (joystick2YCommand == -1)
{
Serial.println("Up Servo Movement");
sendBluetoothCommand("serup"); // Up servo movement
}
else if (joystick2YCommand == 1)
{
Serial.println("Down Servo Movement");
sendBluetoothCommand("serdown"); // Down servo movement
}
// Check Joystick button
if (joystickButtonValue == LOW)
{
Serial.println("Joystick Button Pressed");
sendBluetoothCommand("serclaw"); // Claw movement
}
delay(100); // Delay for 1 second
}
int getJoystickCommand(int value)
{
if (value < (512 - joystickThreshold))
{
return -1; // Negative command for left or up movement
}
else if (value > (512 + joystickThreshold))
{
return 1; // Positive command for right or down movement
}
else
{
return 0; // No movement
}
}
void sendBluetoothCommand(const char* command)
{
bluetoothSerial.print(command);
}
you need the sender to send an end marker, so that you know the message is complete. you can make that a line feed '\n' or carriage return '\r' or both
with both, it can be achieve simply with println() instead of print()