0% found this document useful (0 votes)
21 views2 pages

Pins For Motor Driver

Uploaded by

ramindu.jiat
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)
21 views2 pages

Pins For Motor Driver

Uploaded by

ramindu.jiat
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
You are on page 1/ 2

// Pins for Motor Driver

const int ENA = 5; // Speed control for Motor A


const int IN1 = 6; // Direction control for Motor A
const int IN2 = 7; // Direction control for Motor A

const int ENB = 10; // Speed control for Motor B


const int IN3 = 8; // Direction control for Motor B
const int IN4 = 9; // Direction control for Motor B

// Bluetooth Module Serial Communication


char command;

void setup() {
// Set Motor Driver pins as output
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

// Initialize serial communication for Bluetooth


Serial.begin(9600);
}

void loop() {
// Read the command sent via Bluetooth
if (Serial.available()) {
command = Serial.read();
controlMotors(command);
}
}

// Function to control motors based on the command


void controlMotors(char command) {
switch (command) {
case 'F': // Move Forward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 150); // Adjust speed (0-255)

digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 150); // Adjust speed (0-255)
break;

case 'B': // Move Backward


digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 150);

digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 150);
break;

case 'L': // Turn Left


digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 150);

digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 150);
break;

case 'R': // Turn Right


digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 150);

digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 150);
break;

case 'S': // Stop


digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);

digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0);
break;

default: // Do nothing
break;
}
}

You might also like