hello there, i'm new to the forum,
i'm trying to make a coilwinder for guitar pickups, and am facing a problem, whenever i try to change the speed of the motor during reverse, it changes to a very slow forward rotation.
i'm using a arduino uno, a bts motor driver 48A, ive got a 12v 30A regulated powersupply, and the motor is a black $ decker cordless drill.
i've got both enable pins of the driver wired to digital pin 2 on the arduino, pwm_left to digital pin 12, and pwm right to digital pin 3.
i'llpost my code below, if anyone can help its much apreciated.
int usMotor_Status = 0;
int pwm_right=3;
int pwm_left=11;
int EN_r=2;
//int EN_l=12;
short usSpeed=10;
void setup() {
pinMode(pwm_right, OUTPUT);
pinMode(pwm_left, OUTPUT);
pinMode(EN_r, OUTPUT);
pinMode(EN_l, OUTPUT);
usMotor_Status = 0;
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("1. STOP");
Serial.println("2. FORWARD");
Serial.println("3. REVERSE");
Serial.println("4. READ CURRENT");
Serial.println("+. INCREASE SPEED");
Serial.println("-. DECREASE SPEED");
Serial.println();
}
void loop()
{
char user_input;
while(Serial.available())
{
user_input = Serial.read(); //Read user input and trigger appropriate function
if (user_input =='1')
{
Stop();
}
else if(user_input =='2')
{
Forward();
}
else if(user_input =='3')
{
Reverse();
}
else if(user_input =='+')
{
IncreaseSpeed();
}
else if(user_input =='-')
{
DecreaseSpeed();
}
else
{
Serial.println("Invalid option entered.");
}
}
}
void Stop()
{
Serial.println("Stop");
digitalWrite(EN_r, LOW);
//digitalWrite(EN_l, LOW);
digitalWrite(pwm_left, LOW);
digitalWrite(pwm_right, LOW);
usMotor_Status = Stop;
}
void Forward()
{
Serial.println("forward");
digitalWrite(EN_r, HIGH);
//digitalWrite(EN_l, HIGH);
digitalWrite(pwm_left, LOW);
analogWrite(pwm_right, usSpeed);
usMotor_Status = Forward;
}
void Reverse()
{
Serial.println("reverse");
digitalWrite(EN_r, HIGH);
//digitalWrite(EN_l, HIGH);
digitalWrite(pwm_right, LOW);
analogWrite(pwm_left, usSpeed);
usMotor_Status = Reverse;
}
void IncreaseSpeed() {
usSpeed = usSpeed + 10;
if(usSpeed > 255)
{
usSpeed = 255;
}
if (usMotor_Status = Forward){
Forward();
} else if (usMotor_Status = Reverse) {
Reverse();
}
Serial.print("Speed +: ");
Serial.println(usSpeed);
}
void DecreaseSpeed() {
usSpeed = usSpeed - 10;
if(usSpeed < 0)
{
usSpeed = 0;
}
if (usMotor_Status = Forward){
Forward();
} else if (usMotor_Status = Reverse) {
Reverse();
}
Serial.print("Speed -: ");
Serial.println(usSpeed);
}
btw. just as a note, i have both enable pins wired together because in the tutorial i found, it said you could also just wrie them to 5v