Bluetooth-Controlled Car Project

1. Components Required

2. Wiring Instructions

Follow the wiring instructions to correctly assemble your Bluetooth-controlled car:

4. Arduino Code

#include AF_DCMotor motor1(1); // Motor 1 AF_DCMotor motor2(2); // Motor 2 AF_DCMotor motor3(3); // Motor 3 AF_DCMotor motor4(4); // Motor 4 char command; void setup() { Serial.begin(9600); // Set baud rate for Bluetooth module Serial.println("Bluetooth-Controlled Car Ready"); } void loop() { if (Serial.available()) { command = Serial.read(); switch (command) { case 'F': moveForward(); break; case 'B': moveBackward(); break; case 'L': turnLeft(); break; case 'R': turnRight(); break; case 'S': stopMotors(); break; } } } void moveForward() { motor1.setSpeed(255); motor2.setSpeed(255); motor3.setSpeed(255); motor4.setSpeed(255); motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); } void moveBackward() { motor1.setSpeed(255); motor2.setSpeed(255); motor3.setSpeed(255); motor4.setSpeed(255); motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); } void turnLeft() { motor1.setSpeed(150); motor2.setSpeed(150); motor3.setSpeed(255); motor4.setSpeed(255); motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(FORWARD); motor4.run(FORWARD); } void turnRight() { motor1.setSpeed(255); motor2.setSpeed(255); motor3.setSpeed(150); motor4.setSpeed(150); motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); } void stopMotors() { motor1.run(RELEASE); motor2.run(RELEASE); motor3.run(RELEASE); motor4.run(RELEASE); }

5. Flutter Installation on Windows

  1. Install Git from Git's official website.
  2. Download and extract the Flutter SDK from Flutter's website.
  3. Add the Flutter bin folder to your system's PATH.
  4. Run flutter doctor in the terminal to check for any missing dependencies.

Add Dependency

Add the flutter_blue_plus package to your pubspec.yaml: dependencies: flutter_blue_plus: ^1.2.0

5. Flutter Code

import 'package:flutter/material.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; class BluetoothControlApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: BluetoothControlPage(), ); } } class BluetoothControlPage extends StatefulWidget { @override _BluetoothControlPageState createState() => _BluetoothControlPageState(); } class _BluetoothControlPageState extends State { FlutterBluePlus flutterBlue = FlutterBluePlus.instance; BluetoothDevice? connectedDevice; BluetoothCharacteristic? writeCharacteristic; @override void initState() { super.initState(); scanAndConnect(); } void scanAndConnect() async { flutterBlue.startScan(timeout: Duration(seconds: 5)); flutterBlue.scanResults.listen((results) { for (ScanResult result in results) { if (result.device.name == 'HC-05') { setState(() { connectedDevice = result.device; }); connectToDevice(result.device); flutterBlue.stopScan(); break; } } }); } void connectToDevice(BluetoothDevice device) async { await device.connect(); List services = await device.discoverServices(); for (BluetoothService service in services) { for (BluetoothCharacteristic characteristic in service.characteristics) { if (characteristic.properties.write) { writeCharacteristic = characteristic; break; } } } } void sendCommand(String command) { if (writeCharacteristic != null) { writeCharacteristic!.write(command.codeUnits); // Send the command to the HC-05 module } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Bluetooth RC Car Controller")), body: connectedDevice == null ? Center(child: Text("Scanning for HC-05...")) : Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () => sendCommand('F'), child: Text("Forward"), ), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () => sendCommand('L'), child: Text("Left"), ), SizedBox(width: 10), ElevatedButton( onPressed: () => sendCommand('S'), child: Text("Stop"), ), SizedBox(width: 10), ElevatedButton( onPressed: () => sendCommand('R'), child: Text("Right"), ), ], ), ElevatedButton( onPressed: () => sendCommand('B'), child: Text("Backward"), ), ], ), ); } } void main() => runApp(BluetoothControlApp());

6. Required Permissions

Add the following permissions to your app's AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>