LED Light Control With Analog Joystick

Hello,
I am currently working on a line of code that uses 4 LEDs and one Analog Joysick (Adafru Analog Thumb Joystick) in order to turn on one LED when I go past a certain point in both the X and Y directions. For example, if I wanted to turn the "forward" LED on, I would push up on the joystick, and once the potentiometer hits a certain range (171 and up), it will light the LED. Same goes for the "backwards," "left," and "right" LEDs. I am currently having an issue with my "if" and "while" statements, where they will not loop and thus ending the code as soon as it reads a certain input. If I push in any direction, it will only turn on one LED, and it will leave it on rather than turn off when the joystick returns to its neutral position. Furthermore, the code will not print onto the serial monitor tool, which is another major issue in diagnosing my problem. The code is as stated here:

const int analogInPinX = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 10; // Analog output pin that the LED is attached to
const int analogOutPinX = 11;

int sensorValueX = 0;        // value read from the pot
int outputValueX = 0;        // value output to the PWM (analog out)

const int analogInPinY = A1;  // Analog input pin that the potentiometer is attached to
const int analogOutPinYForward = 9; // Analog output pin that the LED is attached to
const int analogOutPinYBackward = 12;

int sensorValueY = 0;        // value read from the pot
int outputValueY = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValueX = analogRead(analogInPinX);
  // map it to the range of the analog out:
  outputValueX = map(sensorValueX, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPinX, outputValueX);

   sensorValueY = analogRead(analogInPinY);
  // map it to the range of the analog out:
   outputValueY = map(sensorValueY, 0, 1023, 0, 255);
  // change the analog out value:

    

    while (outputValueY > 171){
       analogWrite (analogOutPinYBackward, 0);
       analogWrite (analogOutPinYForward, 120);
       
   }


    
  // print the results to the Serial Monitor:
  Serial.print("sensorX = ");
  Serial.print(sensorValueX);
  Serial.print("outputX = ");
  Serial.println(outputValueX);
  
   Serial.print("sensorY = ");
  Serial.print(sensorValueY);
  Serial.print("\t outputY = ");
  Serial.println(outputValueY);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}

I know this post is a little bit vague, but if anyone has a lead as to what I can do please let me know. I will be more than willing to give more information if it is asked, but as of now this is as much as I can say.

Thanks,
Drake

while (outputValueY > 171)
{
       analogWrite (analogOutPinYBackward, 0);
       analogWrite (analogOutPinYForward, 120);       
 }

It gets stuck here because you are not reading new values for outputValueY. if outputValueY does not change there is no way to exit the while loop.

I didn't study your code carefully but you may be stuck in your while() loop. ... And, you may not need a while() loop if you organize your if-statements differently.

You say you want to "turn on" the LEDs, but analogWrite() is for dimming.

As a general rule it's best to start simple and "develop" your code one or two lines at a time, test-compiling and test-running as you go.

The biggest mistake most beginners make is trying to write the whole program at once. Big programs written by professional programmers are written/developed from smaller "modules" (but a pro may write many lines or a complete function before testing).

And, it's usually best to work on the input (the joystick in your case) and the output (the LEDs in your case) separately before putting everything together.

That's not quite as easy at it seems because you can't just start at the top and work-down... The program needs to make sense to the compiler and the compiler needs to see a "complete program", and you need ways to test if it's working as-expected. And, you can't just throw the input & output halves together... But once you learn to work like that, programming gets a LOT easier. ...Well it never gets easy! :wink:

It can be very helpful to send messages to the serial monitor. You can "watch" your joystick readings and you can add little messages like "starting while-loop", or "Forward LED is on", etc.

For example, start with the [u]Blink Example[/u], modified for the pins your LEDs are connected to (pins 9 & 10?). You can modify multiple times to check all of the LEDs. Once you know the LEDs are working you might want to make a couple more changes to make sure it's on when it's supposed to on, and if you want to dim you can test the dimming.

Then run the [u]Analog Read Serial Example[/u] modified to check the joystick. Again you can modify it twice to check both X & Y readings. This will also give you a chance to test-out the serial monitor. And, you'll want to make note of the joystick-center readings.

Once you have the inputs & outputs tested and working, you can start putting things together with if-statements and any other logic. Again, just one or two lines at a time! i.e. You might want to start with Analog Read Serial in either the X or Y direction, and then add an if-statement to turn-on one LED. Then build little-by-little from there.

Hello,
Thank you for the pointers. Using these I was able to fix the code, and now it does exactly what I needed.

const int analogInPinX = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPinXBackward = 10; // Analog output pin that the LED is attached to
const int analogOutPinXForward = 13;

int sensorValueX = 0;        // value read from the pot
int outputValueX = 0;        // value output to the PWM (analog out)

const int analogInPinY = A1;  // Analog input pin that the potentiometer is attached to
const int analogOutPinYForward = 9; // Analog output pin that the LED is attached to
const int analogOutPinYBackward = 12;

int sensorValueY = 0;        // value read from the pot
int outputValueY = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
 
       analogWrite (analogOutPinYBackward, 0);
       analogWrite (analogOutPinYForward, 0);

       analogWrite (analogOutPinXBackward, 0);
       analogWrite (analogOutPinXForward, 0);
  
  // read the analog in value:
  sensorValueX = analogRead(analogInPinX);
  // map it to the range of the analog out:
  outputValueX = map(sensorValueX, 0, 1023, 0, 255);
 

   sensorValueY = analogRead(analogInPinY);
  // map it to the range of the analog out:
  outputValueY = map(sensorValueY, 0, 1023, 0, 255);
  // change the analog out value:

 if (outputValueY > 171){
       analogWrite (analogOutPinYBackward, 0);
       analogWrite (analogOutPinYForward, 150);
        }
 if (outputValueY < 85){
       analogWrite (analogOutPinYBackward, 150);
       analogWrite (analogOutPinYForward, 0);
        }
 if (outputValueX > 171){
       analogWrite (analogOutPinXBackward, 0);
       analogWrite (analogOutPinXForward, 150);
        }
 if (outputValueX < 85){
       analogWrite (analogOutPinXBackward, 150);
       analogWrite (analogOutPinXForward, 0);
        }

      
    
  // print the results to the Serial Monitor: 
  // Serial.print("sensorX = ");
  // Serial.print(sensorValueX);
  // Serial.print("outputX = ");
  // Serial.println(outputValueX);
  
  Serial.print("sensorY = ");
  Serial.print(sensorValueY);
  Serial.print("\t outputY = ");
  Serial.println(outputValueY);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}

Thank you very much for the help!