0% found this document useful (0 votes)
12 views

Arduino

This Arduino project controls a 4-wheel drive Bluetooth car. The car is powered by a battery and can be controlled via Bluetooth using an Arduino app. The Arduino code uses functions to control motor speed and direction to allow the car to move forward, backward, left, right, and stop based on Bluetooth commands.

Uploaded by

tristannerzs0426
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Arduino

This Arduino project controls a 4-wheel drive Bluetooth car. The car is powered by a battery and can be controlled via Bluetooth using an Arduino app. The Arduino code uses functions to control motor speed and direction to allow the car to move forward, backward, left, right, and stop based on Bluetooth commands.

Uploaded by

tristannerzs0426
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Arduino 4-wheel drive Bluetooth controlled car

Featues:
Battery Powered
Bluetooth control via Arduino App

Code used:
#include <Arduino.h>

int MOTORLEFT[2] = {2, 3};


int MOTORRIGHT[2] = {4, 5};
unsigned long millisflag;
boolean ismoving = false;
int baseSpeed = 255; // Base speed for forward and backward movement
float turnSpeedMultiplier = 0.5; // Speed multiplier for turning (adjust as needed)

void setup() {
Serial.begin(9600);
Serial.setTimeout(50);
for (int i = 0; i < 2; i++) {
pinMode(MOTORLEFT[i], OUTPUT);
pinMode(MOTORRIGHT[i], OUTPUT);
}
}

void loop() {
if (Serial.available()) {
String Serialdata = Serial.readString();
if (Serialdata.indexOf('W') > -1) {
Serial.println("forward");
moveForward();
ismoving = true;
millisflag = millis();
}
else if (Serialdata.indexOf("S") > -1) {
Serial.println("backward");
moveBackward();
ismoving = true;
millisflag = millis();
}
else if (Serialdata.indexOf("D") > -1) {
Serial.println("right");
turnRight();
ismoving = true;
millisflag = millis();
}
else if (Serialdata.indexOf("A") > -1) {
Serial.println("left");
turnLeft();
ismoving = true;
millisflag = millis();
}
else if (Serialdata.indexOf("F") > -1) {
Serial.println("stop");
moveStop();
ismoving = true;
millisflag = millis();
}

delay(200);
}

if (((millis() - millisflag) > 200) && ismoving) {


moveStop();
ismoving = false;
}
}

void moveStop() {
for (int i = 0; i < 2; i++) {
digitalWrite(MOTORRIGHT[i], LOW);
digitalWrite(MOTORLEFT[i], LOW);
}
}

void moveForward() {
analogWrite(MOTORRIGHT[0], baseSpeed);
analogWrite(MOTORLEFT[0], baseSpeed);
digitalWrite(MOTORRIGHT[1], LOW);
digitalWrite(MOTORLEFT[1], LOW);
}

void moveBackward() {
digitalWrite(MOTORRIGHT[0], LOW);
digitalWrite(MOTORLEFT[0], LOW);
analogWrite(MOTORRIGHT[1], baseSpeed);
analogWrite(MOTORLEFT[1], baseSpeed);
}

void turnLeft() {
analogWrite(MOTORRIGHT[0], baseSpeed * turnSpeedMultiplier);
digitalWrite(MOTORLEFT[0], LOW);
digitalWrite(MOTORRIGHT[1], LOW);
analogWrite(MOTORLEFT[1], baseSpeed * turnSpeedMultiplier);
}

void turnRight() {
digitalWrite(MOTORRIGHT[0], LOW);
analogWrite(MOTORLEFT[0], baseSpeed * turnSpeedMultiplier);
analogWrite(MOTORRIGHT[1], baseSpeed * turnSpeedMultiplier);
digitalWrite(MOTORLEFT[1], LOW);
}

Diagram:

You might also like