Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:05:03+00:00 2026-06-07T10:05:03+00:00

I am using the following code to determin where the thumb joystick is positioned:

  • 0

I am using the following code to determin where the thumb joystick is positioned:

const int Left = 1;
const int Right = 2;
const int Up = 3;
const int Down = 4;
const int Center = 0;
int lastButton = -1;

int xpin = 4;
int ypin = 5;

int xAxis;
int yAxis;
char* myStrings[]={"Left","Right","Up","Down"};
int button;

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

void loop() {
    xAxis=map(analogRead(xpin), 0, 1023, 0, 10);
    yAxis=map(analogRead(ypin), 0, 1023, 0, 10);

    if (button == lastButton) {
        //Serial.println(0);
        //button = 0;
        delay(50);
    } else {
        if (xAxis < 4 ) {
            button = Left;
        }
        else if (xAxis > 6 ) {
            button = Right;
        }

        if (yAxis < 4 ) {
            button = Down;
        }
        else if (yAxis > 6 ) {
            button = Up;
        }

        Serial.println(myStrings[button-1]);
        button = 0;
        delay(50);
    }

    lastButton = button;
}

The code above works just fine on my arduino but i am looking to only get the postion ONCE and not every second while its held there.

How can i change the code to only take just one value until its centered (0) again?

Example:

If i move the joystick to the left it reads:

Left
Left
Left
Left
etc etc until i release it.

What i am looking to do is this:

Left
then it stops until i release it.

Any help would be great!

update

 if (xAxis ==5 ) { 
   button = Center; 
 }

 if (yAxis ==5 ) { 
   button = Center; 
 }

Seems to work well with Up and Down but not really for Left and Right. Sometimes it works, more often it doesn’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:05:04+00:00Added an answer on June 7, 2026 at 10:05 am

    Remember where you were at the last iteration and compare to that to see if it has changed:

    int lastButton = 0;
    ...
    void loop() {
        button = ...// Read button state
        ...
        if (button != lastButton) {
            ... // New keypress
            lastButton = button;
        }
    }
    

    Complete cleaned up code:

    enum ButtonState { Neutral=0, Left=1, Right=2, Up=3, Down=4 };
    
    ButtonState lastButton = Neutral;
    
    const int XPin = 4;
    const int YPin = 5;
    
    char* myStrings[]={"Neutral", "Left","Right","Up","Down"};
    
    void setup() {
        Serial.begin(9600);
    }
    
    ButtonState readButtonState() {
        int xAxis=map(analogRead(XPin), 0, 1023, 0, 10);
        int yAxis=map(analogRead(YPin), 0, 1023, 0, 10);
        if (xAxis < 4 ) return Left;
        if (xAxis > 6 ) return Right;
        if (yAxis < 4 ) return Down;
        if (yAxis > 6 ) return Up;
        return Neutral;
    }
    
    void loop() {
        ButtonState button = readButtonState();
        if (button != lastButton) {
            Serial.println(myStrings[button]);
            lastButton = button;
        }
        delay(50);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Check the following code: string toLowerCase(const string& str) { string res(str); int i; for
I am using the following code to determine which key is pressed by the
I am using the following code to determine if I can write to a
I'm trying to determine the width of a string. I'm using the following code
Using following code I try to get updated list of checkbuttons' corresponding text values,
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I am using following code for showing a MessageBox with ok and cancel button.
I am using following code to show a spinning wheel: $(#loading) .hide() .ajaxStart(function(){ $(this).show();
I am using following code to get bitmap from url. This function is used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.