Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: # Bluetooth Controller
- - Version: 001
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2026-03-12 12:50:57
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Receive movement commands via HC-05 Bluetooth */
- /* module (Forward, Backward, Left, Right, Stop) */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Control 4 DC motors (Motor1, Motor2, Motor3, */
- /* Motor4) via motor driver based on Bluetooth */
- /* commands */
- /****** SYSTEM REQUIREMENT 3 *****/
- /* Forward movement: all 4 motors rotate in same */
- /* direction at full speed */
- /****** SYSTEM REQUIREMENT 4 *****/
- /* Backward movement: all 4 motors rotate in opposite */
- /* direction at full speed */
- /****** SYSTEM REQUIREMENT 5 *****/
- /* Left movement: left-side motors reverse, right- */
- /* side motors forward for strafing motion */
- /****** SYSTEM REQUIREMENT 6 *****/
- /* Right movement: right-side motors reverse, left- */
- /* side motors forward for strafing motion */
- /****** SYSTEM REQUIREMENT 7 *****/
- /* Stop command: halt all motors immediately */
- /****** SYSTEM REQUIREMENT 8 *****/
- /* Variable speed control: adjust motor speed based */
- /* on Bluetooth data (0-255 PWM) */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
- #include <SoftwareSerial.h> // For HC-05 Bluetooth communication
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeMotorPins(void);
- void initializeBluetooth(void);
- void processBluetooth(void);
- void moveForward(uint8_t speed);
- void moveBackward(uint8_t speed);
- void moveLeft(uint8_t speed);
- void moveRight(uint8_t speed);
- void stopAllMotors(void);
- void setMotorSpeed(uint8_t motor, uint8_t direction, uint8_t speed);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Motor1_PushButton_PIN_A0 = A0;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Motor4_Potentiometer_Vout_PIN_A5 = A5;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Motor2_LED_PIN_A1 = A1;
- const uint8_t Motor3_LEDRGB_Red_PIN_A2 = A2;
- const uint8_t Motor3_LEDRGB_Green_PIN_A3 = A3;
- const uint8_t Motor3_LEDRGB_Blue_PIN_A4 = A4;
- /****** MOTOR DRIVER PIN DEFINITIONS FOR ARDUINO UNO *****/
- // Motor 1 (Left-Front): Pins 2 (Direction) and 3 (PWM Speed)
- const uint8_t MOTOR1_DIR_PIN = 2;
- const uint8_t MOTOR1_PWM_PIN = 3;
- // Motor 2 (Right-Front): Pins 4 (Direction) and 5 (PWM Speed)
- const uint8_t MOTOR2_DIR_PIN = 4;
- const uint8_t MOTOR2_PWM_PIN = 5;
- // Motor 3 (Left-Rear): Pins 6 (Direction) and 9 (PWM Speed)
- const uint8_t MOTOR3_DIR_PIN = 6;
- const uint8_t MOTOR3_PWM_PIN = 9;
- // Motor 4 (Right-Rear): Pins 7 (Direction) and 10 (PWM Speed)
- const uint8_t MOTOR4_DIR_PIN = 7;
- const uint8_t MOTOR4_PWM_PIN = 10;
- /****** HC-05 BLUETOOTH MODULE PIN DEFINITIONS *****/
- // RX pin on Arduino (connects to TX on HC-05)
- const uint8_t HC05_RX_PIN = 8;
- // TX pin on Arduino (connects to RX on HC-05)
- const uint8_t HC05_TX_PIN = 11;
- /****** MOTOR DIRECTION CONSTANTS *****/
- const uint8_t MOTOR_FORWARD = HIGH;
- const uint8_t MOTOR_BACKWARD = LOW;
- /****** BLUETOOTH COMMAND CONSTANTS *****/
- const char CMD_FORWARD = 'F';
- const char CMD_BACKWARD = 'B';
- const char CMD_LEFT = 'L';
- const char CMD_RIGHT = 'R';
- const char CMD_STOP = 'S';
- const char CMD_SPEED_SEPARATOR = ':';
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- // SoftwareSerial for HC-05 Bluetooth module (RX, TX)
- SoftwareSerial bluetoothSerial(HC05_RX_PIN, HC05_TX_PIN);
- /****** GLOBAL VARIABLES *****/
- uint8_t currentSpeed = 255; // Default speed is maximum (255)
- char lastCommand = 'S'; // Last received command (default: Stop)
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(9600);
- // Initialize motor control pins
- initializeMotorPins();
- // Initialize Bluetooth communication
- initializeBluetooth();
- // Initialize pushbutton pin
- pinMode(Motor1_PushButton_PIN_A0, INPUT_PULLUP);
- // Initialize potentiometer pin
- pinMode(Motor4_Potentiometer_Vout_PIN_A5, INPUT);
- // Initialize LED pins
- pinMode(Motor2_LED_PIN_A1, OUTPUT);
- pinMode(Motor3_LEDRGB_Red_PIN_A2, OUTPUT);
- pinMode(Motor3_LEDRGB_Green_PIN_A3, OUTPUT);
- pinMode(Motor3_LEDRGB_Blue_PIN_A4, OUTPUT);
- // Turn on status LED (Green) to indicate system ready
- digitalWrite(Motor3_LEDRGB_Green_PIN_A3, HIGH);
- // Stop all motors initially
- stopAllMotors();
- Serial.println("Robot Control System Initialized");
- Serial.println("Waiting for Bluetooth commands...");
- }
- void loop(void)
- {
- // Process incoming Bluetooth commands
- processBluetooth();
- // Small delay to prevent overwhelming the processor
- delay(10);
- }
- /****** MOTOR INITIALIZATION FUNCTION *****/
- void initializeMotorPins(void)
- {
- // Set all motor direction pins as outputs
- pinMode(MOTOR1_DIR_PIN, OUTPUT);
- pinMode(MOTOR2_DIR_PIN, OUTPUT);
- pinMode(MOTOR3_DIR_PIN, OUTPUT);
- pinMode(MOTOR4_DIR_PIN, OUTPUT);
- // Set all motor speed (PWM) pins as outputs
- pinMode(MOTOR1_PWM_PIN, OUTPUT);
- pinMode(MOTOR2_PWM_PIN, OUTPUT);
- pinMode(MOTOR3_PWM_PIN, OUTPUT);
- pinMode(MOTOR4_PWM_PIN, OUTPUT);
- // Initialize all motor control pins to LOW
- digitalWrite(MOTOR1_DIR_PIN, LOW);
- digitalWrite(MOTOR2_DIR_PIN, LOW);
- digitalWrite(MOTOR3_DIR_PIN, LOW);
- digitalWrite(MOTOR4_DIR_PIN, LOW);
- analogWrite(MOTOR1_PWM_PIN, 0);
- analogWrite(MOTOR2_PWM_PIN, 0);
- analogWrite(MOTOR3_PWM_PIN, 0);
- analogWrite(MOTOR4_PWM_PIN, 0);
- }
- /****** BLUETOOTH INITIALIZATION FUNCTION *****/
- void initializeBluetooth(void)
- {
- // Initialize SoftwareSerial for HC-05 Bluetooth module
- // HC-05 operates at 9600 baud rate by default
- bluetoothSerial.begin(9600);
- }
- /****** BLUETOOTH COMMAND PROCESSING FUNCTION *****/
- void processBluetooth(void)
- {
- // Check if data is available from Bluetooth
- if (bluetoothSerial.available())
- {
- // Read the incoming command
- char command = bluetoothSerial.read();
- // Log received command
- Serial.print("Command received: ");
- Serial.println(command);
- // Parse command and extract speed if provided
- uint8_t receivedSpeed = currentSpeed;
- // Check if speed data is attached (format: "F:200" for Forward at speed 200)
- if (bluetoothSerial.available() && bluetoothSerial.peek() == CMD_SPEED_SEPARATOR)
- {
- bluetoothSerial.read(); // Consume the ':' separator
- receivedSpeed = 0;
- // Read numeric speed value
- while (bluetoothSerial.available() && isDigit(bluetoothSerial.peek()))
- {
- receivedSpeed = (receivedSpeed * 10) + (bluetoothSerial.read() - '0');
- }
- // Constrain speed to valid PWM range (0-255)
- if (receivedSpeed > 255)
- {
- receivedSpeed = 255;
- }
- currentSpeed = receivedSpeed;
- Serial.print("Speed set to: ");
- Serial.println(currentSpeed);
- }
- // Execute movement command based on received command
- switch (command)
- {
- case CMD_FORWARD:
- moveForward(currentSpeed);
- lastCommand = CMD_FORWARD;
- digitalWrite(Motor3_LEDRGB_Green_PIN_A3, HIGH);
- digitalWrite(Motor3_LEDRGB_Red_PIN_A2, LOW);
- Serial.println("Movement: FORWARD");
- break;
- case CMD_BACKWARD:
- moveBackward(currentSpeed);
- lastCommand = CMD_BACKWARD;
- digitalWrite(Motor3_LEDRGB_Red_PIN_A2, HIGH);
- digitalWrite(Motor3_LEDRGB_Green_PIN_A3, LOW);
- Serial.println("Movement: BACKWARD");
- break;
- case CMD_LEFT:
- moveLeft(currentSpeed);
- lastCommand = CMD_LEFT;
- digitalWrite(Motor3_LEDRGB_Blue_PIN_A4, HIGH);
- digitalWrite(Motor3_LEDRGB_Green_PIN_A3, LOW);
- Serial.println("Movement: LEFT");
- break;
- case CMD_RIGHT:
- moveRight(currentSpeed);
- lastCommand = CMD_RIGHT;
- digitalWrite(Motor3_LEDRGB_Blue_PIN_A4, HIGH);
- digitalWrite(Motor3_LEDRGB_Green_PIN_A3, LOW);
- Serial.println("Movement: RIGHT");
- break;
- case CMD_STOP:
- stopAllMotors();
- lastCommand = CMD_STOP;
- digitalWrite(Motor3_LEDRGB_Green_PIN_A3, LOW);
- digitalWrite(Motor3_LEDRGB_Red_PIN_A2, LOW);
- digitalWrite(Motor3_LEDRGB_Blue_PIN_A4, LOW);
- Serial.println("Movement: STOP");
- break;
- default:
- // Unknown command - do nothing
- Serial.print("Unknown command: ");
- Serial.println(command);
- break;
- }
- }
- }
- /****** FORWARD MOVEMENT FUNCTION *****/
- /* SYSTEM REQUIREMENT 3: Forward movement: all 4 motors */
- /* rotate in same direction at full speed */
- void moveForward(uint8_t speed)
- {
- // Motor 1 (Left-Front): Forward
- setMotorSpeed(1, MOTOR_FORWARD, speed);
- // Motor 2 (Right-Front): Forward
- setMotorSpeed(2, MOTOR_FORWARD, speed);
- // Motor 3 (Left-Rear): Forward
- setMotorSpeed(3, MOTOR_FORWARD, speed);
- // Motor 4 (Right-Rear): Forward
- setMotorSpeed(4, MOTOR_FORWARD, speed);
- }
- /****** BACKWARD MOVEMENT FUNCTION *****/
- /* SYSTEM REQUIREMENT 4: Backward movement: all 4 motors */
- /* rotate in opposite direction at full speed */
- void moveBackward(uint8_t speed)
- {
- // Motor 1 (Left-Front): Backward
- setMotorSpeed(1, MOTOR_BACKWARD, speed);
- // Motor 2 (Right-Front): Backward
- setMotorSpeed(2, MOTOR_BACKWARD, speed);
- // Motor 3 (Left-Rear): Backward
- setMotorSpeed(3, MOTOR_BACKWARD, speed);
- // Motor 4 (Right-Rear): Backward
- setMotorSpeed(4, MOTOR_BACKWARD, speed);
- }
- /****** LEFT MOVEMENT FUNCTION *****/
- /* SYSTEM REQUIREMENT 5: Left movement: left-side motors */
- /* reverse, right-side motors forward for strafing motion */
- void moveLeft(uint8_t speed)
- {
- // Motor 1 (Left-Front): Backward (reverse)
- setMotorSpeed(1, MOTOR_BACKWARD, speed);
- // Motor 2 (Right-Front): Forward
- setMotorSpeed(2, MOTOR_FORWARD, speed);
- // Motor 3 (Left-Rear): Backward (reverse)
- setMotorSpeed(3, MOTOR_BACKWARD, speed);
- // Motor 4 (Right-Rear): Forward
- setMotorSpeed(4, MOTOR_FORWARD, speed);
- }
- /****** RIGHT MOVEMENT FUNCTION *****/
- /* SYSTEM REQUIREMENT 6: Right movement: right-side motors */
- /* reverse, left-side motors forward for strafing motion */
- void moveRight(uint8_t speed)
- {
- // Motor 1 (Left-Front): Forward
- setMotorSpeed(1, MOTOR_FORWARD, speed);
- // Motor 2 (Right-Front): Backward (reverse)
- setMotorSpeed(2, MOTOR_BACKWARD, speed);
- // Motor 3 (Left-Rear): Forward
- setMotorSpeed(3, MOTOR_FORWARD, speed);
- // Motor 4 (Right-Rear): Backward (reverse)
- setMotorSpeed(4, MOTOR_BACKWARD, speed);
- }
- /****** STOP ALL MOTORS FUNCTION *****/
- /* SYSTEM REQUIREMENT 7: Stop command: halt all motors */
- /* immediately */
- void stopAllMotors(void)
- {
- // Stop all motors by setting speed to 0
- analogWrite(MOTOR1_PWM_PIN, 0);
- analogWrite(MOTOR2_PWM_PIN, 0);
- analogWrite(MOTOR3_PWM_PIN, 0);
- analogWrite(MOTOR4_PWM_PIN, 0);
- }
- /****** SET MOTOR SPEED FUNCTION *****/
- /* SYSTEM REQUIREMENT 8: Variable speed control: adjust */
- /* motor speed based on Bluetooth data (0-255 PWM) */
- void setMotorSpeed(uint8_t motor, uint8_t direction, uint8_t speed)
- {
- // Ensure speed is within valid PWM range
- if (speed > 255)
- {
- speed = 255;
- }
- // Set direction and speed based on motor number
- switch (motor)
- {
- case 1: // Motor 1 (Left-Front)
- digitalWrite(MOTOR1_DIR_PIN, direction);
- analogWrite(MOTOR1_PWM_PIN, speed);
- break;
- case 2: // Motor 2 (Right-Front)
- digitalWrite(MOTOR2_DIR_PIN, direction);
- analogWrite(MOTOR2_PWM_PIN, speed);
- break;
- case 3: // Motor 3 (Left-Rear)
- digitalWrite(MOTOR3_DIR_PIN, direction);
- analogWrite(MOTOR3_PWM_PIN, speed);
- break;
- case 4: // Motor 4 (Right-Rear)
- digitalWrite(MOTOR4_DIR_PIN, direction);
- analogWrite(MOTOR4_PWM_PIN, speed);
- break;
- default:
- // Invalid motor number - do nothing
- Serial.print("Invalid motor number: ");
- Serial.println(motor);
- break;
- }
- }
- /* END CODE */
Advertisement