Go Down

Topic: Simple Newbie joystick question (Read 129 times) previous topic - next topic

damlimey

Hi all,

Here's what I am trying to do with a Leonardo and a joystick:

I want to use a joystick to trigger an LED. In other words, I want to detect the movement UP/Down & LEFT/RGHT of the joystick and then have it reported by an LED turning on.

I know it sounds simple but I'm finding it difficult to break down how to do this by looking at various examples of joystick control. There seem to be many ways of skinning this particular cat but none of them are as simplistic as I need them to be. I can do it with a button (pressing 1 of 4 buttons is reported by an LED) but I want to replace the buttons with a joystick.

Please help get me started with this.

Thank you in advance
G

PaulS

Quote
There seem to be many ways of skinning this particular cat but none of them are as simplistic as I need them to be.
I really can't imagine anything simpler than reading two analog pins, to know where the joystick is, and writing a couple of if statements, to decide which LED to light up.

Are you ONLY interested in knowing which quadrant the joystick is in?
The art of getting good answers lies in asking good questions.

damlimey

Hello PaulS,

Yes actually, while it may seem simple to anyone here, at the age of 60 I am starting to write code for the first time in my life. I am unfamiliar with everything I am doing with my first Arduino, which arrived in the post a day or so ago and while I do have an end project in mind, I am stuck with a vast horizon of ignorance and seek guidance in this case.

After a lot of reading and working through examples, the penny just hasn't dropped for me so,I have done that which I have always done in these circumstances; called upon the illumination of others, when I am in the dark.

So, while you are plagued with the inability to imagine not knowing anything so simple, I am blessed with being humble enough to know what I don't know.

I do hope that answers your question.

Meanwhile, in the time it took you to write the above, you might have passed on those few lines of code, methinks.

Thanks for asking,
G

MorganS

What kind of joystick? Not USB, I hope.
"The problem is in the code you didn't post."

PaulS

This is ONE way to skin that cat.
Code: [Select]
const byte xPin = 0; // Connect the x axis of the joystick to analog pin 0
const byte yPin = 1; // Connect the x axis of the joystick to analog pin 1

void setup()
{
   Serial.begin(115200);
}

void loop()
{
   int xVal = analogRead(xPin);
   int yVal = analogRead(yPin);

   if(xVal < 512)
   {
      if(yVal > 512)
      {
         Serial.println("Joystick is in lower right quadrant");
      }
      else
      {
         Serial.println("Joystick is in lower left quadrant");
      }
   }
   else
   {
      if(yVal > 512)
      {
         Serial.println("Joystick is in upper right quadrant");
      }
      else
      {
         Serial.println("Joystick is in upper left quadrant");
      }
   }

   delay(100);
}
The art of getting good answers lies in asking good questions.

MorganS

#5
May 22, 2019, 05:35 pm Last Edit: May 22, 2019, 05:35 pm by MorganS
Here is a slight modification which uses the actual labels on the Arduino board...

Code: [Select]
const byte xPin = A0; // Connect the x axis of the joystick to analog pin 0
const byte yPin = A1; // Connect the y axis of the joystick to analog pin 1


Edit: fixed Y comment
"The problem is in the code you didn't post."

PaulS

Here is a slight modification which uses the actual labels on the Arduino board...

Edit: fixed Y comment
My (old) Arduino has the pins on each side simply numbered. No As or Ds anywhere on the board.

Thanks for fixing the comment.
The art of getting good answers lies in asking good questions.

damlimey

Gentlemen,

That's what I needed, it's the 'if' value code that I was struggling with!

I am very grateful to you both!

With thanks,
G

Go Up