Constrain Function for Negative Numbers

Something like this?


if (input <537 || >487);
	input=0
      
if (input >537 || < 1023);
      map the output for forward
if (input >0 || <486);
      map the out put for reverse
int16_t value = map(analogRead(pin), 0, 1024, -255, 255)

If (value < -10) //backwards
{
 value *= -1; //flip to positive, do reverse stuff
}
else if(value > 10)
{
  //do forward stuff
}
else
{
  // stop
}

Change your deadzone from 10 and -10 to whatever you want

Hello dana_lynn

Consider:

int ljoystick;           // ljoystick Value
int ljoystickmapped;      // Value after mapping
int ljoystickfwd;
int ljoystickrev;
const uint8_t Ljoystick {A0};
const uint16_t Offset {12};

void setup()
{
  Serial.begin(9600);      //Open serial port
}
void loop()
{
  ljoystick = analogRead (Ljoystick);
  switch (ljoystick)
  {
    case 0 ... 512-Offset:
      ljoystickfwd = map (ljoystick, 0, 512 - Offset, 255, 0);
      Serial.print ("Forward: "), Serial.println (ljoystickfwd);
      break;
    case 512+Offset ... 1023:
      ljoystickrev = map (ljoystick, 512 + Offset, 1023, 0, 255);
      Serial.print ("Reverse: "), Serial.println (ljoystickrev);
      break;
    default:
      Serial.println ("Stopp");
      break;
  }
  delay(100);

  /*
    ljoystick = analogRead (PotPin);                //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);
  */
}
1 Like

Nope, you're overthinking it again.
Didn't you say we're not using negative numbers?

Try something like:

if (ljoystick < 487) {
  // this is for reverse
  ljoystickrev = map(ljoystick, 0, 486, 255, 1);
  ljoystickfwd = 0;
}
else if (ljoystick <= 536) {
  // this is for the center deadband
  ljoystickrev = 0;
  ljoystickfwd = 0;
}
else {
  // this is for forward
  ljoystickrev = 0;
  ljoystickfwd = map(ljoystick, 537, 1023, 1, 255);
}

Hi Paul,

Works GREAT!

I added the right joystick, and did the analogWrites. Everything is perfect.

Thanks you so much!!!!!

1 Like

Hi Odometer,

I'll try your code after work today. It makes sense, so this should be interesting.

Thanks you for all your help. You people on this forum are AWESOME!!!

Dana

We do our best.

Have fun in the last days of the year and enjoy programming in C++.

Hi Paul,

In the code for the joystick, can I add a digitalWrite so that anytime the joystick is doing an analogWrite to pins 5,6,10,11 it will digitalWrite pin 2 HIGH? Once the analogWrite is off the digital pin 2 will go LOW...

Hello dana_lynn

Post your current sketch to see what happens.

int ljoystick;           // ljoystick Value
int rjoystick;
int ljoystickfwd;
int ljoystickrev;
int rjoystickfwd;
int rjoystickrev;
const uint8_t Ljoystick {A0};
const uint8_t Rjoystick {A1};
const uint16_t Offset {50};

void setup()
{
  Serial.begin(9600);      //Open serial port
}


void loop()
{
 
  ljoystick = analogRead (Ljoystick);
  switch (ljoystick)
  	   {
    case 0 ... 512-Offset:
      ljoystickfwd = map (ljoystick, 0, 512 - Offset, 255, 0);
      Serial.print ("Forward: "), Serial.println (ljoystickfwd);
       { 
  		analogWrite (11,ljoystickfwd);
         digitalWrite (2,HIGH);  //  my digitalWrite
  	   }
      break;
    case 512+Offset ... 1023:
      ljoystickrev = map (ljoystick, 512 + Offset, 1023, 0, 255);
      Serial.print ("Reverse: "), Serial.println (ljoystickrev);
      { 
  		analogWrite (10,ljoystickrev);
        digitalWrite (2,HIGH);  //  my digitalWrite
  	   }
    
     break;
    default:
      Serial.println ("Stop");
    
       { 
  		analogWrite (11,0);
        analogWrite (10,0);
         digitalWrite (2,LOW);  // my digitalWrite
  	   }
    
    break;
    
  }
    
     
  rjoystick = analogRead (Rjoystick);
  switch (rjoystick)
       {
    case 0 ... 512-Offset:
      rjoystickfwd = map (rjoystick, 0, 512 - Offset, 255, 0);
      Serial.print ("Forward: "), Serial.println (rjoystickfwd);
       { 
  		analogWrite (5,rjoystickfwd);
          digitalWrite (2,HIGH);  //  my digitalWrite
  	   }
      break;
    case 512+Offset ... 1023:
      rjoystickrev = map (rjoystick, 512 + Offset, 1023, 0, 255);
      Serial.print ("Reverse: "), Serial.println (rjoystickrev);
      { 
  		analogWrite (6,rjoystickrev);
         digitalWrite (2,HIGH);  //  my digitalWrite
  	   }
    
     break;
    default:
      Serial.println ("Stop");
    
       { 
  		analogWrite (6,0);
        analogWrite (5,0);
          digitalWrite (2,LOW);  // my digitalWrite
  	   }
    
    break;
  }
    
  }
type or paste code here

It appears to work, but instead of a HIGH it only has a few hundred Mv

Post your schematic too

Joystick Works.pdf (5.3 KB)

Hello dana_lynn

In case of "case default" of joystick right the port pin 2 will written to LOW.

I took them out and now it does go high, but stays high. Where can I put the digitalWrite low to get it to go low when the joystick is not being used?

Do you drive a motor shield?
I“m confused.

Make this modifcations to your sketch.

void setup()
{
  Serial.begin(9600);      //Open serial port
  pinMode (2,OUTPUT);
}

Did you make pin 2 an OUTPUT mode pin?

Does using the same pin and LED for both joysticks make sense?

Inquiring minds want to know.

a7

When any of the joysticks are moved, I need to start a hydraulic pump.
The analog writes will drive the hydraulic valve PWM coils.

So I want to drive a MOSFET anytime a joystick is moved in any direction.