thread based dc motor direction

Dependencies:   Motor mbed-rtos mbed

Fork of rtos_basic by mbed official

Revision:
12:bbdc4c6d12d3
Parent:
11:0309bef74ba8
--- a/main.cpp	Wed Feb 15 14:04:02 2017 -0600
+++ b/main.cpp	Fri May 05 06:21:47 2017 +0000
@@ -1,22 +1,103 @@
 #include "mbed.h"
+#include "Motor.h"
 #include "rtos.h"
  
+AnalogIn inputx(p20); // input pins 20,19,18 for x,y,z axis respectively.
+AnalogIn inputy(p19);
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
-Thread thread;
- 
-void led2_thread() {
-    while (true) {
+DigitalOut led3(LED3);
+Motor m1(p23,p5,p6); // pwm, fwd, rev
+//Motor m2(p22,p11,p12); // pwm,fwd,rev
+
+Serial pc(USBTX,USBRX); //Serial class for transmission of serial data
+Thread thread;  
+
+ void led2_thread() {
+    //while (true) {
+         
         led2 = !led2;
-        Thread::wait(1000);
-    }
+        pc.printf("thread2 executed");
+        Thread::wait(500);
+      
+    //} 
+    
 }
  
-int main() {
-    thread.start(led2_thread);
+ void led1_thread() {
+   // while(true) {
+       
+        led1 = !led1;
+        pc.printf("thread1 executed");
+       
+       Thread::wait(500);
+     
+  //  }
+}
+
+void led3_thread() {
+    //while (true) {
+         pc.printf("invalid");
+         led3 = !led3;
+        //m2.speed(0);
+        Thread::wait(500);
+  //  }
+}
+
+
+int main() 
+{
+pc.baud(9600);   // fixing a constant baud rate of 9600 bps at which mbed will interact with computer
+float cal_x,cal_y;  //caliberation variables
+int x,y; // variables for x,y,z axes
+int i=100;
+while(i--)      //small timed loop to caliberate the accelerometer with currentl position of the accelerometer
+{
+cal_x=inputx*100;
+cal_y=inputy*100;
+
+}
+while(true)
+    {  
+     Thread::wait(500);
+        x=4*(cal_x - inputx*100);
+        y=4*(cal_y - inputy*100);
+ 
+  
+    y=(y-30);
+      pc.printf("x=%d,y=%d *",x,y);
+      pc.printf("\n");
+     
+    if(x >= 0 && y >=0)
+   {
+      pc.printf("\n Anticlockwise\n");
+        for (float s= 1.0; s < 10.0 ; s += 0.01)
+       m1.speed(s); // clockwise
+   thread.start(led1_thread);
+   }
+   
+     
+    else if (x <= 0 && y <=0)
+   { 
+     pc.printf("\n Clockwise \n");
+        for(float s= -1.0; s > -10.0 ; s += -0.01)
+        m1.speed(s); 
+      
+       // anticlockwise //Anticlockwise
+                //  led1_thread();                                                                                    
+  thread.start(led2_thread);
+   }
     
-    while (true) {
-        led1 = !led1;
-        Thread::wait(500);
-    }
-}
+   else  if((x<=0 && y>=0)|| (x>=0 && y<=0))
+    {
+         pc.printf("");
+         m1.speed(0); //stop
+         thread.start(led3_thread);
+        }
+       
+       Thread::wait(500);
+     
+        //passing the caliberated x,y,z values to the computer.
+        }  
+       
+}
\ No newline at end of file