Photo Interrrupter - motor shield

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.

Thank you for your help.

This Simple Stepper Program and Stepper Motor Basics may have some useful ideas.

...R

Thank you for the answers,

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 :

#include <AFMotor.h>
AF_Stepper motor(48, 1);

void setup() {

motor.setSpeed(200);

motor.step(1000, FORWARD, SINGLE);

motor.release();
delay(10000);
}

void loop()
{
motor.step(10000,FORWARD, SINGLE);

//Missed code to stop the motor when the photo interrupter is HIGH

motor.step(10000,BACKWARD, SINGLE);

//Missed code to restart the motor to the next photo interrupter (HIGH)

}

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
   }
}

...R

Thank you Robin2,
the motor should run continuously until the interrupter and stop, delay 4 seconds, and restart to the other interrupter and stop.

I have no idea how to write this simple code :slight_smile:

Something like this - not complete and not tested

void loop() {
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
        motorRun = ! motorRun; // change the state
        if (motorRun == false) {
            motorPauseStartMillis = millis();
            motorPause = true;
        }
    }
    if (millis() - motorPauseStartMillis >= motorPauseIntervalMillis) {
        motorPause = false;
    }
    if (motorRun == true && motorPause == false)
        if (millis() - prevStepMillis >= stepIntervalMillis) {
            prevStepMillis += stepIntervalMillis;
            motor.step(1, FORWARD, SINGLE); // not sure if this is correct way to get 1 step
    }
}

You will have to figure out the value for stepIntervalMillis to get the speed you want.

...R

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;

void setup() {
Serial.begin(9600);
motor.setSpeed(100);

}

void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
motorRun = ! motorRun; // change the state
if (motorRun == false) {
motorPauseStartMillis = millis();
motorPause = true;
}
}
if (millis() - motorPauseStartMillis >= motorPauseIntervalMillis) {
motorPause = false;
}
if (motorRun == true && motorPause == false)
if (millis() - prevStepMillis >= stepIntervalMillis) {
prevStepMillis += stepIntervalMillis;
motor.step(1, FORWARD, SINGLE); // not sure if this is correct way to get 1 step
}
}

Any suggestions?? Now I'm lost :-))

Please use the code button </>

so your code looks like this

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.

...R

SOLVED.......!!!!!!!! Now It works....!!!! :slight_smile:

I need your help for the last question, I would like to run the void loop 8 times and then the program will stop.... any suggestions?

This is the final code:

#include <AFMotor.h>

AF_DCMotor motor3(3);

#define analog0 A8 
#define led0 16
 
int statoLed0 = 0;
int val;
int val2;

 
void setup()
{
    pinMode( analog0, INPUT );
    pinMode( led0, OUTPUT );
    pinMode(15,INPUT);
    pinMode(14,INPUT);
 
    digitalWrite( led0, statoLed0 );

motor3.setSpeed(127);

      
}
 
void loop()
{
val=digitalRead(15); 
val2=digitalRead(14); 
if(val==LOW && val2==LOW) 
{


  if ( analogRead(analog0) > 1000 )
  {
    if ( statoLed0 == LOW  ) statoLed0 = HIGH;
    else                     statoLed0 = LOW;
    digitalWrite( led0, statoLed0 );
    motor3.run(FORWARD);
    }
  
 }

   if(val==HIGH && val2==LOW) 
{

    motor3.run(BACKWARD);
    }
  
if(val==LOW && val2==HIGH) 
{

    motor3.run(FORWARD);
    }
   
  

   
   }

noflyzone25:
I need your help for the last question, I would like to run the void loop 8 times and then the program will stop.... any suggestions?

Just add a variable that increments each time loop() repeats and then add this

if (loopCount > 8) {
   while(true) {
   }
}

The while(true) will loop for ever and the only way to stop it is to switch off your Arduino.

...R

Thank you Robin2,
I tried ... no errors....but now it doesn't start anything .... :-((

#include <AFMotor.h>
#include <Servo.h>

Servo myservo;
int pos = 0;  

AF_DCMotor motor3(3);

#define analog0 A8 
#define led0 16
 
int statoLed0 = 0;
int val;
int val2;
int loopCount = 0;
 
void setup()
{
    pinMode( analog0, INPUT );
    pinMode( led0, OUTPUT );
    pinMode(15,INPUT);
    pinMode(14,INPUT);
 
    digitalWrite( led0, statoLed0 );
    myservo.attach(9);

motor3.setSpeed(127);

      
}
 
void loop()
{
val=digitalRead(15); 
val2=digitalRead(14); 
if (loopCount > 8) {
   while(true) {
   }

}

if(val==LOW && val2==LOW) 
{


  if ( analogRead(analog0) > 1000 )
  {
    if ( statoLed0 == LOW  ) statoLed0 = HIGH;
    else                     statoLed0 = LOW;
    digitalWrite( led0, statoLed0 );
    motor3.run(FORWARD);
    }
  
 }

   if(val==HIGH && val2==LOW) 
{


    motor3.run(BACKWARD);
    
    
    }
  
if(val==LOW && val2==HIGH) 
{
motor3.run(RELEASE);
delay(10000);
motor3.run(FORWARD);
delay(1000);
motor3.run(RELEASE);
delay(1000);
 
for (pos = 0; pos <= 180; pos += 180) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1000);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 180) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1000);                       // waits 15ms for the servo to reach the position
  }
  
    motor3.run(FORWARD);
    delay(10000);
    }
   

   loopCount++;
  }

I suspect you don't actually want to count the iterations of the loop() function as it probably gets to 8 in a millisecond or so.

What exactly to you want to happen 8 times?
Put the counter there.

...R

I would like to repeat the functions of void loop 8 times and then stop it.... :wink:

Thank you for your time Robin2.... really thank you

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.

...R