08-12-2024 06:56 AM
I am trying to control 4 dc motors for a car using L293D motor shield for Arduino Uno.I have some experience with Labview,but I am new with Arduino coding and LINX.I am trying to make 4 buttons(forward,backward,right,left) that when held on,the car will move accordingly.Because I am not sure how to use Digital Write N chan (since I want to have 4 commands),I tried using Digital Write 1 Channel and tried testing with only 1 button,but it didn't work out.I dont know if there is an issue with my Arduino Code,the blocks I am using on Labview or both.Below I show the block diagram and the Arduino code.
#include <AFMotor.h>
AF_DCMotor motor1(1); // Front-left motor
AF_DCMotor motor2(2); // Front-right motor
AF_DCMotor motor3(3); // Back-left motor
AF_DCMotor motor4(4); // Back-right motor
void setup() {
Serial.begin(9600); // Set the baud rate to match LabVIEW
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
moveLeft();
break;
case 'R':
moveRight();
break;
case 'S': // Stop command
stopMotors();
break;
default:
stopMotors(); // Default to stopping if an unknown command is received
break;
}
}
}
void moveForward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void moveBackward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void moveRight() {
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
void moveLeft() {
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
void stopMotors() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}