Code for testing scooter electronics

Dependencies:   mbed BH1750 BLE_API L3GD20H nRF51822

Revision:
1:aa9eabdefc12
Parent:
0:3cef86fc978f
diff -r 3cef86fc978f -r aa9eabdefc12 main.cpp
--- a/main.cpp	Tue Feb 15 22:22:20 2022 +0000
+++ b/main.cpp	Mon Feb 21 17:26:52 2022 +0000
@@ -1,9 +1,13 @@
                                                      
  #include "mbed.h"
+ 
  #include "BH1750.h"
+ #include "L3GD20H.h"
+ //#include <cmath>
  
  // I2C Communication
   BH1750      lum(P0_30,P0_7);    // BH1750 SDA, SCL
+  //L3GD20H ori(P0_30,P0_7); //PinName sda, PinName scl
   Serial pc(USBTX, USBRX); // tx, rx
   
   DigitalOut led(LED2);
@@ -11,6 +15,7 @@
   Ticker flipLed;
   float dutyCycle=1.0f;
 
+  short ori_rd[3];
   
 DigitalOut BH1750_EN0(P0_13);
 float lum0 = 0;
@@ -18,10 +23,40 @@
 //float lum1 = 0;
 
   
-// // If you connected I2C line not only this device but also other devices,
-// //     you need to declare following method.
-//  I2C         i2c(P0_7,P0_30);    // SDA, SCL
-//  BH1750      lum(i2c);         // BH1750 SDA, SCL (Data available every 120mSec)
+ // If you connected I2C line not only this device but also other devices,
+ //     you need to declare following method.
+ // I2C         i2c(P0_7,P0_30);    // SDA, SCL
+ // BH1750      lum(i2c);         // BH1750 SDA, SCL (Data available every 120mSec)
+
+float fround2(float f){
+    float temp = 100*f;
+    temp = floor(temp);
+    return temp/100;
+}
+
+float decideDutyCycle(float f){
+    if(f > 3500){
+        return 1.0f;
+    }
+    else if(f > 3000){
+        return 0.98f;
+    }
+    else if(f > 2500){
+        return 0.96f;
+    }
+    else if(f > 2000){
+        return 0.93f;
+    }
+    else if(f > 1500){
+        return 0.89f;
+    }
+    else if(f > 1000){
+        return 0.83f;
+    }
+    else{
+        return 0.83*f/1000;
+    }
+}
 
 void flip(){
 //    if(tickerCntr1 %3000 == 0)
@@ -29,9 +64,14 @@
 //        
 //    if(dutyCycle<= 0)
 //        dutyCycle = 1;
-    if(dutyCycle<= 0)
+    if(lum0 == 0)
+        dutyCycle = 1;
+    else
+        dutyCycle = fround2(decideDutyCycle(lum0));
+        
+    if(dutyCycle < 0)
         dutyCycle = 0;
-    else if(dutyCycle >= 1)
+    else if(dutyCycle > 1)
         dutyCycle = 1;
             
     if(100*dutyCycle >= tickerCntr1%100){
@@ -42,21 +82,40 @@
     }
     tickerCntr1++;
     
-    if(tickerCntr1 ==100001 )
+    if(tickerCntr1 ==100000001 )
         tickerCntr1 = 0;
      
  }
  
 void measureBH1750_0(){
-     BH1750_EN0 = 0;
-     BH1750_EN1 = 1;
-     //wait(0.005);
-     return lum.lux();
+     //BH1750_EN0 = 1;
+     //BH1750_EN1 = 1;
+     // //wait(0.005);
+     
+     lum0 = lum.lux();
+     
  }
  
+ void printthis(){
+    if(tickerCntr1 % 6000 == 0){
+        lum0 = lum.lux();
+        //pc.printf("Illuminance 1: %+7.2f [Lux] DutyCycle: %+7.2f [decima'] \r\n", lum0, dutyCycle);
+        
+    }
+    else if(tickerCntr1 % 6000 == 3000){
+       // ori.read(ori_rd);
+        
+    }
+    //else if(tickerCntr1 % 10000 == 0)
+        //pc.printf("Illuminance 1: %+7.2f [Lux] DutyCycle: %+7.2f [decimal] XYZ ori: %+7.2f %+7.2f %+7.2f [decimal] \r\n", lum0, dutyCycle, ori_rd[0], ori_rd[1], ori_rd[2]);
+}
+     
+     
+
+ 
  float measureBH1750_1(){
      BH1750_EN0 = 1;
-     BH1750_EN1 = 0;
+     //BH1750_EN1 = 0;
     // wait(0.005);
      return lum.lux();
  } 
@@ -65,14 +124,23 @@
  int main() {;
    int reading_num = 0;
 
+  
+  //flipLed.attach_us(&flip, 50.0f);
+  //t1.attach(&printthis, 1.0f);
+  //pc.printf("Hi1 \r\n");
+  
+   while(true){
 
-  flipLed.attach_us(&flip, 50.0f);
-   while(true){
 //      lum1 = measureBH1750_1();
 //      lum0 = measureBH1750_0();
 //      pc.printf("Illuminance 1: %+7.2f [Lux] : Illuminance 2: %+7.2f [Lux] : %i\r\n", lum0, lum1, reading_num++);
+        
+        flip();
+        printthis();
+        wait_us(50.0f);
+        
 
-      wait(0.5);
+        
    }
    
  }
\ No newline at end of file