Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a Remote Control RC car using Arduino and HC-06 bluetooth module #39

Open
indico774 opened this issue Dec 21, 2024 · 1 comment

Comments

@indico774
Copy link

No description provided.

@indico774
Copy link
Author

#include <softwareserial.h>

// Arduino UNO Rx, Tx
SoftwareSerial hc06(13,12);
String cmd="";

//Motor A or MOTOR 1
int PWMA = 9; //Speed control
int DIRA = 8; //Direction
//Motor B or MOTOR 2
int PWMB = 6; //Speed control
int DIRB = 7; //Direction
//Motor C or MOTOR 3
int PWMC = 5; //Speed control
int DIRC = 4; //Direction
//Motor D or MOTOR 4gggjh
int PWMD = 3; //Speed control
int DIRD = 2; //Direction

void setup() {
//Initialize Serial Monitor
Serial.begin(9600);
//Initialize Bluetooth Serial Port
hc06.begin(9600);

pinMode(PWMA, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRB, OUTPUT);
pinMode(PWMC, OUTPUT);
pinMode(DIRC, OUTPUT);
pinMode(PWMD, OUTPUT);
pinMode(DIRD, OUTPUT);

// Set default at full stop
stopped();
movehalt();
Serial.print("Setup DONE.");
}

void loop() {
//Read data from HC06
while(hc06.available()>0){
cmd+=(char)hc06.read();
}

//Select function with cmd
if(cmd!=""){
cmd.trim(); // Remove added LF in transmit
// We expect Command from bluetooth
if (cmd.equals("F")) {
Serial.println("Vehicle Forward");
forward();
}else if (cmd.equals("B")){
Serial.println("Vehicle Backward");
backward();
}else if(cmd.equals("L")){
Serial.println("Vehicle Turn Left");
turnleft();
}else if(cmd.equals("R")){
Serial.println("Vehicle Turn Right");
turnright();
}else if(cmd.equals("S")){
Serial.println("Vehicle Stop");
stopped();
}else if(cmd.equals("U")){
Serial.println("Crane UP");
moveup();
}else if(cmd.equals("D")){
Serial.println("Crane DOWN");
movedown();
}else if(cmd.equals("H")){
Serial.println("Crane HALT");
movehalt();
}else{
Serial.println("Command UNKNOWN");
stopped();
movehalt();
}

Serial.print("Command recieved:");
Serial.println(cmd);
cmd=""; //reset cmd
}

delay(100);
}

// Function for the vehicle to move FORWARD
void forward() {
analogWrite(PWMA, 255);
digitalWrite (DIRA, HIGH);
analogWrite(PWMB, 255);
digitalWrite (DIRB, HIGH);
}

// Function for the vehicle to move BACKWARD
void backward() {
analogWrite(PWMA, 255);
digitalWrite (DIRA, LOW);
analogWrite(PWMB, 255);
digitalWrite (DIRB, LOW);
}

// Function for the vehicle to turn RIGHT
void turnright() {
analogWrite(PWMA, 255);
digitalWrite (DIRA, HIGH);
analogWrite(PWMB, 0);
digitalWrite (DIRB, HIGH);
}

// Function for the vehicle to turn LEFT
void turnleft() {
analogWrite(PWMA, 0);
digitalWrite (DIRA, HIGH);
analogWrite(PWMB, 255);
digitalWrite (DIRB, HIGH);
}

// Function for the vehicle to STOP
void stopped() {
analogWrite(PWMA, 0);
digitalWrite (DIRA, HIGH);
analogWrite(PWMB, 0);
digitalWrite (DIRB, HIGH);
}

// Function for the crane to move UP
void moveup() {
analogWrite(PWMC, 255);
digitalWrite (DIRC, HIGH);
}

// Function for the crane to move DOWN
void movedown() {
analogWrite(PWMC, 255);
digitalWrite (DIRC, LOW);
}

// Function for the crane to HALT
void movehalt() {
analogWrite(PWMC, 0);
digitalWrite (DIRC, HIGH);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant