Hi,
I'm new @ Arduino family, I would like to create this simple project with Arduino Mega 2560 and AF motor shield:
The stepper motor has "to move" until the photo interrupter is HIGH and Stop then after few seconds restart to opposite direction to another photo interrupter and when is HIGH will Stop again.
noflyzone25:
This is my simple code. I need to add code to stop the motor when photo interrupter is HIGH and restart to the next photo interrupter :
I presume that means that it should stop when a HIGH is detected and move again when the next HIGH is detected and continue with that sequence.
How many steps should the motor take between HIGHs of the interrupter - or should it run continuously?
What is the minimum time between subsequent HIGHs?
In simple terms you need a variable to record whether the motor should be moving or not (call it motorRunning). When a HIGH is detected it will toggle the value between true and false. Then that value will determine whether the motor runs - something like this
void loop() {
// other stuff
if (motorRunning == true) {
// move motor 1 step
}
}
This is my code no errors.... but the motor doesn't start:
#include <AFMotor.h>
AF_Stepper motor(48, 1);
int buttonPin=15; //interrupter
int buttonState;
int motorRun;
int motorPauseStartMillis;
int motorPause;
int motorPauseIntervalMillis;
int stepIntervalMillis;
int prevStepMillis;
and is easy to copy to a text editor. It makes life so much easier.
I presume you have already tried another test program to prove that the motor actually works?
Try these changes
byte buttonPin=15; //interrupter
boolean buttonState;
boolean motorRun = false;
unsigned long motorPauseStartMillis;
boolean motorPause = false;
unsigned long motorPauseIntervalMillis;
unsigned long stepIntervalMillis = 100; // YOU need to figure out this value
unsigned long prevStepMillis;
Note that variable associated with millis() should always be unsigned long.
However I think the real problem is that you did not give stepIntervalMillis any value.
What have you attached to Pin 15 ? Try a simple button to start with.
When programs don't work the first thing to do is add some Serial,println() statements so you can see if the variables have the value you expect.
You need to add some Serial.println() statements to your code so you can see what it is actually doing. Printing the value of loopCount would be a good start.
Successful debugging requires patience and hard thinking.
It would also be a big help if you use the AutoFormat tool to lay out your code so it is easier to read.