0% found this document useful (0 votes)
35 views4 pages

Arduino Bluetooth Car Control Code

This code controls a Bluetooth-enabled car. It uses an L293 motor driver and defines pins to control two motors independently. The car's movement is controlled by reading serial input and mapping single character commands like "F" for forward and "S" for stop to specific pin configurations and PWM signals to drive the motors in different combinations for motions like forward, backward, left, and right. Speed is also adjustable from 0-255 based on another serial input character.

Uploaded by

BLOOD SUCKERS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Arduino Bluetooth Car Control Code

This code controls a Bluetooth-enabled car. It uses an L293 motor driver and defines pins to control two motors independently. The car's movement is controlled by reading serial input and mapping single character commands like "F" for forward and "S" for stop to specific pin configurations and PWM signals to drive the motors in different combinations for motions like forward, backward, left, and right. Speed is also adjustable from 0-255 based on another serial input character.

Uploaded by

BLOOD SUCKERS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

* Project: Bluetooth Controlled Car


* [Link]

*/

//L293 Connection
const int motorA1 = 3;
const int motorA2 = 4;
const int motorAspeed = 5;
const int motorB1 = 7;
const int motorB2 = 8;
const int motorBspeed =6;

//Useful Variables
int state;
int vSpeed=200; // Default speed, from 0 to 255

void setup() {
// Set pins as outputs:
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);

// Initialize serial communication at 9600 bits per second:


[Link](9600);
}

void loop() {

if([Link]() > 0){


state = [Link]();
}
[Link](state);
//Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
if (state == '0'){
vSpeed=0;}
else if (state == '1'){
vSpeed=100;}
else if (state == '2'){
vSpeed=180;}
else if (state == '3'){
vSpeed=200;}
else if (state == '4'){
vSpeed=255;}

/***********************Forward****************************/
//If state is equal with letter 'F', car will go forward!
if (state == 'F') {
digitalWrite (motorA1,LOW);
delay(1);
digitalWrite(motorA2,HIGH);
delay(1);

digitalWrite (motorB1,LOW);
delay(1);
digitalWrite(motorB2,HIGH);
analogWrite (motorAspeed, vSpeed);
analogWrite (motorBspeed, vSpeed);
}
/**********************Forward Left************************/
//If state is equal with letter 'I', car will go forward left
else if (state == 'I') {
digitalWrite (motorA1,LOW);
delay(1);
digitalWrite(motorA2,HIGH);
delay(1);

digitalWrite (motorB1,LOW);
delay(1);
digitalWrite(motorB2,HIGH);

analogWrite (motorAspeed, 0);


analogWrite (motorBspeed, vSpeed);
}
/**********************Forward Right************************/
//If state is equal with letter 'G', car will go forward right
else if (state == 'G') {
digitalWrite (motorA1,LOW);
delay(1);
digitalWrite(motorA2,HIGH);
delay(1);

digitalWrite (motorB1,LOW);
delay(1);
digitalWrite(motorB2,HIGH);

analogWrite (motorAspeed, vSpeed);


analogWrite (motorBspeed, 0);
}
/***********************Backward****************************/
//If state is equal with letter 'B', car will go backward
else if (state == 'B') {
digitalWrite (motorA1,HIGH);
delay(1);
digitalWrite(motorA2,LOW);
delay(1);

digitalWrite (motorB1,HIGH);
delay(1);
digitalWrite(motorB2,LOW);

analogWrite (motorAspeed, vSpeed);


analogWrite (motorBspeed, vSpeed);
}
/**********************Backward Left************************/
//If state is equal with letter 'J', car will go backward left
else if (state == 'J') {
digitalWrite (motorA1,HIGH);
delay(1);
digitalWrite(motorA2,LOW);
delay(1);

digitalWrite (motorB1,HIGH);
delay(1);
digitalWrite(motorB2,LOW);

analogWrite (motorAspeed, 0);


analogWrite (motorBspeed, vSpeed);
}
/**********************Backward Right************************/
//If state is equal with letter 'H', car will go backward right
else if (state == 'H') {
digitalWrite (motorA1,HIGH);
delay(1);
digitalWrite(motorA2,LOW);
delay(1);

digitalWrite (motorB1,HIGH);
delay(1);
digitalWrite(motorB2,LOW);

analogWrite (motorAspeed, vSpeed);


analogWrite (motorBspeed, 0);
}
/***************************Left*****************************/
//If state is equal with letter 'L', wheels will turn left
else if (state == 'L') {
digitalWrite (motorA2,HIGH);
delay(1);
digitalWrite(motorA1,LOW);
delay(1);

digitalWrite (motorB2,LOW);
delay(1);
digitalWrite(motorB1,HIGH);

analogWrite (motorAspeed, vSpeed);


analogWrite (motorBspeed, vSpeed);
}
/***************************Right*****************************/
//If state is equal with letter 'R', wheels will turn right
else if (state == 'R') {
digitalWrite (motorA2,LOW);
delay(1);
digitalWrite(motorA1,HIGH);
delay(1);

digitalWrite (motorB2,HIGH);
delay(1);
digitalWrite(motorB1,LOW);

analogWrite (motorAspeed, vSpeed);


analogWrite (motorBspeed, vSpeed);
}

/************************Stop*****************************/
//If state is equal with letter 'S', stop the car
else if (state == 'S'){
analogWrite(motorA1, 0); analogWrite(motorA2, 0);
analogWrite(motorB1, 0); analogWrite(motorB2, 0);
}
}

Common questions

Powered by AI

The setup function initializes the car's operational framework by configuring the motor control pins as outputs, ensuring they are ready to send signals to the motors. It also establishes the serial communication at a baud rate of 9600 bps, essential for receiving commands from a Bluetooth device. This preparation phase is crucial for defining the car's baseline state, ensuring that subsequent commands are executed smoothly without initialization errors .

PWM (Pulse Width Modulation) controls the speed of the motors by varying the duty cycle of the voltage applied. In the code, it is implemented using the 'analogWrite' function, which sends a PWM signal with a specific value (0 to 255) to the motor speed control pins 'motorAspeed' and 'motorBspeed'. The value of 'vSpeed' determines the intensity of the PWM signal, allowing for speed adjustments from stopped (0) to maximum (255). This approach allows smooth and continuous motor speed control .

Delays between digital pin write operations ensure the motor control commands are properly timed and synchronized, reducing the likelihood of synchronization issues and erratic behavior. Without these small delays, the rapid state changes might not register properly with the motors, leading to unreliable operations. The 1-millisecond delay gives the system adequate time to transition states, ensuring the signals driving the motors are applied uniformly across control commands .

The system ensures the car stops effectively by setting all control pins for the motors to a PWM value of 0 when the 'state' variable is 'S'. This action switches off all motor outputs, halting any motion. It instantly removes the voltage to the motors, ensuring an immediate stop rather than a gradual slowdown, which is crucial for precise control in a dynamic environment .

Potential improvements could include implementing error handling for malformed or unexpected inputs to prevent unintended behaviors, optimizing the use of delays or removing them by ensuring synchronization through other methods, adding more nuanced speed control features such as acceleration and deceleration curves for smoother transitions, and enhancing the security of Bluetooth communications. Furthermore, introducing a feedback system, like sensors for obstacle detection or encoders for precise wheel rotations, could improve the car's autonomous capabilities .

The code structure handles varying speed levels through the 'vSpeed' variable, which is adjusted based on 'state' values—ranging from 0 (stop) to 255 (full speed). These speed variations affect the PWM signals sent to the 'motorAspeed' and 'motorBspeed' pins, directly influencing the motors' speed. Changing 'vSpeed' dynamically alters the car's velocity, providing responsive control over movement speed. This capability allows the car to adapt quickly to different situations, such as needing a slower speed for precision or faster speed for covering ground quickly .

The 'state' variable determines the car's movement by storing the last command from serial input. It influences directional commands by triggering specific motor outputs depending on its value. For 'F', it sets both motors to move forward by adjusting the motor pins to LOW and HIGH states respectively and applying a PWM signal with 'vSpeed' to control speed. Each state, such as 'I' for forward left, 'G' for forward right, 'B' for backward, and so forth, dictates a unique pattern of HIGH and LOW signals to the motor pins, ensuring precise directional control .

The design facilitates interaction with user inputs by continuously reading available data from the Serial interface, typically connected to a Bluetooth module. The system captures user commands and updates the 'state' variable to reflect the latest input. This method ensures real-time processing of user commands, allowing the user to dynamically control the car's movement. By efficiently mapping user input to specific motor actions, the system provides a direct and responsive interface for remote control. This setup is significant for maintaining precision and responsiveness in remote operations .

Directional movement logic is implemented by selectively controlling the HIGH and LOW states of the motor control pins. States like 'I' and 'G' facilitate forward left and forward right movements by setting the speed of one motor to zero, allowing the opposite motor to pivot the car in the desired direction. Similarly, 'J' and 'H' control backward turns. This method of asymmetric motor control enables precise turning capabilities, enhancing maneuverability by allowing the car to make sharp or gradual turns depending on motor speed configurations. Such flexibility is crucial for navigating complex environments .

Security considerations include ensuring secure pairing and encryption of Bluetooth communications to prevent unauthorized access or command injection. Implementing authentication mechanisms can verify the identity of users attempting to control the car. Moreover, setting limits on command execution to prevent erroneous inputs can safeguard against potential software or hardware malfunctions caused by insecure commands. Ensuring robust error handling and validation for serial inputs can further mitigate risks from malicious or accidental inputs .

You might also like