@UKHeliBob
quote="UKHeliBob, post:5, topic:896602"]
The trick to combining the 2 pot values is to use the maxSpeed value as the upper limit in map() to control the maximum speed in either direction.
[/quote]
This is the information I was looking for, Thanks!
I've now got the maxSpeed set to limit the upper value of the direction pot, and when I change the value of the maxSpeed pot it scales down the output I'm seeing through the serial monitor all the way to zero which is great and means that portion of the code works.
However now I'm running into an issue dividing the direction pot into separate forward and backwards values. I'm testing this on a breadboard with two 10k pots and looking at the serial monitor.
When the direction pot is swept to the left (forward) the serial is showing a positive number between 0 and 255 for both FwdOut and RevOut (The same value for both) and when it's swept to the right (reverse) it's showing a negative number between 0 and -265 for both Fwd and Rev (The same value for both)
I had hoped that setting the map(DirSensorValue, X, Y, 0, SpeedOut) X and Y values to be the lower and higher vales for each of the forward and backwards sections of the pot would make it disregard any values outside of the ranges I specified however I think that my understanding of how this works is wrong.
How do I make the FwdOut value show a value between 0 and 255 when the direction pot sweeps from a value of 500 to 0, and the RevOut show a value between 0 and 255 when the Pot sweeps from a value of 523-1023? (500 to 523 is the "dead zone" between forward and reverse)
This is what I've written so far:
// These constants won't change. They're used to give names to the pins used:
const int SpeedPot = A0; // Analog input pin that the speed pot is attached to
const int DirPot = A1; // Analog input pin that the direction pot is attached to
const int analogOutPin = 9; // Analog output pin that the motor controller is attached to
int SpeedSensorValue = 0; // value read from the speed pot
int SpeedOut = 0; //Value from maping speedpot output
int DirSensorValue = 0; // value read from the direction pot
int FwdOut = 0; // forward value from mapping direction pot
int RevOut = 0; // reverse value from mapping direction pot
int OutputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
Speed();
Direction();
Forward();
Reverse();
Output();
cereal();
}
void Speed() {
// read the speed pot analog in value:
SpeedSensorValue = analogRead(SpeedPot);
// map it to the range of the analog out:
SpeedOut = map(SpeedSensorValue, 0, 1023, 0, 255);
}
void Direction() {
// read the direction pot analog in value:
DirSensorValue = analogRead(DirPot);
}
void Forward() {
// map expected values from pressing the forward lever to the range of the maximum speed:
FwdOut = map(DirSensorValue, 523, 1023, 0, SpeedOut);
}
void Reverse() {
// map expected values from pressing the reverse lever to the range of the maximum speed:
RevOut = map(DirSensorValue, 0, 500, 0, SpeedOut);
}
void Output() {
OutputValue = min(FwdOut, RevOut);
// change the analog out value:
analogWrite(analogOutPin, OutputValue);
}
void cereal () {
// print the results to the Serial Monitor:
Serial.print("\t output = ");
Serial.println(OutputValue);
Serial.print("\t fwd = ");
Serial.println(FwdOut);
Serial.print("\t rev = ");
Serial.println(FwdOut);
Serial.print("\t Direction = ");
Serial.println(DirSensorValue);
// wait 1 second before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(1000);
}