with optic sensor

Dependencies:   Stepper mbed PinDetect

main.cpp

Committer:
rschimpf78
Date:
2019-09-04
Revision:
2:65c5232b9465
Parent:
0:88834eed3de0
Child:
3:5410ddd83592

File content as of revision 2:65c5232b9465:

#include "mbed.h"
#include "Stepper.h"
#include "string"
DigitalIn optic(D5);
Stepper mot(D3,D2); //(PUL+,DIR+)
DigitalOut en(D4);
Serial pc(USBTX, USBRX);

int main()
{
optic.mode(PullDown);
wait(0.1);
int steps = 0;
int slot = 0;
en = 1; // stepper motor off
wait(1);
mot.setSpeed(400); //stepper speed
mot.setPositionZero(); //set origin for stepper steps 
while(1)
  {
        pc.printf("\n\n\nEnter desired position: ");
        pc.scanf("%d",&slot);  
        en = 0; //turns on stepper driver
        mot.goesTo(slot*100); // move to absolute 100
        while(!mot.stopped()); // while motor is turning...do line below
        steps = mot.getPosition(); //read in absolute step postition
        while(optic == 0)
        {
        wait(1);
        mot.goesTo(steps+25); //go to absolute step position
        while(!mot.stopped());
        steps = mot.getPosition();
        pc.printf("\nNot in Position", steps);
        }
        pc.printf("\nIn Position");
        en = 1; //disable the motor and driver
        //if (tickcount<steps)
        //{
        //mot.goesTo(steps+25);
        //while(!mot.stopped());
        //steps = mot.getPosition();
        //pc.printf("\nNot in Position", steps);
        //}
        //if (tickcount>steps)
        //{
        //mot.goesTo(steps-25);
        //while(!mot.stopped());
        //steps = mot.getPosition();
        //pc.printf("\nNot in Position", steps);
        //}
    }
}