with optic sensor

Dependencies:   Stepper mbed PinDetect

main.cpp

Committer:
rschimpf78
Date:
2019-09-04
Revision:
6:7cd69300073f
Parent:
5:1b451520ed5f

File content as of revision 6:7cd69300073f:

#include "mbed.h"
#include "Stepper.h"
#include "string"
DigitalIn optic(D5);
Stepper mot(D3,D2); //(PUL+,DIR+)
DigitalOut en(D4);
Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
int main(){
     en = 1;
    int count = 0;
    int old_optic=0;
    int new_optic;
    optic.mode(PullDown);
    wait(0.01);
    int slot = 0;
 
    while(1) {
        mot.stop();
        en = 1;
        
        pc.printf("\n\n\nEnter desired position: ");
        pc.scanf("%d",&slot); 
      

        while (slot>count){
        
        en = 0;
        wait(.001);
        mot.setSpeed(300);
        wait(.001);
        mot.rotate(1);
        new_optic = optic;
        
        if ((new_optic==1) && (old_optic==0)){
                myled4 = count & 0x01;
                 myled3 = (count & 0x02)>>1;
              myled2 = (count & 0x04)>>2;
                    myled = (count & 0x08)>>3;
                    count++;
                    pc.printf("\ncount:%ld",count);
                }
        old_optic = new_optic;
        
        }
     
        while (slot<count){
            
        en = 0;
        wait(.001);
        mot.setSpeed(300);
        wait(.001);
        mot.rotate(0);
        new_optic = optic;
        
             if ((new_optic==1) && (old_optic==0)){
                myled4 = count & 0x01;
                myled3 = (count & 0x02)>>1;
                myled2 = (count & 0x04)>>2;
                myled = (count & 0x08)>>3;
                count--;
                pc.printf("\ncount:%ld",count);
                }
        
        old_optic = new_optic;
        }
        
     
   }
}