![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Code for testing scooter electronics
Dependencies: mbed BH1750 BLE_API L3GD20H nRF51822
main.cpp
- Committer:
- wonin
- Date:
- 2022-02-21
- Revision:
- 1:aa9eabdefc12
- Parent:
- 0:3cef86fc978f
File content as of revision 1:aa9eabdefc12:
#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); int tickerCntr1 = 0; Ticker flipLed; float dutyCycle=1.0f; short ori_rd[3]; DigitalOut BH1750_EN0(P0_13); float lum0 = 0; //DigitalOut BH1750_EN1(P0_14); //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) 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) // dutyCycle -= 0.01; // // if(dutyCycle<= 0) // dutyCycle = 1; if(lum0 == 0) dutyCycle = 1; else dutyCycle = fround2(decideDutyCycle(lum0)); if(dutyCycle < 0) dutyCycle = 0; else if(dutyCycle > 1) dutyCycle = 1; if(100*dutyCycle >= tickerCntr1%100){ led = 1; } else{ led = 0; } tickerCntr1++; if(tickerCntr1 ==100000001 ) tickerCntr1 = 0; } void measureBH1750_0(){ //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; // wait(0.005); return lum.lux(); } int main() {; int reading_num = 0; //flipLed.attach_us(&flip, 50.0f); //t1.attach(&printthis, 1.0f); //pc.printf("Hi1 \r\n"); 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); } }