Constrain Function for Negative Numbers

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?

Correct. One POT and one input on A0

I would like to have two PWN outputs on 10 and 11.
One would be for forward (0-255) and one for reverse (0-255).

For this I would use an if statement.

I would say

if (ljoystick < 512) {
    ljoystickfwd = 0;
    ljoystickrev = (511 - ljoystick) / 2;
}
else {
  // you take care of the else, I already did half of this for you
}

Thanks Odometer!

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.

Again, Thanks a zillion!!!

Hi Odometer,

Could a person just do this somehow:
So that I could have a deadband in the center incase the joystick does not center exactly?

 if (ljoystickfwd < 0) {
    ljoystickfwd == 0;

See


Everything you are doing here is simple logic or maths. And anything you can do to be able to figure it out will not be time wasted.

I found

googling "joystick deadband".

a7

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.

I'm not sure what to do now..

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.

Thanks again Odometer, but I don't know how. I'm so confused after the last few days, that I don't know whether I'm on foot or horse back.

Try filling in the center deadband part first. That should be the easiest. I'm sure you can do it.

Hint: Don't overthink it.

You've got this.

Let me try to clear up some of the confusion.

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 mean?

When the input is 0, what is the output? Call it aaa

When the input is 487, what is the output? Call it bbb

Use map, viz:

     output = map(input, 0, 487, aaa, bbb);

a7

Nope, you're overcomplicating things again. Way overcomplicating things again.

I don't understand. Where does that number 23 come from? I thought your desired output range was 0 to 255.

I was thinking the diff between 487 and 512 = 24 so maybe that's where the 487 number came from.

I see what your thinking..

in the mid point (487-536) I would want both ljoystickfwd and ljoystickrev to = 0

Good.

Now, what about the "high" situation?

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.

Low I would want to map to 0

780 I'm not sure on this.

780 should be 193 for forward
780 for reverse would be back to a negative number.. yikes!