Problem with analogwrite

Hello, I am using the arduino to read inputs from a joystick, and then send outputs to an H-bridge driver. I am using analogWrite(pin#, 255), but rather than getting an always on output I get a pulsed output with a duty cycle of around 50% the weird thing is that this only happens when I push the joystick forward, left, and right. If I push the joystick back I get the output that I expect. I've tried changing the output pins and the input pins of the joystick, I've tried different joysticks, and I tried using digitalwrite but the problem persists. here is my code.

/* Capstone Project
 Sends On signals to the inputs of an H bridge when a joystick is pushed
 in various directions
 */

// constants won't change. They're used here to 
// set pin numbers:
const int JoystickFWD = 2;     // the number of the pin attached to forward on joystick
const int FWD1 = 6;          // the number of the pin attached to h bridge FWD1 pin (Left motor forward)
const int FWD2 = 5;           // the number of the pin attached to h bridge FWD2 pin (Right motor forward)
const int JoystickBACK = 4;    // the number of the pin attached to back on joystick
const int BACK1 = 10;          // the number of the pin attached to h bridge BACK1 pin (Left motor back)
const int BACK2 = 11;          // the number of the pin attached to h bridge BACK2 pin (Right motor back)
const int JoystickLEFT = 7;    // the number of the pin attached to left on the joystick
const int JoystickRIGHT = 8;   // the number of the pin attached to right on the joystick


// variables will change:
int buttonState = 0;         // variable for reading the Joystick Direction

void setup() {
  // initialize the h bridge pins as outputs
  pinMode(FWD1,OUTPUT);
  pinMode(FWD2,OUTPUT);
  pinMode(BACK1,OUTPUT);
  pinMode(BACK2,OUTPUT);

  // initialize the joystick directions as inputs:
  pinMode(JoystickFWD,INPUT);
  pinMode(JoystickBACK,INPUT);
  pinMode(JoystickLEFT,INPUT);
  pinMode(JoystickRIGHT,INPUT); 
}

void loop(){
  // read the state of the pushbutton value
  buttonState = digitalRead(JoystickFWD);

  // check if the joystick is pushed forward.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH){     
    // turns both motors on    
    analogWrite(FWD1, 255);
    analogWrite(FWD2, 255);
  }
  else {
    // turns both motors off:
    analogWrite(FWD1, 0);
    analogWrite(FWD2, 0);
  }

  buttonState = digitalRead(JoystickBACK);

  if (buttonState == HIGH){
    analogWrite(BACK1, 255);
    analogWrite(BACK2, 255);
  }
  else {

    analogWrite(BACK1, 0);
    analogWrite(BACK2, 0);
  }
  buttonState = digitalRead(JoystickLEFT);

  if (buttonState == HIGH){
    analogWrite (FWD1, 255);
  }
  else {
    analogWrite(FWD1, 0);
  }
  buttonState = digitalRead(JoystickRIGHT);

  if (buttonState == HIGH){
    analogWrite (FWD2, 255);
  }
  else{
    analogWrite(FWD2, 0);
  }

}
/*
Code modified from
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 17 Jun 2009
 by Tom Igoe
 
 http://www.arduino.cc/en/Tutorial/Button
 */

any help would be greatly appreciated.

Joysticks use potentiometers, which are analog devices. You appear to have the joystick connected to digital pins, and are using digital functions to read the joystick.

What kind of joystick do you have?

All the joysticks I've used are microswitch joysticks, sorry I guess I should have mentioned that but they aren't the problem.

Try a simple sketch that simply sets the PWM duty cycle for the four pins, and delays for 2 seconds.

Do you observe the same behavior?

You do not appear to be debouncing the switches that you are reading.

I don't think debouncing is necessary, I've checked signals going to the arduino with an oscilloscope when I hold the joystick in a direction the signal is very steady.

I tried the simple sketch like you suggested and the duty cycle for the output pins is what it should be.

If you add a delay to your original sketch, does that change/alleviate the problem?

After
if (buttonState == HIGH){
// turns both motors on
analogWrite(FWD1, 255);
analogWrite(FWD2, 255);
?

No. Put the delay at the end of the loop() function, just before the closing }.

Tried adding a really short delay, 1 and 10 ms and now I don't see any output. Any other suggestions?

Any other suggestions?

Just the usual one that I make when someone has a software problem. Post the current code.

I am using analogWrite(pin#, 255), but rather than getting an always on output I get a pulsed output with a duty cycle of around 50%

What do you base this observation on? How are you determining duty cycle?

Here is the code now

/* Capstone Project
 Sends On signals to the inputs of an H bridge when a joystick is pushed
 in various directions
 */

// constants won't change. They're used here to 
// set pin numbers:
const int JoystickFWD = 2;     // the number of the pin attached to forward on joystick
const int FWD1 = 6;          // the number of the pin attached to h bridge FWD1 pin (Left motor forward)
const int FWD2 = 5;           // the number of the pin attached to h bridge FWD2 pin (Right motor forward)
const int JoystickBACK = 4;    // the number of the pin attached to back on joystick
const int BACK1 = 10;          // the number of the pin attached to h bridge BACK1 pin (Left motor back)
const int BACK2 = 11;          // the number of the pin attached to h bridge BACK2 pin (Right motor back)
const int JoystickLEFT = 7;    // the number of the pin attached to left on the joystick
const int JoystickRIGHT = 8;   // the number of the pin attached to right on the joystick


// variables will change:
int buttonState = 0;         // variable for reading the Joystick Direction

void setup() {
  // initialize the h bridge pins as outputs
  pinMode(FWD1,OUTPUT);
  pinMode(FWD2,OUTPUT);
  pinMode(BACK1,OUTPUT);
  pinMode(BACK2,OUTPUT);

  // initialize the joystick directions as inputs:
  pinMode(JoystickFWD,INPUT);
  pinMode(JoystickBACK,INPUT);
  pinMode(JoystickLEFT,INPUT);
  pinMode(JoystickRIGHT,INPUT); 
}

void loop(){
  // read the state of the pushbutton value
  buttonState = digitalRead(JoystickFWD);
  // check if the joystick is pushed forward.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH){     
    // turns both motors on    
    analogWrite(FWD1, 255);
    analogWrite(FWD2, 255);
    delay(40);
  }
  else {
    // turns both motors off:
    analogWrite(FWD1, 0);
    analogWrite(FWD2, 0);
  }
  buttonState = digitalRead(JoystickBACK);

  if (buttonState == HIGH){
    analogWrite(BACK1, 255);
    analogWrite(BACK2, 255);
  }
  else {

    analogWrite(BACK1, 0);
    analogWrite(BACK2, 0);
  }
 
  buttonState = digitalRead(JoystickLEFT);

  if (buttonState == HIGH){
    analogWrite (FWD1, 255);
    analogWrite (FWD2, 0);
  }
  else {
    analogWrite(FWD1, 0);
    analogWrite(FWD2, 0);
  }

  buttonState = digitalRead(JoystickRIGHT);

  if (buttonState == HIGH){
    analogWrite (FWD2, 255);
    analogWrite (FWD1 ,0);
  }
  else{
    analogWrite(FWD2, 0);
    analogWrite(FWD1, 0);
  }
  
}
/*
Code modified from
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 17 Jun 2009
 by Tom Igoe
 
 http://www.arduino.cc/en/Tutorial/Button
 */

I'm just guessing on the duty cycle, it looks like about 50% when I check out the output on an oscilloscope

actually adding that delay after
if (buttonState == HIGH){
// turns both motors on
analogWrite(FWD1, 255);
analogWrite(FWD2, 255);
helped, but the same thing doesn't work with the left and right sections

also when I comment out
buttonState = digitalRead(JoystickLEFT);

if (buttonState == HIGH){
analogWrite (FWD1, 255);
analogWrite (FWD2, 0);
}
else {
analogWrite(FWD1, 0);
analogWrite(FWD2, 0);
}

buttonState = digitalRead(JoystickRIGHT);

if (buttonState == HIGH){
analogWrite (FWD2, 255);
analogWrite (FWD1 ,0);
}
else{
analogWrite(FWD2, 0);
analogWrite(FWD1, 0);
}
it works perfectly forward and backwards

I suspect that the problem is that more than one switch is being triggered at a time. I suspect that you are executing the analogWrite on and analogWrite off statements so close together that the duty cycle really is only 50%.

Try reading all 4 switches into different variables, making the appropriate amalogWrite statements, then Serial.print() the 4 switch states.

I didn't do it with serial print, but I have checked to make sure multiple buttons aren't being turned on. I also have a restrictor plate on the joystick that ensures that won't happen. And it doesn't make sense that it only happens on left and right, and that the problem persists when i rearrange the buttons

I think I've pretty conclusively ruled out the buttons as the problem, is there any other reason why I can turn on two pins with analog write i.e. with

 buttonState = digitalRead(JoystickFWD);
  // check if the joystick is pushed forward.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH){     
    // turns both motors on    
    analogWrite(FWD1, 255);
    analogWrite(FWD2, 255);
    delay(40);
  }
  else {
    // turns both motors off
    analogWrite(FWD1, 0);
    analogWrite(FWD2, 0);
  }
  buttonState = digitalRead(JoystickBACK);

  if (buttonState == HIGH){
    analogWrite(BACK1, 255);
    analogWrite(BACK2, 255);
  }
  else {

    analogWrite(BACK1, 0);
    analogWrite(BACK2, 0);
  }

but not one output with

 buttonState = digitalRead(JoystickLEFT);

  if (buttonState == HIGH){
    analogWrite(FWD1, 255);
  }
  else {
    analogWrite(FWD1, 0);
  }

  buttonState = digitalRead(JoystickRIGHT);

  if (buttonState == HIGH){
    analogWrite(FWD2, 255);
  }
  else {
    analogWrite(FWD2, 0);
  }

?

Are the buttons wired with pull-up resistors or pull-down resistors?

yeah there are pull down resistors, and small (around 50 ohm) resistors in series

So beside the buttons does anyone have any other ideas?
Thanks