I need a Stepper to increment in sections on the press of a button only while a limit switch is held in. When the switch is released, the motor will return to the 0 degree point.
I'm not sure if the section of his code that homes the motor relies on information from the Encoder. If it doesn't, then maybe you can let me know where I'm going wrong with my code.
I appreciate the guidance! I'm new to the forums, by the way, so I apologise if I post my code in an inappropriate manner.
You do need to know the home or starting position. That's often done with an [u]slotted optical switch[/u]. If there are pulleys, etc, for linear motion you might have a slotted optical switch or a mechanical switch at both limits.
It's common for the program to search for home at start-up or reset.
As long as you know the starting position, the software knows (or can know) how many steps have been taken in each direction, so you don't normally need a rotary encoder. (That's assuming the motor doesn't slip.) For example if you want to turn 90 degrees, your software sends 50 steps and as long as everything is working you don't need to confirm that with an encoder. If you want to go to 90 degrees from wherever you are, you have to know where you are starting and calculate the number of steps from there.
Most rotary encoders have the same issue... There is usually no built-in home position, and the rotary encoder only knows (actually, only the software knows) how far it's been turned, and in what direction.
Right now, it moves the stepper a certain number of degrees every time the button is pressed ONLY when the limit switch is closed. When the limit switch is open, the button doesn't work and I want the stepper to return to its original position.
What, exactly, is "the button"? Imagine the deck of the Starship Enterprise. There is unlikely to be a switch on a panel labeled "the button". Stop referring to this anonymous switch that way.
"the button" apparently performs some function, though it is hard to imagine what function ONE switch should perform when the stepper has pressed something against a limit switch AND when that something is not pressed against the limit switch. Please describe what this function is, and how it is meaningful in both cases.
MildlyCompetent:
If I were to use no additional hardware, could I not just have the stepper count the iterations it moved from it's starting position and have it do the same number in reverse when the limit switch is disengaged?
I don't know what you mean by "when the limit switch is disengaged".
At the very least you need a limit switch so that the Arduino can identify the starting position. Without that the motor could be starting from anywhere - for example someone may move it manually when the system is switched off.
Another option would be to watch the motor, use your eyes as the limit switch and press a button when it is at the place you want to be considered ZERO. However that won't be very accurate.
why can't I get the board to record how many steps the motor has taken and tell it to go backwards that recorded number when the limit switch is detected as being open?
The Arduino can remember how many steps it has told the stepper to move IF THE ONLY WAY FOR THE MOTOR TO MOVE IS FOR THE ARDUINO TO MAKE IT MOVE.
That being highly unlikely, you do need the limit switches.
As you read in my last reply, one is a limit switch meaning the other one must be the button which I previously described as a push button.
So, your control panel has a switch labeled "Do what I want done". I see. Good luck with that.
MildlyCompetent:
No I totally understand that. And that it would mess up what it thinks is home. I'm fine with that because it would be unlikely to happen in the setup it's in, plus power is constantly fed to the motor, so it would take a hefty knock to skip a step.
It was not clear from your earlier posts that you were clear about that and, in consequence, I don't think your
"Honestly, work on your comprehension skills and your ability to provide help will soar to no ends!" was in any way justified.
Similarly, I did not need to be told "A limit switch is basically a button that can be held in to provide a constant signal (engaged) or released to cause a loss of signal (disengaged)."
If there is any lack of comprehension it is in your ability to understand the questions you have been asked. I still cannot make sense of what you were trying to convey in the piece I quoted in Reply #6.
And you have not addressed the question of how the Arduino can know where the motor is at the time when the Arduino is powered up. That is the principal occasion when a limit switch is required. And I don't think you should overlook the possibilty that the motor might jump a step at shutdown or startup even if nobody disturbs it.
There is still the question of what exactly is the motor position when everything is first switched on. Random position?
Do you know how many steps from "home" position to limit switch trigger? If so, in your setup function, have the motor advance until it triggers the limit switch, then back up that number of steps.
MildlyCompetent:
If the limit switch is held down and you press the button, the motor turns. If the limit switch is up, the motor turns backwards to it's starting position. That's basically 100% of the functionality.
Maybe this has illustrated the confusion.
The issue is not the type of switch, but how it used.
A limit switch is a switch that is pressed by something moved by the motor when it gets to the limit of permitted movement. Its purpose is to tell the Arduino to stop moving the motor - for example when the cross-slide of a lathe gets close to the end of the drive screw. I would not expect the motor to move when the switch is pressed. And as soon as the motor moves the cross-slide away from the limit switch the switch will be released.
The button you have described is for a very different purpose - just controlling the direction of the motor.
So apparently, you need to count the number of steps the motor advances while the "limit switch" is held down, and then back up that same number of steps when the switch is released.
--Michael
MildlyCompetent:
Yeah I think you're right. That's probably where the confusion came from, but regardless of how it gets pressed, it gets pressed. So I figured I didn't need to say how it was used. Sorry for that.
Does the following describe what you want to achieve.
It does not matter where the motor is initially positioned
When a button is pressed the motor starts moving in one direction and steps are counted
When the button is released the motor moves in the other direction for the same number of steps
If so, what has been confusing me all the time is the first point because, usually, when limit switches are involved the initial position (or the final position, or both) is of vital importance.
MildlyCompetent:
Yes. When the limit switch is pressed in, the button can function.
I think it is useful to clarify this a little further. (Computers are incredibly stupid and need all the help they can get)
There appear to be two switches - let's call them A and B
Nothing can happen unless switch A is ON
While switch A is ON if B is ON the motor moves (say) clockwise counting steps
While A is ON if B is OFF and the motor will move anti-clockwise subtracting from the step count until the count reaches zero
What causes switch A to change state?
Can switch A change to OFF before the position count gets back to zero?
What happens if B turns ON before the position count gets back to zero?
...R
And, just for the future, the thing you posted a picture of in Reply #20 is normally referred to as a "microswitch"
I began to think the limit switch itself just wasn't physically capable of what I wanted it to do!
You will need to wire it correctly. The easiest way is to wire it such that it takes an Arduino pin LOW (to GND) when the switch is activated and use the internal pullup resistor in the Arduino when setting the pinMode()
pinMode(limitSwitchPin, INPUT_PULLUP);
That way you will always know whether the pin is HIGH or LOW and not some indeterminate, floating value)
I began to think the limit switch itself just wasn't physically capable of what I wanted it to do!
Everyone here has a concept of what a limit switch is, and how it is to be used. The problem is that everyone except you has one concept. You have something completely different.
It's like asking how to use ketchup in a recipe, and then posting a picture of a watermelon when asked what kind of ketchup you have.
What we think you should do involves our concept of a limit switch and how it is used. What you want to do is completely foreign to our concept of a limit switch because you are not using the switch as a limit switch but you insist on referring to your watermelon as ketchup, and then berating everyone that tries to tell you otherwise.
@MildlyCompetent, why, oh why, have you started another Thread about the same basic subject when all of the useful background information that people need to help you is in the other Thread.
I am suggesting to the moderator to merge this back into the first Thread.