electro-jerry:
Im not 100% on how the code step counter works. As I understand it is used to count how many steps are moved each time the button is pressed.
I don't think there is any alternative to becoming 100% certain - especially as you have a project with the possibility of damage being caused to your mechanism.
Putting limit switches to protect both ends of the motor travel is another option. But it is more complicated mechanically and does not really reduce the programmers burden either.
When you understand the step counter code my suggestion will probably make more sense.
And if you need help understanding the step counter code you need to explain exactly what you don't understand. My sense (from this distance) is that it is not so much uncertainty about the step counter code as uncertainty about the whole process of logic needed for your control system. Humans "know" how do do things without bringing into consciousness all the detail that is needed to do them. You must bring everything into consciousness for a computer program.
Here is a stripped down version which "should" do what you want. I wrote it in notepad and therefore can't compile or test it so there might be some syntax or formatting errors but the concept should work and is hopefully straight forward enough for you to understand. You can always add bells and whistles but, if you are anything like me, it's hard to grasp until you can at least see it work on a basic level.
int stepCount;
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
pinMode(2, INPUT_PULLUP); // move down and open nozzle
pinMode(3, INPUT_PULLUP); // move up and close nozzle
pinMode(4, INPUT_PULLUP); //return home switch
while (digitalRead(4) == HIGH) //As long pin 4 is HIGH (limit switch is open)
{ //move towards limit switch
digitalWrite(9, HIGH);
delayMicroseconds(500);
digitalWrite(9, LOW);
delayMicroseconds(500);
}
stepCount == 0; //when limit switch is reached set stepCount to zero
}
void loop() {
if (digitalRead(3) == LOW && stepCount < 3000) //stepper closes nozzle and counts steps
{
digitalWrite(8, LOW); //button is released or steps reach 3000
digitalWrite(9, HIGH);
delayMicroseconds(500);
digitalWrite(9, LOW);
delayMicroseconds(500);
stepCount = stepCount + 1;
}
if (digitalRead(2) == LOW && stepCount > 0) //stepper opens and counts steps until
{
digitalWrite(8, HIGH); //button is released or steps reach 0
digitalWrite(9, HIGH);
delayMicroseconds(500);
digitalWrite(9, LOW);
delayMicroseconds(500);
stepCount = stepCount - 1;
}
}
It is lovely and simple, plus I understand why and what it does!!!!
I promise not to be one of the people which spam the group for a few weeks whilst they are doing a project, ill do my best to continue playing with my Arduino.
stepCount == 0; //when limit switch is reached set stepCount to zero
}
The comment explains what should happen, but the code does something else. The code still works but only because stepCount was already zero from when it was declared. This really should be fixed.
stepCount == 0; //when limit switch is reached set stepCount to zero
}
The comment explains what [u]should[/u] happen, but the code does something else. The code still works but only because stepCount was already zero from when it was declared. This really should be fixed.
Thanks Bob, my mistake! I did say I might have some errors in there. LOL Jerry, change that to
Jerry I did want to comment that with the simplified code there is no protection against someone pressing the open and close button simultaneously. While this could be worked out in the coding I think a far simpler solution would be to use an "on"-"off"-"on" momentary switch to control it which would mechanically prevent actuating both directions at the same time. Something like this: 2 Pieces RED Waterproof boot cap DPDT momentary Toggle switch 2x ON/OFF/ON 30a 731840993251 | eBay
Cheers for the switch idea, iv order a similar model. Should look a lot neater!
As im not worried about power consumption do I need to worry about leaving the steppers energised all day or should I add some sort of sleep function in. (Going to be o for about 6 hours)
(no resistance in the mechanism so no chance of it turning the steppers back.
I'm pretty new to stepper motors myself so I'm not really sure although my gut tells me they should be able to be powered and hold position indefinitely.
electro-jerry:
As im not worried about power consumption do I need to worry about leaving the steppers energised all day or should I add some sort of sleep function in. (Going to be o for about 6 hours)
Assuming you have the current limit set correctly on your stepper driver so that the motor is not overloaded they should be able to remain powered up indefinitely. The motor will probably be quite hot (uncomfortable to touch) so they should not be in an enclosed space where the heat cannot escape.