Hey guys, I want to run 3 dc motors connected to 2 l298n drivers at a slow speed and control it via an app. Everything is functioning just fine besides the speed of the motors, which is always at maximum speed. I can't figure out how to set the speed I want. The code I have is the following:
int motor1Pin1 = 8;
int motor1Pin2 = 9;
int enable1Pin = 10;
int motor2Pin1 = 12;
int motor2Pin2 = 13;
int enable2Pin = 11;
int motor3Pin1 = 2;
int motor3Pin2 = 3;
int enable3Pin = 4;
int state;
int flag=0;
int stateStop=0;
float delay_time;
void setup()
{ // sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
pinMode(motor3Pin1, OUTPUT);
pinMode(motor3Pin2, OUTPUT);
pinMode(enable3Pin, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
delay_time = 15;
}
else
{
delay_time=0;
}
// if the state is 'F' the DC motor will go Forward
if (state == 'F') {
analogWrite(enable1Pin, 30);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
if(flag == 0){
Serial.println("Go Forward!");
flag=1;
}
}
// if the state is 'L' the motor will turn Left
else if (state == 'L') {
analogWrite(enable2Pin, 30);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
if(flag == 0){
Serial.println("Turn LEFT");
flag=1;
}
}
// if the state is 'S' the motor will Stop
else if (state == 'S' || stateStop == 1) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
if(flag == 0){
Serial.println("STOP!");
flag=1;
}
stateStop=0;
}
// if the state is 'R' the motor will turn Right
else if (state == 'R') {
analogWrite(enable2Pin, 30);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
if(flag == 0){
Serial.println("Turn RIGHT");
flag=1;
}
}
// if the state is 'B' the motor will Reverse
else if (state == 'B') {
analogWrite(enable1Pin, 30);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, LOW);
if(flag == 0){
Serial.println("Reverse!");
flag=1;
}
}
// if the state is 'U' the motor will go Up
else if (state == 'U') {
analogWrite(enable3Pin, 30);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, HIGH);
digitalWrite(motor3Pin2, LOW);
if(flag == 0){
Serial.println("Go UP");
flag=1;
}
}
// if the state is 'D' the motor will go Down
else if (state == 'D') {
analogWrite(enable3Pin, 30);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(motor3Pin1, LOW);
digitalWrite(motor3Pin2, HIGH);
if(flag == 0){
Serial.println("Go Down");
flag=1;
}
}
}