Code for testing scooter electronics
Dependencies: mbed BH1750 BLE_API L3GD20H nRF51822
main.cpp@1:aa9eabdefc12, 2022-02-21 (annotated)
- Committer:
- wonin
- Date:
- Mon Feb 21 17:26:52 2022 +0000
- Revision:
- 1:aa9eabdefc12
- Parent:
- 0:3cef86fc978f
Working:; LED array; LIght Sensor; Dimmer;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wonin | 0:3cef86fc978f | 1 | |
wonin | 0:3cef86fc978f | 2 | #include "mbed.h" |
wonin | 1:aa9eabdefc12 | 3 | |
wonin | 0:3cef86fc978f | 4 | #include "BH1750.h" |
wonin | 1:aa9eabdefc12 | 5 | #include "L3GD20H.h" |
wonin | 1:aa9eabdefc12 | 6 | //#include <cmath> |
wonin | 0:3cef86fc978f | 7 | |
wonin | 0:3cef86fc978f | 8 | // I2C Communication |
wonin | 0:3cef86fc978f | 9 | BH1750 lum(P0_30,P0_7); // BH1750 SDA, SCL |
wonin | 1:aa9eabdefc12 | 10 | //L3GD20H ori(P0_30,P0_7); //PinName sda, PinName scl |
wonin | 0:3cef86fc978f | 11 | Serial pc(USBTX, USBRX); // tx, rx |
wonin | 0:3cef86fc978f | 12 | |
wonin | 0:3cef86fc978f | 13 | DigitalOut led(LED2); |
wonin | 0:3cef86fc978f | 14 | int tickerCntr1 = 0; |
wonin | 0:3cef86fc978f | 15 | Ticker flipLed; |
wonin | 0:3cef86fc978f | 16 | float dutyCycle=1.0f; |
wonin | 0:3cef86fc978f | 17 | |
wonin | 1:aa9eabdefc12 | 18 | short ori_rd[3]; |
wonin | 0:3cef86fc978f | 19 | |
wonin | 0:3cef86fc978f | 20 | DigitalOut BH1750_EN0(P0_13); |
wonin | 0:3cef86fc978f | 21 | float lum0 = 0; |
wonin | 0:3cef86fc978f | 22 | //DigitalOut BH1750_EN1(P0_14); |
wonin | 0:3cef86fc978f | 23 | //float lum1 = 0; |
wonin | 0:3cef86fc978f | 24 | |
wonin | 0:3cef86fc978f | 25 | |
wonin | 1:aa9eabdefc12 | 26 | // If you connected I2C line not only this device but also other devices, |
wonin | 1:aa9eabdefc12 | 27 | // you need to declare following method. |
wonin | 1:aa9eabdefc12 | 28 | // I2C i2c(P0_7,P0_30); // SDA, SCL |
wonin | 1:aa9eabdefc12 | 29 | // BH1750 lum(i2c); // BH1750 SDA, SCL (Data available every 120mSec) |
wonin | 1:aa9eabdefc12 | 30 | |
wonin | 1:aa9eabdefc12 | 31 | float fround2(float f){ |
wonin | 1:aa9eabdefc12 | 32 | float temp = 100*f; |
wonin | 1:aa9eabdefc12 | 33 | temp = floor(temp); |
wonin | 1:aa9eabdefc12 | 34 | return temp/100; |
wonin | 1:aa9eabdefc12 | 35 | } |
wonin | 1:aa9eabdefc12 | 36 | |
wonin | 1:aa9eabdefc12 | 37 | float decideDutyCycle(float f){ |
wonin | 1:aa9eabdefc12 | 38 | if(f > 3500){ |
wonin | 1:aa9eabdefc12 | 39 | return 1.0f; |
wonin | 1:aa9eabdefc12 | 40 | } |
wonin | 1:aa9eabdefc12 | 41 | else if(f > 3000){ |
wonin | 1:aa9eabdefc12 | 42 | return 0.98f; |
wonin | 1:aa9eabdefc12 | 43 | } |
wonin | 1:aa9eabdefc12 | 44 | else if(f > 2500){ |
wonin | 1:aa9eabdefc12 | 45 | return 0.96f; |
wonin | 1:aa9eabdefc12 | 46 | } |
wonin | 1:aa9eabdefc12 | 47 | else if(f > 2000){ |
wonin | 1:aa9eabdefc12 | 48 | return 0.93f; |
wonin | 1:aa9eabdefc12 | 49 | } |
wonin | 1:aa9eabdefc12 | 50 | else if(f > 1500){ |
wonin | 1:aa9eabdefc12 | 51 | return 0.89f; |
wonin | 1:aa9eabdefc12 | 52 | } |
wonin | 1:aa9eabdefc12 | 53 | else if(f > 1000){ |
wonin | 1:aa9eabdefc12 | 54 | return 0.83f; |
wonin | 1:aa9eabdefc12 | 55 | } |
wonin | 1:aa9eabdefc12 | 56 | else{ |
wonin | 1:aa9eabdefc12 | 57 | return 0.83*f/1000; |
wonin | 1:aa9eabdefc12 | 58 | } |
wonin | 1:aa9eabdefc12 | 59 | } |
wonin | 0:3cef86fc978f | 60 | |
wonin | 0:3cef86fc978f | 61 | void flip(){ |
wonin | 0:3cef86fc978f | 62 | // if(tickerCntr1 %3000 == 0) |
wonin | 0:3cef86fc978f | 63 | // dutyCycle -= 0.01; |
wonin | 0:3cef86fc978f | 64 | // |
wonin | 0:3cef86fc978f | 65 | // if(dutyCycle<= 0) |
wonin | 0:3cef86fc978f | 66 | // dutyCycle = 1; |
wonin | 1:aa9eabdefc12 | 67 | if(lum0 == 0) |
wonin | 1:aa9eabdefc12 | 68 | dutyCycle = 1; |
wonin | 1:aa9eabdefc12 | 69 | else |
wonin | 1:aa9eabdefc12 | 70 | dutyCycle = fround2(decideDutyCycle(lum0)); |
wonin | 1:aa9eabdefc12 | 71 | |
wonin | 1:aa9eabdefc12 | 72 | if(dutyCycle < 0) |
wonin | 0:3cef86fc978f | 73 | dutyCycle = 0; |
wonin | 1:aa9eabdefc12 | 74 | else if(dutyCycle > 1) |
wonin | 0:3cef86fc978f | 75 | dutyCycle = 1; |
wonin | 0:3cef86fc978f | 76 | |
wonin | 0:3cef86fc978f | 77 | if(100*dutyCycle >= tickerCntr1%100){ |
wonin | 0:3cef86fc978f | 78 | led = 1; |
wonin | 0:3cef86fc978f | 79 | } |
wonin | 0:3cef86fc978f | 80 | else{ |
wonin | 0:3cef86fc978f | 81 | led = 0; |
wonin | 0:3cef86fc978f | 82 | } |
wonin | 0:3cef86fc978f | 83 | tickerCntr1++; |
wonin | 0:3cef86fc978f | 84 | |
wonin | 1:aa9eabdefc12 | 85 | if(tickerCntr1 ==100000001 ) |
wonin | 0:3cef86fc978f | 86 | tickerCntr1 = 0; |
wonin | 0:3cef86fc978f | 87 | |
wonin | 0:3cef86fc978f | 88 | } |
wonin | 0:3cef86fc978f | 89 | |
wonin | 0:3cef86fc978f | 90 | void measureBH1750_0(){ |
wonin | 1:aa9eabdefc12 | 91 | //BH1750_EN0 = 1; |
wonin | 1:aa9eabdefc12 | 92 | //BH1750_EN1 = 1; |
wonin | 1:aa9eabdefc12 | 93 | // //wait(0.005); |
wonin | 1:aa9eabdefc12 | 94 | |
wonin | 1:aa9eabdefc12 | 95 | lum0 = lum.lux(); |
wonin | 1:aa9eabdefc12 | 96 | |
wonin | 0:3cef86fc978f | 97 | } |
wonin | 0:3cef86fc978f | 98 | |
wonin | 1:aa9eabdefc12 | 99 | void printthis(){ |
wonin | 1:aa9eabdefc12 | 100 | if(tickerCntr1 % 6000 == 0){ |
wonin | 1:aa9eabdefc12 | 101 | lum0 = lum.lux(); |
wonin | 1:aa9eabdefc12 | 102 | //pc.printf("Illuminance 1: %+7.2f [Lux] DutyCycle: %+7.2f [decima'] \r\n", lum0, dutyCycle); |
wonin | 1:aa9eabdefc12 | 103 | |
wonin | 1:aa9eabdefc12 | 104 | } |
wonin | 1:aa9eabdefc12 | 105 | else if(tickerCntr1 % 6000 == 3000){ |
wonin | 1:aa9eabdefc12 | 106 | // ori.read(ori_rd); |
wonin | 1:aa9eabdefc12 | 107 | |
wonin | 1:aa9eabdefc12 | 108 | } |
wonin | 1:aa9eabdefc12 | 109 | //else if(tickerCntr1 % 10000 == 0) |
wonin | 1:aa9eabdefc12 | 110 | //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]); |
wonin | 1:aa9eabdefc12 | 111 | } |
wonin | 1:aa9eabdefc12 | 112 | |
wonin | 1:aa9eabdefc12 | 113 | |
wonin | 1:aa9eabdefc12 | 114 | |
wonin | 1:aa9eabdefc12 | 115 | |
wonin | 0:3cef86fc978f | 116 | float measureBH1750_1(){ |
wonin | 0:3cef86fc978f | 117 | BH1750_EN0 = 1; |
wonin | 1:aa9eabdefc12 | 118 | //BH1750_EN1 = 0; |
wonin | 0:3cef86fc978f | 119 | // wait(0.005); |
wonin | 0:3cef86fc978f | 120 | return lum.lux(); |
wonin | 0:3cef86fc978f | 121 | } |
wonin | 0:3cef86fc978f | 122 | |
wonin | 0:3cef86fc978f | 123 | |
wonin | 0:3cef86fc978f | 124 | int main() {; |
wonin | 0:3cef86fc978f | 125 | int reading_num = 0; |
wonin | 0:3cef86fc978f | 126 | |
wonin | 1:aa9eabdefc12 | 127 | |
wonin | 1:aa9eabdefc12 | 128 | //flipLed.attach_us(&flip, 50.0f); |
wonin | 1:aa9eabdefc12 | 129 | //t1.attach(&printthis, 1.0f); |
wonin | 1:aa9eabdefc12 | 130 | //pc.printf("Hi1 \r\n"); |
wonin | 1:aa9eabdefc12 | 131 | |
wonin | 1:aa9eabdefc12 | 132 | while(true){ |
wonin | 0:3cef86fc978f | 133 | |
wonin | 0:3cef86fc978f | 134 | // lum1 = measureBH1750_1(); |
wonin | 0:3cef86fc978f | 135 | // lum0 = measureBH1750_0(); |
wonin | 0:3cef86fc978f | 136 | // pc.printf("Illuminance 1: %+7.2f [Lux] : Illuminance 2: %+7.2f [Lux] : %i\r\n", lum0, lum1, reading_num++); |
wonin | 1:aa9eabdefc12 | 137 | |
wonin | 1:aa9eabdefc12 | 138 | flip(); |
wonin | 1:aa9eabdefc12 | 139 | printthis(); |
wonin | 1:aa9eabdefc12 | 140 | wait_us(50.0f); |
wonin | 1:aa9eabdefc12 | 141 | |
wonin | 0:3cef86fc978f | 142 | |
wonin | 1:aa9eabdefc12 | 143 | |
wonin | 0:3cef86fc978f | 144 | } |
wonin | 0:3cef86fc978f | 145 | |
wonin | 0:3cef86fc978f | 146 | } |