Aaron Stevenson / Mbed 2 deprecated Assignment3

Dependencies:   mbed MCP23017 mbed-rtos WattBob_TextLCD

Revision:
1:c2c4bd530112
Parent:
0:1580a6dbd6a9
Child:
2:51a06b9a52d1
diff -r 1580a6dbd6a9 -r c2c4bd530112 main.cpp
--- a/main.cpp	Wed Mar 21 11:51:37 2018 +0000
+++ b/main.cpp	Wed Mar 21 12:50:48 2018 +0000
@@ -13,102 +13,153 @@
 AnalogIn accel(p15);       // Analog
 AnalogIn brake(p16);
 float speed = 0;
+float aveSpeed;
 float acc = 0;
 float br = 0;
 float dif;
+float distance = 0;
 
 Timer tim;
 long int t;
 
-DigitalIn  engine(p5);
+DigitalOut sideLights(LED1);
+DigitalOut lIndicator(LED2);
+DigitalOut rIndicator(LED3);
 DigitalOut engLight(LED4);
 
 DigitalOut brakeLights(p6);
-
 DigitalOut fluxCapacitor(p7);
 
-float distance = 0;
-
 DigitalIn  lightSwitch(p8);
-DigitalOut sideLights(LED1);
-
-DigitalOut lIndicator(LED2);
-DigitalOut rIndicator(LED3);
-
 DigitalIn  lIndicate(p9);
 DigitalIn  rIndicate(p10);
-
-Thread thread;
+DigitalIn  engine(p5);
 
-void acceleration() {//run at 20hz                  //  1  read brake and accelerator       10
-    acc = accel.read()*3.3;
-    br = brake.read()*3.3;
+Thread sp;
+Thread task1;
+Thread task2;
+Thread task3;
+Thread task4;
+Thread task5;
+Thread task6;
+Thread task7;
+Thread task8;
+Thread task9;
+Thread task10;
+
+
+void acceleration()  //run at 20hz                  //  1  read brake and accelerator       10
+{
+    while (1) {
+        acc = accel.read()*3.3;
+        br = brake.read()*3.3;
+        Thread::wait(500);
+    }
 }
-void getSpeed(){                                    // 20Hz
-    dif = acc - br;
-    t = tim.read();
-    if (dif<0) {
-        speed = speed - (dif*t);
-    } else {
-        speed = speed + (dif*t);
-    }
-    speed = speed * 3.6;
-    t = tim.read();
-    distance = speed*t + (0.5*dif*(t*t));
-    distance = distance/1000;
-    return speed;
-}
-void ignition(){                                    //  2   Read engine on/off show LED     2
-    if (engine == 1) {
-        engLight = 1;
+void getSpeed()                                     // 20Hz
+{
+    while (1) {
+        dif = acc - br;
+        t = tim.read();
+        speed = speed + (dif*t*3.6);
+        if (speed < 0) {
+            speed = 0;
+        }
+        distance = distance + (speed*t + (0.5*dif*(t*t)))/1000;
+        tim.reset();
+
+        Thread::wait(100);
     }
 }
-void speedo(){                                      //  3   Average last n speed readings   2
-    for (int i = 0; i<3; i++) {
-        speed = speed + getSpeed();
+void ignition()                                     //  2   Read engine on/off show LED     2
+{
+    while (1) {
+        if (engine == 1) {
+            engLight = 1;
+        }
+
+        Thread::wait(500);
     }
-    speed = speed/4;
 }
-void braking(){                                     //  4   Brake indicated by LED          2
-    if ( br>0){
-        brakeLights = 1;
+void speedo()                                       //  3   Average last n speed readings   2
+{
+    while (1) {
+        for (int i = 0; i<3; i++) {
+            aveSpeed = speed + aveSpeed;
+        }
+        speed = speed/4;
+
+        Thread::wait(200);
     }
 }
-void greatScott(){                                  //  5   if speed > 88 LED on            1
-    if (speed > 88){
-        fluxCapacitor = 1;    
+void braking()                                      //  4   Brake indicated by LED          2
+{
+    while (1) {
+        if ( br>0) {
+            brakeLights = 1;
+        }
+
+        Thread::wait(500);
     }
 }
-void LCD(){                                         //  6   display odometer and speed      2
-    t = tim.read();
-    distance = speed * (t/3600);
-    
-    lcd->locate(0,0);
-    lcd->printf("KM:%0.1f",distance);
-    lcd->locate(1,0);
-    lcd->printf("KMPH:%0.1f",speed);
+void greatScott()                                   //  5   if speed > 88 LED on            1
+{
+    while (1) {
+        if (speed > 88) {
+            fluxCapacitor = 1;
+        }
+
+        Thread::wait(1000);
+    }
+}
+void LCD()                                          //  6   display odometer and speed      2
+{
+    while (1) {
+        t = tim.read();
+        distance = speed * (t/3600);
+
+        lcd->locate(0,0);
+        lcd->printf("KM:%0.1f",distance);
+        lcd->locate(1,0);
+        lcd->printf("KMPH:%0.1f",speed);
+
+        Thread::wait(500);
+    }
 }
 //    7       Send speed, acc, brake to 100       0.2
 //           element MAIL q MBED RTOS
 //    8       MAIL q to serial PC                 0.05
-void lights(){                                      //  9   side light switch, set lights   1
-    if (lightSwitch == 1){
-        sideLights = 1;    
+void lights()                                       //  9   side light switch, set lights   1
+{
+    while (1) {
+        if (lightSwitch == 1) {
+            sideLights = 1;
+        }
+        else{
+            sideLights = 0;    
+        }
+
+        Thread::wait(1000);
     }
 }
-void indicators(){                                  //    10.     Read indicator switches, flash 
-    if ((lIndicate == 1) && (rIndicate == 1)){      //      both LED at 2Hz If both switch on 
+void indicators()                                   //    10.     Read indicator switches, flash
+{
+    while (1) {
+        if ((lIndicate == 1) && (rIndicate == 1)) {     //      both LED at 2Hz If both switch on
             lIndicator = !lIndicator;
             rIndicator = !rIndicator;
-            Thread::wait(2000);
-    }
-    if ((lIndicate == 1) && (rIndicate == 0)){      //        if left switch on
+            //   Thread::wait(2000);
+        }
+        if ((lIndicate == 1) && (rIndicate == 0)) {     //        if left switch on
             lIndicator = !lIndicator;               //          left LED at 1Hz
-            Thread::wait(1000);
-    }
-    if ((lIndicate == 0) && (rIndicate == 1)){      //        if right switch on
+            // Thread::wait(1000);
+        }
+        if ((lIndicate == 0) && (rIndicate == 1)) {     //        if right switch on
             rIndicator = !rIndicator;               //          right LED at 1Hz
-            Thread::wait(1000);
+            // Thread::wait(1000);
+        }
+
+        Thread::wait(2000);
     }
 }
 
@@ -117,9 +168,20 @@
     par_port = new MCP23017(p9, p10, 0x40);             // initialise 16-bit I/O chip
     lcd = new WattBob_TextLCD(par_port);                // initialise 2*26 char display
     par_port->write_bit(1,BL_BIT);                      // turn LCD backlight ON
-    while(1) {
-    }
+
+    sp.start(getSpeed);
+    task1.start(acceleration);
+    task2.start(ignition);
+    task3.start(speedo);
+    task4.start(braking);
+    task5.start(greatScott);
+    task6.start(LCD);
+    //task7.start();
+    //task8.start();
+    task9.start(lights);
+    task10.start(indicators);
+
 }
 
 
-    
+