Joystick and leds using PWM, simple problems needs great minds too

HI everyone, i connected leds and i control their state with the joystick and also their brightness with PWM, my problems are,
(1)if you look at the code a second i want to know how to increase the accuracy of the fade effect, [EDIT: i figured out that the fading isn’t accurate because of the joystick it self]

(2) why the heck my ledGreen won’t use PWM, i changed to another PWM pin but still not working, [EDIT: and i also changed my LED twice]

thanks for anyone answers, your helping the world to become a better place :slight_smile:

byte swPin = 12; //button Pin
byte xPin = 0; // X axis Analog pin
byte yPin = 1;// Y axis Analog pin
byte ledBlue = 6;
byte ledGreen = 5;
byte ledYellow = 11;
byte ledRed = 10;


void setup() {
  // put your setup code here, to run once:
  pinMode(ledBlue, OUTPUT);    // setting pin modes and starting Serial for debugging
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(swPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  int xVal = analogRead(xPin);

  int yVal = analogRead(yPin);

  int xpVal = map(xVal, 0, 1023, 0, 255);

  int ypVal = map(yVal, 0, 1023, 0, 255);

  Serial.println(ypVal); //for  debugging onlu

  if (xpVal > 128) {

    int xppVal = map(xpVal, 129, 255, 0, 255);

    analogWrite(ledRed, xppVal);

  }
  else if (xVal < 128) {

    int xpppVal = map(xpVal, 123, 0, 0, 255);

    analogWrite(ledGreen, xpppVal);

  }
  else
  {
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, LOW);
  }

  if (ypVal > 123) {
    int yppVal = map(ypVal, 123, 255, 0, 255);

    analogWrite(ledBlue, yppVal);
  }

  else if (ypVal < 123)
  {

    int ypppVal = map(ypVal, 123, 0, 0, 255);

    analogWrite(ledYellow, ypppVal);

  }
  else
  {
    digitalWrite(ledBlue, LOW);
    digitalWrite(ledYellow, LOW);
  }

}

One error, 2 suggestions

  if (xpVal [color=blue]> 128[/color]) {
    int xppVal = map(xpVal, 129, 255, 0, 255);
    analogWrite(ledRed, xppVal);
  }
  else if ([color=red]xVal[/color] [color=blue]< 128[/color]) {

xVal should be xpVal → that why the heck your ledGreen won’t use PWM :slight_smile:

also that means that if xpVal is exactly 128 you turn off Red and Green. is that intended?
with your mapping from 0-1023 to 0-255 you get 128 only for 4 original analogRead values roughly. you might want to be a bit more generous and do between 125 and 130 for example, that will make it easier to hit that spot.

1/ you don’t need to create those weird looking variables with different names such as xpppVal as they are local anyway to the if clause. And calling them something nicer than xpVal would probably have prevented the error above. Use normalizedX instead of xpVal or rawAnalogX instead of xVal for example and it would have been obvious in your if you were not testing the right thing.

2/ ensure your

  int xVal = analogRead(xPin);
  int yVal = analogRead(yPin);

have the full range between 0 and 1023 before mapping otherwise you loose a bit in your gradient

Thanks man!!! I really appreciate it from you to help me . Respect