JOYSTICK to LED HELP!

EDIT: I fixed the issue by using greenValue - red/blue Value then i only had to think about 2 colors being effected by the Y axis. works amazing now!

   int y = 0;
   y = abs(analogRead(joyY));

  int redValue = 0;
  int greenValue = 0;
  int blueValue = 0;

  
  greenValue = (map(y, 500, 550, 255, 255));

  redValue = (map(y, 550,1023,0,255));
if (redValue > 0){
  greenValue = (map(y, 550, 900, 255, 0)-redValue);
  }

  blueValue =  (map(y, 19, 500, 255, 0));
if (blueValue > 0)
{
  greenValue = (map(y, 200, 550, 0, 255)-blueValue);
}

  blueValue = constrain(blueValue, 0, 255);         //Make sure the range of power for each output is 0 to 255
  redValue = constrain(redValue, 0, 255);
  greenValue = constrain(greenValue, 0, 255);

  analogWrite(RED, redValue);                       //Make the lights turn on.
  analogWrite(BLUE, blueValue);                     //    :)
  analogWrite(GREEN, greenValue);

I hope this is the correct place to post this.

I’m working on a project where I use a joystick to perform 2 separate functions. for the Y axis and the X axis.

the x axis controls a servo, that is working fine.

the Y axis is where I am having trouble. I am trying to it programmed where if I have the Y axis near center the RGB LED’s stay Green;
If i move said stick UP then LED fade into RED;
and if i move it down LED fades to BLUE;

i have been ale to make the LED fade in and out of color, just not the correct color, its like the are fading in, or 1 color is more dominant. like I move it UP and I get red but then center is yellow and down is green. or any other combination but the right one.

I have attached the code. any advice of fixes would be greatly appreciated…

I am thinking of using the switch on the joystick to activate an audio clip eventually. the finished project will be an animatronic beholder head.

#include <Bounce2.h>
#include <Servo.h>
#include <SlowMotionServo.h>


//Servo objects created to control the servos
SMSSmooth myServo1;
SMSSmooth myServo2;



// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6


// define variables
int servo1 = 9; //Digital PWM pin used by the servo 1
int servo2 = 10; //Digital PWM pin used by the servo 2
int joyX = 0;   //Analog pin to which the joystick (X) is connected
int joyY = 1;   //Analog pin to which the joystick (Y) is connected
const int SW_pin = 2; // digital pin connected to switch output
int redValue;
int greenValue;
int blueValue;
int xValue, yValue;


void setup(){
  Serial.begin(9600); 
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, LOW);
  
  myServo1.attach(servo1);
  myServo2.attach(servo2);
  myServo1.write(90);
  myServo2.write(90);
  myServo1.setSpeed(1000);
  myServo2.setSpeed(1000);  

}

void loop(){

  
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(joyX));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(joyY));
  Serial.print("\n\n");
  delay(100);

  //LED + Joystick

   int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)
   valY = map(valY, 0, 1023, 0, 255); //Scale the joystick Y value to use it with the LED

if (valY <=550 && valY >=500)
      {
      valY = map(valY, 500, 550, 0, 255);
      analogWrite(GREEN, valY);
      analogWrite(BLUE, 255-valY);
      analogWrite(RED, 255);

      delay (1);
      }
else if (valY <= 500 && valY >=0)
     {
      valY = map(valY, 0, 500, 0, 255);
      analogWrite(RED, valY);
      analogWrite(GREEN, 255-valY);
      analogWrite(BLUE, 255);
      delay (1);
     }

else if (valY >= 550 && valY <=1023)
     {
      valY = map(valY, 550, 1023, 0, 255);
      analogWrite(BLUE, valY);
      analogWrite(GREEN, 255-valY);
      analogWrite(RED, 255);
      delay (1);
     }






// Servo + Joystick

  SlowMotionServo::update();
  int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
  valX = map(valX, 0, 1023, 0, 160); //Scale the joystick X value to use it with the servo

  
//Sets the servo position according to the scaled values.
  myServo1.write(valX);

 delay (1); // so we'll take 10 * 180 milliseconds = 1.8 seconds for the traverse.

}

MASTER.ino.ino (2.48 KB)

MASTER.ino.ino (2.48 KB)

I have attached the code.

It would be more convenient if you posted it here, using code tags when you do

Incidentally, it looks like you have Windows "hide extensions" turned on and the filename has become a mess

posted code in message now. thank you!

int servo1 = 9; //Digital PWM pin used by the servo 1
int servo2 = 10; //Digital PWM pin used by the servo 2
int joyX = 0;   //Analog pin to which the joystick (X) is connected
int joyY = 1;   //Analog pin to which the joystick (Y) is connected
const int SW_pin = 2; // digital pin connected to switch output

The names would be more meaningful if pin appeared somewhere in all the names of variables that hold pin numbers.

You read, and print, the joystick values. Then, you wait a tenth of a second (uselessly), then read them again, and act on unknown values. That makes no sense. Read, store, print, and act on the same values.

   valY = map(valY, 0, 1023, 0, 255); //Scale the joystick Y value to use it with the LED

map() is a very expensive way to divide by 4.

if (valY <=550 && valY >=500)

After you’ve mapped the value in valY to be in the range 0 to 255, what are the odds that the value will be between 550 and 500? Does expressing the code in English that way make sense? It does not, to me.

I would write the code the way I think - is the value between 500 and 550?