arduino joystick button


If you go back to a tutorial, we did a couple of months ago we were using the joystick analog joystick module. We have here to control a stepper motor back and forth and we’re doing it is that when we moved the joystick module it would move one side or the other, but when we released it it would come back to the middle. Now we had a lot of question regarding that tutorial and people were asking: how can I control it? Move the joystick, the stepper motor would move either side and when I release the joystick it would stop. I don’t want it to come back to the center, so this tutorial is going to see how we do that, but also what we’re going to do. We’Re going to add some limit switches, as you see here so we’re, going to have to limit switches which will represent the left side and the right side so technically, when it would hit one of those limit switches. What would happen is that the motor would stop, even though you would keep pushing left or right. It would stop because it would be activated. So when the switch is released, then you would get the control again and also what we’re going to do. We’Re going to use the switch that’s on the joystick to actually control the speed of the motor, so every time we click it’s going to go faster and we set three speed in our codes.

So the first, when we first start it’s going to be the slowest speed. So when we click it’s going to go a little bit faster and when we click again it’s going to go to the fastest speed. So, like I always say, I encourage you to go. See the tutorial page of this project at Bernadette’s, comm, slash, tutorials and you’ll, see the schematic how everything is plugged in and you’ll get the code. If you want to copy and paste that into your Arduino IDE and a little bit more explanation, and also you have, you will find a link to our store where we sell all these parts that you can buy from our store and actually recreate this tutorial here. So this is going to be a quick tutorial, like I said so, we’re going to cut here, we’re, going to look at the code and we’re not using any libraries again to control the motor we’re. Just setting the pins high and low very fast to make the stepper motor do steps and we’ll explain these limit switches and the switch of the joystick to control the speed. So let’s cut here and let’s go check that out and then we’ll come back in see it working right here. So let’s go. Take a look alright, so we are in the code of the joystick stepper it’s, not going to be a complicated code. We’Re going to start by defining all the pins that everything is connected to so at the first at the top.

Here we have the pins that are connected to the EZ driver. Then we have the X pin the x axis. Pin of the joystick is connected to analog 0 because it reads analog values between 0 and 10, 23 and 512 is like. When the joystick is setting at the sitting at the center, then we define the joystick switch, pin it’s connected to pin 4 on the you know, then r2 limit switches that are going to be for the left and right limit. These are connected to pin 2 amp 3. Then we define a variable step, speed equal to 10. Now this 10 represent a delay when the stepper moves, so the higher the number the slower the stepper is going to move and we’re going to change this value. We’Re sitting at a 10 which will be the slowest speed at the beginning, and every time we press the joystick switch we’re going to use the switch case function to actually change that number to medium and fast. Then we have our setup for all the pins right here, so we’re defining which are which pins our output and which pins are input. The drastic switch is an input, pull up we’re using the resistor that is integrated inside you know for that switch at the other. Two here have their own resistors on the PCB already, so we don’t need to use the pull ups. Then we do a digital right of the to sleep in on the easy driving too high to wake it up, and we do a very small delay to wait for the easy driver to get ready and we’ve seen these in other tutorial here.

This is setting up the type of steps that we’re going to do with our stepper so depending on, if we put those two pins, I’m, s1 and M, is too low or high it’s going to decide what type of steps we’re doing. If we do an eighth of a step, the motor will be more precise, but we’ll go slower, so we’re doing full step by putting both pins to low. Then we jump directly to the de loop, so the first thing we’re going to do we’re going to check. If the joystick switch is clicked so every time we click the joystick that’s going to go inside this loop here. So if we click the joystick switch, we do delay of 502 D balance the switch. Then we do a switch case, so basically switch step, speed, which is 10 right now so we’re going to check what value it is at right now now case that’s the value so case 1. It is not case 1, it’s case 10, basically at the beginning, so it’s not case tree either so it’s skipping that – and it is case 10. So in case the step speed is equal to 10. Then we’re going to set the variable step, speed to 3, which is medium sweet and then we break out of that switch case function. So every time we’re going to click. That value is going to change. So we start at 10 when we click it’s going to be 3.

So if we click again it’s going to be case 3 now so it’s going to set the speed to 1, which is the fastest speed where I gather that and when we click the joystick switch again it’s going to be a case 1 and it’s going to Set the speed back to 10, which is a slow speed, so we’re going to alternate between these three speeds. You could put more speeds and there you can change the numbers to make it slower or faster. Whatever suits your your project, so once we’ve taken care of that, then we’re checking the x axis of the joystick. Now, if analog read of the X bin is greater than 712 and, like I said at the beginning, joysticks are analog devices, so they go from 0, which is complete, left side of the X and up to 1023, which is full right, so we’re kind of like We don’t want to go we’re using values that are a little bit on the left side and the right side like in the middle, so the X bend, is greater than 712. That means the analog joystick has been moved to the left, and then we know that we want to move the motor to the left. So basically before we do that we’re going to check if the limit switch is activated. If it is activated, then we do nothing. We just break out that loop right away, but if it isn’t else, then we move the motor right here and those values will decide on which which side, counterclockwise or clockwise.

We move the stepper motor and here are the delay which will control the speed of our motor that we use so set speed. As step. Speed will be equal to 10, which is slow to 1, which is fast or to 3, which is medium, and then we break out of that, and then we check if we’re moving the joystick to the right instead. Now, if we move it to the right, then we check the second limit switch to make sure that one is not activated. If it is, then we don’t move the motor. If it’s not we move it and that’s all there is to it. So basically, this is the whole code, it’s very simplified. I mean, of course you could use a stepper driver libraries to have more more function, to give you more options when you move your motor, but this works fairly well and it’s. First, it’s easy to write and we’ll go back, we’ll upload this code to the Ohno and then we’ll go back and check out the results. So let’s go take a look alright, so we’re back and we uploaded the code to the uno right away and we plugged in the power for the EZ driver and we’re powering the Ohno using one eight six five zero battery pack like like you see here. So everything is ready to go. The little carriage here simulating the carriage is in the middle, but we’re going to start moving the joystick one way so we’re going to go left first and this limit switch here will represent the limit switch.

That would be the end of the left travel. So we’ll go now. As you can see, we set the speed in the code at very slow, so one way or the other stops now. If I click one it’s going to go to medium speed, so a little bit faster, both ways and if I click again then it’s going to be the fastest speed. Of course you could, you know, put more speeds if you want in the code but we’re using three right here so and there you go now it’s at the fastest speed, so now we’re going to simulate that the carriage would hit this limit switch, which would be To the left so I’m just going to press it when it comes close and there you go it stopped. So, even though I keep pushing left nothing’s happening because this one is activated so technically as I move right, it would release the switch like so and then I could move back again and we’re going to simulate the other one, which is this switch. So we’re going to go right and before it hits it clicks and it stops. But of course I can still move left, but if I hold it I cannot go back. So if I release it, I can go back again and if I put it back to slow speed again very slow, medium fast, so there you go guys and not a. I wanted to make a quick tutorial because we had a lot of questions so, instead of answering everything in emails separately, I thought that I would do a quick tutorial on how to control the motor directly using the analog joystick and also show you how to use The limit switches to actually control the travel of the stepper so that’s it for this tutorial we’re going to have another one coming very soon: we’re gon na we’re trying to do something.

That’S, a halloween themed, as you can say so, we’re gon na have a little bit of fun with that. So keep a lookout for that new tutorial. If you’re not subscribed to our channel. Please do so that way. You’Ll get notified when we post new tutorials and also I encourage you to go check out our store at brainy, best calm, where we sell all the parts that we do, the tutorials with on YouTube. So if you guys need some part, come check us out and if you find something that that’s cool so that that’s always great for us too.


arduino joystick button Video







arduino joystick button news








arduino joystick button Social






A modular, Arduino Mega-controlled delta robot for your desktop.
safe image.php?d=AQDI5lGBL JxubWR&w=720&h=720& content%2Fuploads%2F2019%2F08%2F9154041566833130733 - arduino joystick button

This interactive floor recalls the precariousness of standing on an icy surface, enhanced with imagery, sounds and physical dynamics.
safe image.php?d=AQDDp  jywcEiEdX&w=720&h=720& v6%252Fshare%252Fplay icon overlay - arduino joystick button

Elegoo Arduino Starter Kit – Amazon Lightning Deal $26.76

ROHITH/D photo

Wired up and working! Wood, arcade buttons and stick and an Arduino pretending to be a computer keyboard. I’ve built a few arcade joysticks before, but haven’t had a working one for something like 5 years, after dismantling the last one for parts. And t

peterschulman photo









(Visited 28 times, 1 visits today)

About The Author

You might be interested in

Comment (29)

    1. Well i tryed to get one out but you need to heat up all the pins and pull it off (at the same time) hahaha, its hell of a job!! since i just found out that the all the Arduino components are relatively cheap, i wont continue that struggle.

  1. hello, please help me, i need help with these switchers, i need they reverse the motor instead of stoping it, is there any weay to do this? i need it is programable. i want to make a coil winding machine,

  2. I have a problem dude, my easy driver module get so hot in about 20 seconds, I’m using a 12v power supply.

    1. This is quite normal, but don’t worry the driver chip has a temperature protection set at around 165 degrees Celsius, so it wont’ burn out. You can also lower the current the Easy Driver provides to your stepper motor, which will make it run cooler. Turn the little Potentiometer that’s on the ED to adjust it from 150mA to 750mA. Hope that helps! Thanks for watching btw 🙂

  3. Not sure why my previous question showed up, but with this code, an Arduino Nano, Easy Driver, Nema 17 Stepper, and some mechanical limit switches (Vcc, Out, Gnd) the motor will only run if the switches are activated. If I let up on the switch, the motor stops. How do I fix this?

  4. I am a total noob honest am just poking wires everywhere. I have never written code ever so I am learning still. If I wanted to add a y axis to your code do you just repeat what is there for the X axis but call it y? And if you don’t want the limit switches can you just leave them out?

    1. Brainy-Bits Thank you it makes sense, I think I need too learn some more. Your video’s are really good thank you.

  5. Thank you for share this project with us. Very good explained and code included ….. perfect for an Beginner like me. Please let me ask: In your wiring diagram is an stepper control included by “A3967” – Is it possible to Change to stepper driver “A4988”? What for changes are for this needed? Thank you very much for your friendly Support,

  6. I know this is an old video about controlling stepper motors through Arduino and such so my question is can you use the same joystick controller a second stepper motor assuming you need a second easy driver i’d really appreciate your advice cos I’m intending to use it as a set up to control parts of a costume

  7. Hi, another great video. Roughly how long do you think the Arduino battery pack would last assuming the steppers have a separate supply?

  8. Wow, this is the perfect video to help me with my school project! Is there any way the joystick can be replaced with a variable resistor?

  9. can you replace the analog stick with a toogle switch so you basicly off with be 200 step (eg) and on with be the other direction and 200 steps? how would you go about doing this in the code thanks in advance

  10. must say great work . so many other gates opened for me after watching this video . Much much appreciated. Thank you!
    Please if you can tell me or guide me to a relevant project where i can use the same principle but just need a small detail to be altered where i can just toggle the joystick up/down for the movement. No need to keep on holding the joystick up to keep going up. If i toggle it up it should keep going up till the limit switches and the same for when pulled down. would really appreciate it .

  11. I wants to control 2 stepper motor with joystick but i wants to control the speed of the motor with another knob can u plz help me its my project to do


Your email address will not be published. Required fields are marked *