I just got my stepper motor running with this code.
/********************************************************
** More info about the project at: **
** http://lusorobotica.com/index.php?topic=106.0 **
** by TigPT at [url=http://www.LusoRobotica.com]www.LusoRobotica.com[/url] **
*********************************************************/
int dirPin = 2;
int stepperPin = 3;
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
}
void step(boolean dir,int steps){
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepperPin, LOW);
delayMicroseconds(100);
}
}
void loop(){
step(true,1600);
delay(500);
step(false,1600*5);
delay(500);
}
Now I want to join the button code. So I can controll the motor. Ex. One push one relovution.
But I dont really know how I'm gona manage this with the code abowe.
I thought I would be pretty simple, my "solution";
const int buttonPin = 7; // the number of the pushbutton pin
int dirPin = 2;
int stepperPin = 3;
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void step(boolean dir,int steps){
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepperPin, LOW);
delayMicroseconds(100);
}
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
step(true,1600);
}
else {
step(false,1600);
}
}
But I dont really know how I'm gona manage this with the code abowe.
Do you KNOW what that code does? Have you made changes and observed the effects?
If so, making the motor step once each time you press the switch is trivial.
You are stepping one way while the switch is held down (assuming you wired the switch correctly) and stepping the other way when the switch is released. Is that what you want?
What does that code actually do, and how does it differ from what you want it to do?
Just so behaves stepper motor now. What I want is that the stepper motor to be disabled. Then press the button and release it spins the engine one turn.
I did not know where I would start, hence I took the code above.
What I want is that the stepper motor to be disabled. Then press the button and release it spins the engine one turn.
You need to detect when the switch transitions from released to pressed, and from pressed to released. This requires, of course. keeping track of the previous state of the switch.
int prevState = LOW;
int currState;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
// A transition occurred
}
prevState = currState;
}
Where the transition occurs, the current state tells you whether the transition was from released to pressed or from pressed to released. You can then step when the switch is pressed or when it is released. Your choice.
It depends on how often it receives the command from outside.
What I build is a cat feeder. I will have an ethernet shield that reads a feeding schedule from my server.
Then on the cat feeder, it will be a push button to force feeding.
Right now I'm just trying to get the foundation of the system work.