Doesn't hit the ends

Dependencies:   Motordriver QEI mbed

main.cpp

Committer:
meikefrok
Date:
2016-10-21
Revision:
0:e881319cfd74

File content as of revision 0:e881319cfd74:

#include "QEI.h"
#include "mbed.h"
#include "motordriver.h"

//======== Serial Communication ================================================
Serial pc(USBTX, USBRX);


//======== Motor and QEI =======================================================
QEI wheel (D12, D13, NC, 62);

//Safety for Motor
int Brakeable;
int sign;

//Motor
Motor A(D6, D7, D7, Brakeable); // pwm, fwd, rev, brake          LEFT


//======== Miscellaneous =======================================================
AnalogIn pot(A1);               //Potmeter
DigitalOut led(LED_GREEN);      //Green LED
InterruptIn btn(SW2);           //Button
InterruptIn btn2(SW3);          //Button


//======== Variables ===========================================================
float factor = 0.03279;
int position;
float ain;   /* Variable to store the analog input*/




//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//======== Functions and main ==================================================
int main() {
   while(1){
   
   if(btn && btn2) {   
        A.speed(0)==0;
      
   }else if(btn && !btn2){
        if(position <= -105){
        A.speed(0.2)==0.2;
    
        }else{
        A.speed(-0.5)==-0.5;
        }
    
    }else if(!btn && btn2){
        if(position >= 105){
        A.speed(-0.2)==-0.2;
    
        }else{
        A.speed(0.5)==0.5;
        }
    }
    
    else{
    A.speed(0)==0;
    }   
    
    position = (wheel.getPulses()*factor) ; // position of the motor      
    ain = pot.read();
   
            if (ain == 0){
                wheel.reset();   
            }
            else {}
        wait(0.1);
        pc.baud(115200);
        pc.printf("Afgelegde milimeters is: %i\n", position);
    }

}