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
Posted on Friday August 23, 2019GigaDevice Releases GD32V RISC-V MCU and Development Boards CNX SoftwareA few years ago, we came across GigaDevice GD32 microcontroller compatible with STMicro STM32F103, but with a higher 108 MHz clock, and zero wait state. … Continue Reading »
Posted on Thursday February 07, 2019Arduino vs Raspberry Pi: How to Choose the Right Board All 3DPIf you are looking for a small, affordable single board computer, there are two popular choices: the Arduino and the Raspberry Pi. But which one should you pick … … Continue Reading »
China The Most Complete Ultimate UNO Starter Kit for children stem education on Global Sources – Global Sources
Posted on Monday August 19, 2019China The Most Complete Ultimate UNO Starter Kit for children stem education on Global Sources Global SourcesChina UNO Starter kit AE086 is supplied by ☆ UNO Starter kit manufacturers, producers, suppliers on Global Sources. … Continue Reading »
arduino joystick button Social