Optimized Code for Bluetooth Controlled Car
To optimize the original code, you can make the following improvements.
Remove redundant speed values
The code currently has multiple else if statements that set the speed to 150. You can remove these redundant statements.
Consolidate similar actions
There are two sets of conditions related to lights and backlights ('W' and 'w', 'U' and 'U'). Since the actions for these conditions are commented out, you can remove these conditions altogether.
Combine the two play_happy_birthday conditions
The conditions 'X' and 'x' both call the play_happy_birthday function. You can combine these conditions into a single one.
Here's the optimized version of the code:
Code Explanation
The first line includes the "pitches.h" header file, which likely contains definitions for musical note frequencies.
#include "pitches.h"
#define enB 8
#define in4 9
#define in3 10
#define in2 11
#define in1 12
#define enA 13
#define buzzerpin 2
The above lines define the pin numbers for various components used in the code, such as motor drivers and the buzzer.
char btsignal;
bool reversing = false;
unsigned long previousMillis = 0;
const long interval = 100;
The above lines declare variables used in the code. btsignal stores the Bluetooth signal received. reversing is a flag indicating whether the car is in reverse mode. previousMillis keeps track of the time since the last action. interval determines the interval at which certain actions are performed.
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(buzzerpin, OUTPUT);
set_speed(150);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
Serial.begin(9600);
}
The setup() function is called once at the beginning of the program. These lines set the pin modes for motor drivers, buzzer, and LEDs. The set_speed(150) function call sets the initial speed to 150 (can be modified as needed). The following digitalWrite() calls set the initial state of the motor control pins. Serial.begin(9600) initializes serial communication at a baud rate of 9600.
void loop() {
if (Serial.available()) {
btsignal = Serial.read();
}
The loop() function is where the main program logic resides. It runs repeatedly. This code block checks if there is serial data available and reads it into the btsignal variable.
if (btsignal == 'F') {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
else if (btsignal == 'B') {
reversing = true;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
else if (btsignal == 'L') {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
else if (btsignal == 'R') {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
These conditions check the value of btsignal and perform corresponding actions. If btsignal is 'F', the car moves forward. Motor control pins are set accordingly. If btsignal is 'B', the car moves backward. Reversing flag is set, and motor control pins are adjusted. If btsignal is 'L', the car turns left. Motor control pins are set accordingly. If btsignal is 'R', the car turns right. Motor control pins are adjusted.
else if (btsignal >= '0' && btsignal <= '9') {
set_speed(150 + (btsignal - '0') * 10);
}
else if (btsignal == 'q') {
set_speed(255);
}
These conditions handle speed control. If btsignal is a digit from '0' to '9', the speed is adjusted based on the value. The speed increment is calculated by subtracting '0' from btsignal and multiplying by 10. If btsignal is 'q', the maximum speed of 255 is set.
else if (btsignal == 'W') {
//frontlight = true;
}
else if (btsignal == 'w') {
//frontlight = false;
}
else if (btsignal == 'V' || btsignal == 'v') {
tone(buzzerpin, 2000, 1000);
}
else if (btsignal == 'U') {
//backlight = true;
}
else if (btsignal == 'u') {
//backlight = false;
}
else if (btsignal == 'X' || btsignal == 'x') {
play_happy_birthday();
}
else {
reversing = false;
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
}
These conditions handle other control signals. 'W' and 'w' may control front lights (commented out in the code). 'V' and 'v' play a tone on the buzzer. 'U' and 'u' may control backlighting (commented out in the code). 'X' and 'x' trigger the play_happy_birthday() function to play the "Happy Birthday" song. If none of the conditions match, the car stops, and the motor control pins are set to stop the motors.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (reversing) {
tone(buzzerpin, 5000, interval);
}
}
}
These lines handle the reversing tone. The elapsed time since the last action is calculated using millis(). If the elapsed time reaches the specified interval, the reversing tone is played on the buzzer. The reversing tone is only played if the reversing flag is set to true.
void set_speed(int speed) {
analogWrite(enA, speed);
analogWrite(enB, speed);
}
This function sets the speed of the motors by adjusting the PWM values of the motor control pins.
void play_happy_birthday() {
// Melody and timing arrays for the "Happy Birthday" song are defined here
// (not shown for brevity)
// ...
}
This function plays the "Happy Birthday" song using predefined melody and timing arrays.
At Troniction, we hope this breakdown helps you understand the optimized code better!
Unlock the Secrets: Get Your Copy Now!
You can download everything you need know to build your own Arduino-based electronic car as a PDF file for your reference.
Troniction Book