with optic sensor

Dependencies:   Stepper mbed PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Stepper.h"
00003 #include "string"
00004 DigitalIn optic(D5);
00005 Stepper mot(D3,D2); //(PUL+,DIR+)
00006 DigitalOut en(D4);
00007 Serial pc(USBTX, USBRX);
00008 DigitalOut myled(LED1);
00009 DigitalOut myled2(LED2);
00010 DigitalOut myled3(LED3);
00011 DigitalOut myled4(LED4);
00012 int main(){
00013      en = 1;
00014     int count = 0;
00015     int old_optic=0;
00016     int new_optic;
00017     optic.mode(PullDown);
00018     wait(0.01);
00019     int slot = 0;
00020  
00021     while(1) {
00022         mot.stop();
00023         en = 1;
00024         
00025         pc.printf("\n\n\nEnter desired position: ");
00026         pc.scanf("%d",&slot); 
00027       
00028 
00029         while (slot>count){
00030         
00031         en = 0;
00032         wait(.001);
00033         mot.setSpeed(300);
00034         wait(.001);
00035         mot.rotate(1);
00036         new_optic = optic;
00037         
00038         if ((new_optic==1) && (old_optic==0)){
00039                 myled4 = count & 0x01;
00040                  myled3 = (count & 0x02)>>1;
00041               myled2 = (count & 0x04)>>2;
00042                     myled = (count & 0x08)>>3;
00043                     count++;
00044                     pc.printf("\ncount:%ld",count);
00045                 }
00046         old_optic = new_optic;
00047         
00048         }
00049      
00050         while (slot<count){
00051             
00052         en = 0;
00053         wait(.001);
00054         mot.setSpeed(300);
00055         wait(.001);
00056         mot.rotate(0);
00057         new_optic = optic;
00058         
00059              if ((new_optic==1) && (old_optic==0)){
00060                 myled4 = count & 0x01;
00061                 myled3 = (count & 0x02)>>1;
00062                 myled2 = (count & 0x04)>>2;
00063                 myled = (count & 0x08)>>3;
00064                 count--;
00065                 pc.printf("\ncount:%ld",count);
00066                 }
00067         
00068         old_optic = new_optic;
00069         }
00070         
00071      
00072    }
00073 }