I have been trying to use a POT for forward and reverse on an analog input.
I was hoping to have the pot in the center and be able to move it forward 0-255 and reverse 0-255 from the center position. With the map function it does go to zero in the center, but has negative numbers so when I do an analog write it is doing weird things.
Would the constrain function help, or am I on the wrong track...
Here is my code.. Thanks !! Dana
int ljoystick; // ljoystick Value
int ljoystickmapped; // Value after mapping
int ljoystickfwd;
int ljoystickrev;
void setup()
{
Serial.begin(9600); //Open serial port
}
void loop()
{
ljoystick = analogRead (A0); //Read in the L joystick data
//Serial.println (ljoystick); //Print analog L joystick value
ljoystickmapped = map (ljoystick,0,1023,0,255); //ljoystick mapped value
ljoystickfwd = map (ljoystickmapped,128,255,0,255); // map for forward
Serial.print ("Forward :");
Serial.println (ljoystickfwd);
ljoystickrev = map (ljoystickmapped,0,128,0,255); // Reverse map for reverse function
ljoystickrev = map (ljoystickrev,255,0,0,255);
Serial.print (" Reverse : ");
Serial.println (ljoystickrev);
delay(1000);
}
Please explain more clearly what it is you wish to do.
It appears that you have one input, and that is a potentiometer which is read by one of the analog input pins on the Arduino. Is this correct?
How many outputs do you wish to have? Just one? Or do you want to have two outputs, one for forward and another one for reverse?
What exactly is the relationship between this input and this output (or these outputs)? Can you draw a graph (hand-drawn is OK) which shows this relationship?
if (ljoystick < 512) {
ljoystickfwd = 0;
ljoystickrev = (511 - ljoystick) / 2;
}
else {
// you take care of the else, I already did half of this for you
}
I somewhat follow what you posted and will have to do more research to try to figure what the 'else' statement needs to be. At least I'm one step closer. I never thought it would be this difficult to use a pot for this purpose.
The negative numbers is what my problem is now. Forward is near zero to 255 and reverse is near zero to 255 in the opposite direction (perfect..). Forward however goes from near zero to a -255 in the reverse direction (real bad), and I think the analogWrite is actually writing a negative valve and apparently the arduino thinks it is a positive number.
I have spent two days solid trying to figure out a way to make the arduino disregard the negative numbers, but to no avail.
It must not be something they ever thought there would be a reason to do.
First of all: you need to be clear on the difference between = and ==.
Use = when you want to change a variable to a value.
Use == when you just want to check whether a variable is equal to a value.
Second: How big do you want your deadband to be? The range is 0 to 1023, so dead center will be 511 or 512. Perhaps 50 numbers wide would be wide enough? So that would be from 487 to 536.
So,
if (ljoystick < 487) {
// this should be reverse
}
else if (ljoystick <= 536) {
// this should be the center deadband
}
else {
// this should be forward
}
I leave it to you to fill in the actual code, if you can.
I am trying to use ifs and elses to split the possible values of your analog input (0~1023) into three separate ranges, low (0~486), middle (487~536), and high (537~1023). Your analog input will only be in one of these ranges at any given time. This means that we have three separate situations to consider. We can consider them one by one.
Let us consider the "middle" situation first, as it is the simplest.
In the "middle" situation, what should the value of ljoystickrev be? What about ljoystickfwd?
I guess it would be something like this, but I know it will still horrify me with negative numbers.
if (ljoystick < 487) {
map the reverse 0,1023, 487,0
map for output 487,0,23,255
analogWrite
}
else if (ljoystick <= 536) {
}
else {
map the forward 0,1023, 0,487
map for output 0,487,23,255
AnalogWrite
}
What do you want ljoystickrev to be in the high situation?
What about ljoystickfwd? This requires more thought, as to give the value of ljoystickfwd, you will need to more precisely specify the value of the analog input.
When the value of the analog input is 537, what should ljoystickfwd be?
When the value of the analog input is 1023, what should ljoystickfwd be?
Now put on your thinking cap. Suppose the value of the analog input is 780. Now give me the correct values for ljoystickrev and ljoystickfwd.