I can't seem to figure out how to set limits with my ESC. I am putting an ultrasonic sensor on my arduino for this rc project I am making but the issue is that I need my car's esc to slow down according to this:
1-2 feet = 100% stop and I can only go reverse
3 - 5 feet = only a 50% limit 10 + ignore
I understand I need to convert this to cm but i measured in feet. I also understand I have to use servo.h in my code (I HAVE) I dont know how to make the esc have the limits. I have a traxxas XL5HV esc and the controller I'm using is the fly sky fs-i6x. I've tried the map() function its just I don't know how to set my own values here is my code:
#include <Servo.h> //Servo librarey to support ESC
#define TX 3
int TXValue;
//First Defines pins numbers for trig & echo
int trigPin = 7;
int echoPin = 6;
// now Defines variables
long duration;
int distance;
Servo ESC; //Here name of the DC motor
void setup()
{
pinMode(TX, INPUT);
// here we defines output and input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
ESC.attach(3); //Generate PWM in pin 11 of Arduino
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
TXValue = pulseIn(TX, HIGH);
//Serial.println(TXValue);
digitalWrite(trigPin, LOW);
delayMicroseconds(3);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the Value of TX (my remote)
Serial.println(TXValue);
//int throttle = distance; //Read the distance from Ultrasonic sensor
//throttle = map(throttle, 100, 0, 1000, 1500); //Map works only from 0-180 so convert distance in cm to new map range
//ESC.writeMicroseconds(TXValue);
if(distance < 10){
ESC.writeMicroseconds(1500); // send "stop" signal to ESC. Also necessary to arm the ESC.
delay(2500);
// send "reverse" signal to ESC i dont know how
delay(2500);
ESC.writeMicroseconds(TXValue);
}
else{
ESC.writeMicroseconds(TXValue); //I've done this only because I thought It would make less bugs
}
ESC.writeMicroseconds(TXValue);
}
I passed the pwm directly towards the esc with no map funtion. How do I set some limits and can I use the map() to do so?
fs I6X pwm limits
(min)1010 - 1990(max)
1480 - 1500 Mid point
If you know how to make a bypass switch for this it will also help!!
loop:
read the pulse coming in, map it to microseconds
check distance
if distance < 1 foot:
Stop (ESC.writeMicroseconds(1500).
Wait 2.5 sec as per your code
You need to know what microseconds value corresponds to reverse. If 1500 is stop and 1000 is full reverse, you could write say 1400 to the ESC.... slow reverse.
I think this will control the throttle the way you want:
// Calculating the distance
distance = duration * 0.034 / 2;
#define ONE_FOOT (12 * 2.54)
#define TWO_FEET (ONE_FOOT * 2)
#define FIVE_FEET (ONE_FOOT * 5)
// Prints the Value of TX (my remote)
Serial.println(TXValue);
if (distance == 0 || distance > FIVE_FEET)
{
// No obstruction so use TXValue unchanged
}
else if (distance < TWO_FEET)
{
// Within two feet so maximum forward speed is 1500 (stopped).
TXValue = constrain(TXValue, 1000, 1500);
}
else if (distance < FIVE_FEET)
{
// Within five feet so maximum forward speed is 1750 (1/2 forward).
TXValue = constrain(TXValue, 1000, 1750);
}
ESC.writeMicroseconds(TXValue);
}
the bypass switch was for sometimes when im driving this car I want to drive near any wall and the bypass switch lets me do so, what I was asking in my form was how do I make the switch bypass the code. One way I did find out is setting the value of the ultrasonic sensor to 1000+ when I flick the switch.
kinda like this
if(button == 1){
distance= 1194;
}
else{
distance= duration*0.034/2; // to advoid some bugs
}
Thank you so much that was exactly what I was looking for. I tryed to use the constrain() funtion before but it never worked only because I didnt put the "TXValue = " in the first part of the code, I leaned somthing new today