How do I code it so that I can have two potentiometer with one controlling an esc, and the other controlling a servo motor?
This is the code I have so far:
int potPin = 0; //Analog Pin 0 is the read pin for the potentiometer
int signal = 12; //Pin 12 is the signal that goes to the ESC
int val = 0;
int pot_min = 0; //Sets the lower value of the potentiometer read range
int pot_mid = 512;
int pot_max = 1024; //Sets the upper value of the potentiometer read range
float old_pulse_width = 15; //The neutral value of the to pulse. Default is 15 (1.5 milliseccond pulse duration)
float new_pulse_width = 15;
float pulse_width = 15; //milliseconds
float period = 20; //milliseconds
float increment = .01; //milliseconds
void setup() {
Serial.begin(9600);
pinMode(signal, OUTPUT); //Sets signal pin as output
delay(3000); // waits 3 seconds (3000 ms) before program runs
}
// the loop routine runs over and over again forever:
void loop() {
Serial.print(val); //prints out potentiometer value
Serial.print("\t");
Serial.print(10pulse_width); //prints out current pulse duration
Serial.print("\t");
Serial.println(10new_pulse_width); //prints out target pulse duration
old_pulse_width = pulse_width;
for(int x = 0; x < 20; x++){
val = analogRead(potPin); // read the value from the sensor
{
new_pulse_width = (10.0/(pot_max-pot_min))*(pot_mid-val) + 15;
}
if (new_pulse_width > old_pulse_width)
{
pulse_width = old_pulse_width + increment;
}
else if (new_pulse_width < old_pulse_width)
{
pulse_width = old_pulse_width - increment;
}
else
{
pulse_width = old_pulse_width;
}
if (pulse_width > 25 || pulse_width < 5)
{
pulse_width = 15;
}
delayMicroseconds(pulse_width*100); // the duration of the pulse (15 => 1.5 ms pulse duration)
delay(17 - pulse_width/20); // the duration between pulses
delay(3); // wait for an additional 3 ms (I forget why this line and the line before is split
}
}