accelerometer adxl 335 and motor direction

Dependencies:   DcMotorDirectDrive Motor mbed adxl335_motor_direction

Dependents:   adxl335_motor_direction

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Motor.h"
00003  
00004 AnalogIn inputx(p20); // input pins 20,19,18 for x,y,z axis respectively.
00005 AnalogIn inputy(p19);
00006 
00007 Motor m1(p23,p5,p6); // pwm, fwd, rev
00008 Motor m2(p22,p11,p12); // pwm,fwd,rev
00009 
00010 Serial pc(USBTX,USBRX);  //Serial class for transmission of serial data
00011  
00012 int main() 
00013 {
00014 pc.baud(9600);   // fixing a constant baud rate of 9600 bps at which mbed will interact with computer
00015 float cal_x,cal_y;  //caliberation variables
00016 int x,y; // variables for x,y,z axes
00017 int i=100;
00018 while(i--)      //small timed loop to caliberate the accelerometer with currentl position of the accelerometer
00019 {
00020 cal_x=inputx*100;
00021 cal_y=inputy*100;
00022 
00023 }
00024 while(1)
00025     {  
00026         x=4*(cal_x - inputx*100);
00027         y=4*(cal_y - inputy*100);
00028  
00029   
00030     y=(y-30);
00031       pc.printf("x=%d,y=%d *",x,y);
00032       pc.printf("\n");
00033      
00034     if(x <= 0 && y <=0)
00035    {
00036        for (float s= -1.0; s > -1000.0 ; s += -0.01)
00037       
00038        m1.speed(s); // anticlockwise
00039       for (float s= -1.0; s > -1000.0 ; s += -0.01)  
00040        m2.speed(s);
00041     
00042    }
00043    
00044      
00045     else if (x>=0 && y>=0)
00046    { for (float s= 1.0; s < 1000.0 ; s += 0.01)
00047        m1.speed(s); // clockwise
00048       for (float s= 1.0; s < 1000.0 ; s += 0.01)
00049       m2.speed(s);                                                                                                 
00050   
00051    }
00052     
00053     else
00054     {
00055         m1.speed(0);
00056         }
00057        
00058        
00059      
00060         //passing the caliberated x,y,z values to the computer.
00061         }  
00062        
00063 }