I have a simple Arduino code to run a BLDC motor with a joystick. It works but the motor does not always shut off when the joystick springs back to the center position, and the slightest touch will start the motor. There needs to be a larger off position or dead zone in the code so that the joystick is not so sensitive. At the bottom of the code I've included a snippet someone made but I can't get it to work. I'd appreciate any help.
#include <Servo.h> //Using servo library to control ESC
Servo esc; //Creating a servo class with name as esc
void setup()
{
esc.attach(9); //Specify the esc signal pin,Here as D9
esc.writeMicroseconds(1000); //initialize the signal to 1000
Serial.begin(9600);
}
void loop()
{
int val; //Creating a variable val
val= analogRead(A0); //Read input from analog pin a0 and store in val
val= map(val, 0, 1023,1000,2000); //mapping val to minimum and maximum(Change if needed)
esc.writeMicroseconds(val); //using val as the signal to esc
}
int val = analogRead(0); // Raw value
val -= 512; // Subtract center value (512)
int direction = val < 0 ? -1 : 1; // Direction is either -1 or +1
val = abs(val); // Remove the sign
val -= 10; // Subtract the threshold (10)
if (val < 0) val = 0; // Anything below zero is now zero
// val is now 0-501 with a dead zone, and direction is -1 or +1.
The motor works with this one but still not big enough dead zone, When I add the snippet there are duplicate lines and it won't compile, I don't know which to delete.
#include <Servo.h> //Using servo library to control ESC
Servo esc; //Creating a servo class with name as esc
void setup()
{
esc.attach(10); //Specify the esc signal pin,Here as D10
esc.writeMicroseconds(1000); //initialize the signal to 1000
Serial.begin(9600);
}
void loop()
{
int val = analogRead(0); // Raw value
val -= 512; // Subtract center value (512)
int direction = val < 0 ? -1 : 1; // Direction is either -1 or +1
val = abs(val); // Remove the sign
val -= 10; // Subtract the threshold (10)
if (val < 0) val = 0; // Anything below zero is now zero
// val is now 0-501 with a dead zone, and direction is -1 or +1.
val= analogRead(A0); //Read input from analog pin a0 and store in val
val= map(val, 0, 1023,1000,2000); //mapping val to minimum and maximum(Change if needed)
esc.writeMicroseconds(val); //using val as the signal to esc
}
#include <Servo.h> //Using servo library to control ESC
Servo esc; //Creating a servo class with name as esc
void setup()
{
esc.attach(9); //Specify the esc signal pin,Here as D9
esc.writeMicroseconds(1000); //initialize the signal to 1000
Serial.begin(9600);
}
void loop()
{
int val; //Creating a variable val
val = analogRead(A0); //Read input from analog pin a0 and store in val
val = map(val, 0, 1023, 1000, 2000); //mapping val to minimum and maximum(Change if needed)
if (val > 1550 || val < 1450)
{
esc.writeMicroseconds(val); //using val as the signal to esc
}
}
#include <Servo.h> //Using servo library to control ESC
Servo esc; //Creating a servo class with name as esc
void setup()
{
esc.attach(9); //Specify the esc signal pin,Here as D9
esc.writeMicroseconds(1000); //initialize the signal to 1000
Serial.begin(9600);
}
void loop()
{
int val; //Creating a variable val
val = analogRead(A0); //Read input from analog pin a0 and store in val
val = map(val, 0, 1023, 1000, 2000); //mapping val to minimum and maximum(Change if needed)
if (val > 1525 || val < 1475)
{
esc.writeMicroseconds(val); //using val as the signal to esc
}
else
{
esc.writeMicroseconds(1500);
}
}
I just tried your latest, its much better than before but it still doesn't want to stop going in reverse. Forward is fine but when I leave of the joystick from reverse it doesn't want to stop. It's not the joystick, It tests fine and it does the same thing when I connect it to a pot. The dead zone is not in the center of the pot, its 10- 15 deg off. If I could increase the dead zone of reverse I'd have it. If you have any ideas I'd appreciate it.
It's odd that doing the same thing for forward doesn't stop it in reverse. I expect it's more of the same though - the numbers going to the ESC aren't quite right. It'll take some experimenting to find the right ones.
For now, I'd fiddle with that 1500 number and see if you can find one that works better.