Dual Throttles for an RC Electric Motor

Hi Group,
I'm pretty new to the Arduino world but I am an engineer so I'm familiar with programming. I am however having a problem figuring out a piece of code. The goal of the code is to take two signals coming from two separate potentiometers, compare them, and then using the greater of the two to generate an ESC signal to drive an RC motor (or a servo signal, same thing). I started with a piece of code for a single input to generate a servo output, which worked fine for driving my motor. Here it is:

#include <Servo.h>

Servo ESC; // create servo object to control the ESC

int potValue; // value from the analog pin

void setup() {
// Attach the ESC on pin 9
ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
}

void loop() {
potValue = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
potValue = map(potValue, 0, 1023, 0, 180); // scale it to use it with the servo library (value between 0 and 180)
ESC.write(potValue); // Send the signal to the ESC
}

So then I tried to modify it to do the comparison of two separate potentiometers and use the greater of the two values to generate the ESC signal. Here it is:

#include <Servo.h>

Servo ESC; // create servo object to control the ESC

int potValue; // calculated value of the greatest value between potvalue1 and potvalue2
int potValue1; // value from the analog pin A0 "hand throttle"
int potValue2; // value from the analog pin A1 "foot throttle"

void setup() {
// Attach the ESC on pin 9
ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
}

void loop() {
potValue1 = analogRead(A0); // reads the value of potentiometer1 (value between 0 and 1023)
potValue2 = analogRead(A1); // reads the value of potentiometer2 (value between 0 and 1023)
if (potValue1 > potValue2) digitalWrite(potValue, potValue1); //if the hand throttle is greater than the foot throttle then use the hand throttle
if (potValue2 > potValue1) digitalWrite(potValue, potValue2); //if the foot throttle is greater than the hand throttle then use the foot throttle
if (potValue1 = potValue2) digitalWrite(potValue, potValue1); //if both throttles are equal then use the hand throttle by default
potValue = map(potValue, 0, 1023, 0, 180); // scale it to use it with the servo library (value between 0 and 180)
ESC.write(potValue); // Send the signal to the ESC
}

Problem is it doesn't work. Neither pot changes the output signal at all. I'm sure it's a rookie mistake but I can't seem to see it. Anyone with experienced eyes, if you could take a look I would greatly appreciate it.

Thanks!

Please use Autoformat, Ctrl T, in the IDE and then copying the code here using code tags, the uppe left symbol in this window.
Tell about the last, not working setup, what is does and what You want.
Time is late here, sharpness is low. Maybe better "tomorrow".

I see no curley braces on your if statements.
Also see lines that should be under //
Posting the code in the correct format would be a good start.

Hi,
Welcome to the forum.

Please read http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

barracj:

 if (potValue1 = potValue2) digitalWrite(potValue, potValue1);  //if both throttles are equal then use the hand throttle by default

There's an = sign missing there. That's probably a major part of your problems.

#include <Servo.h>

Servo ESC;     // create servo object to control the ESC

int potValue;  // value from the analog pin

void setup() {
  // Attach the ESC on pin 9
  ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
}

void loop() {
  potValue = analogRead(A0);   // reads the value of the potentiometer (value between 0 and 1023)
  potValue = map(potValue, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180)
  ESC.write(potValue);    // Send the signal to the ESC
}

This is the one that actually works. The next one is the one that doesn't.

#include <Servo.h>

Servo ESC;     // create servo object to control the ESC

int potValue;  // calculated value of the greatest value between potvalue1 and potvalue2
int potValue1;  // value from the analog pin A0 "hand throttle"
int potValue2;  // value from the analog pin A1 "foot throttle"

void setup() {
  // Attach the ESC on pin 9
  ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
}

void loop() {
  potValue1 = analogRead(A0);   // reads the value of potentiometer1 (value between 0 and 1023)
  potValue2 = analogRead(A1);   // reads the value of potentiometer2 (value between 0 and 1023)
  if (potValue1 > potValue2) digitalWrite(potValue, potValue1);   //if the hand throttle is greater than the foot throttle then use the hand throttle
  if (potValue2 > potValue1) digitalWrite(potValue, potValue2);  //if the foot throttle is greater than the hand throttle then use the foot throttle
  if (potValue1 = potValue2) digitalWrite(potValue, potValue1);  //if both throttles are equal then use the hand throttle by default
  potValue = map(potValue, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180)
  ESC.write(potValue);    // Send the signal to the ESC
}

So then I thought I would try using analogRead instead of digitalWrite. It seems more logical. But I keep getting an error message when trying to compile: "potvalue was not declared in this scope."

#include <Servo.h>

Servo ESC;     // create servo object to control the ESC

int potValue;  // calculated value of the greatest value between potvalue1 and potvalue2
int potValue1;  // value from the analog pin A0 "hand throttle"
int potValue2;  // value from the analog pin A1 "foot throttle"

void setup() {
  // Attach the ESC on pin 9
  ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds) 
}

void loop() {
  potValue1 = analogRead(A0);   // reads the value of potentiometer1 (value between 0 and 1023)
  potValue2 = analogRead(A1);   // reads the value of potentiometer2 (value between 0 and 1023)
  if (potValue1 > potValue2) potvalue = analogRead(A0);   //if the hand throttle is greater than the foot throttle then use the hand throttle
  if (potValue2 > potValue1) potvalue = analogRead(A1);  //if the foot throttle is greater than the hand throttle then use the foot throttle
  if (potValue1 = potValue2) potvalue = analogRead(A0);  //if both throttles are equal then use the hand throttle by default
  potValue = map(potValue, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180)
  ESC.write(potValue);    // Send the signal to the ESC
}

Hi,
How do you spell the potValue or potvalue variable.
If you look at the IDE when it gives you that error, you should be able to see in your code a highlighted line, this shows where the error was detected.

  if (potValue1 > potValue2) potvalue = analogRead(A0);   //if the hand throttle is greater than the foot throttle then use the hand throttle
  if (potValue2 > potValue1) potvalue = analogRead(A1);  //if the foot throttle is greater than the hand throttle then use the foot throttle
  if (potValue1 = potValue2) potvalue = analogRead(A0);

Tom... :slight_smile:

Thank you to everyone that has responded helpfully. As it turned out I had spelled "potValue" with a lowercase "v". Duh! :roll_eyes:. The analogRead approach with the if statements was the right approach. The circuit works beautifully now! :smiley:

Cheers!

Did you fix the one that I pointed out as well?
= is an assignment, == a boolean comparison.

By the way, the following three (corrected) lines can be rewritten much simpler:

if (potValue1 > potValue2) potvalue = analogRead(A0);   //if the hand throttle is greater than the foot throttle then use the hand throttle
  if (potValue2 > potValue1) potvalue = analogRead(A1);  //if the foot throttle is greater than the hand throttle then use the foot throttle
  if (potValue1 == potValue2) potvalue = analogRead(A0);  //if both throttles are equal then use the hand throttle by default

is equivalent to:

if (potValue2 > potValue1) {
  potValue = potValue2;
else {
  potValue = potValue1;
}

Or, even shorter but a bit less readable:

potValue = (potValue2 > potValue1) ? potValue2 : potValue1;

I do think the extra analogRead() calls are redundant, as the analog value won't change significantly in between the two times it's read; and if you expect it does there is a whole different issue as the larger value may have become the other...

  if (potValue1 > potValue2) digitalWrite(potValue, potValue1);   //if the hand throttle is greater than the foot throttle then use the hand throttle
  if (potValue2 > potValue1) digitalWrite(potValue, potValue2);  //if the foot throttle is greater than the hand throttle then use the foot throttle
  if (potValue1 == potValue2) digitalWrite(potValue, potValue1);  //if both throttles are equal then use the hand throttle by default

In the original code, digitalWrite() is used to write a HIGH or LOW value to an output pin, in this case the value of potValue is the pin number, and the value of potValue1 or potValue2 is the HIGH or LOW (LOW if 0, HIGH if any value other than 0).

I would guess what you really want is to assign the value of potValue1 or potValue2 to potValue:

  if (potValue1 > potValue2) potValue = potValue1;   //if the hand throttle is greater than the foot throttle then use the hand throttle
  if (potValue2 > potValue1) potValue = potValue2;  //if the foot throttle is greater than the hand throttle then use the foot throttle
  if (potValue1 == potValue2) potValue = potValue1;  //if both throttles are equal then use the hand throttle by default

Awesome, thanks guys! This is really good stuff. I've just got to get used to the new language.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.