本帖最后由 lustao 于 2019-5-31 15:43 编辑
Microbit的5X5 LED屏同时也是一块光线传感器,不仅可以感光还可以根据
对应的光线值进行相应控制。在室内测了下光,值为
- 5
- 5
- 11
- 17
- 0
- 10
- 9
- 8
- 9
- 8
- 8
- 11
- 0
- 0
- 0
- 0
- 0
- 5
- 1
- 3
- 220
- 49
- 51
- 217 手机灯10cm
- 47
- 47 手机灯20cm
- 0
- 2
- 6
- 2
- 3
- 4
- 4
- 4
- 4
- 4
- 0
- 0
- 0
- 0
- 4
- 5
- 5
- 7
- 4 日光灯下
复制代码
决定光线值:
〈=4时车不动左右灯亮,下彩灯亮白
〈=46时车不动左右灯不亮,下彩灯亮变彩
〉46时车向前开速度30 各灯不亮
A键取消光控,B键光控
基于MIND+平台,用的上传模式。
产生的自动生成码
- #include
- #include
- #include
- #include
- double df_light;
- double df_state;
- double my_variable;
- double df_red;
- double df_green;
- double df_blue;
- DFRobot_NeoPixel rgb_display_15;
- Maqueen_Motor motor;
- void _5149_63A7();
- void df_statecar();
- void _663E_793A();
- void _5149_63A7()
- {
- Serial.println("this is guangkong");
- df_statecar();
- if ((df_light <= 0)) {
- Serial.println((MMatrix.readLightLevel()));
- Serial.println(millis());
- MMatrix.clear();
- if (((MMatrix.readLightLevel()) <= 3)) {
- if ((df_state == 2)) {
- rgb_display_15.setRangeColor(-1, -1, 0xffffff);
- delay(500);
- delay(500);
- }
- else {
- df_state = 2;
- Serial.println("state0");
- digitalWrite(8,HIGH);
- digitalWrite(12,HIGH);
- rgb_display_15.setRangeColor(-1, -1, 0xffffff);
- delay(500);
- }
- }
- else {
- if (((MMatrix.readLightLevel()) <= 46)) {
- if ((df_state == 1)) {
- delay(150);
- }
- else {
- df_state = 1;
- Serial.println("state1");
- digitalWrite(12,LOW);
- digitalWrite(8,LOW);
- delay(1000);
- }
- }
- else {
- if ((df_state == 8)) {
- delay(500);
- }
- else {
- df_state = 8;
- Serial.println("state8");
- digitalWrite(12,LOW);
- digitalWrite(8,LOW);
- delay(2000);
- df_statecar();
- }
- }
- }
- }
- else {
- df_state = 0;
- Serial.println("kong off");
- digitalWrite(12,LOW);
- digitalWrite(8,LOW);
- rgb_display_15.clear();
- delay(500);
- }
- }
- void df_statecar()
- {
- if ((df_state >= 64)) {
- motor.motorRun(motor.LEFT,motor.CW,my_variable);
- motor.motorRun(motor.RIGHT,motor.CW,0);
- }
- else {
- if ((df_state >= 32)) {
- motor.motorRun(motor.LEFT,motor.CW,0);
- motor.motorRun(motor.RIGHT,motor.CW,my_variable);
- }
- else {
- if ((df_state >= 16)) {
- motor.motorRun(motor.LEFT,motor.CCW,my_variable);
- motor.motorRun(motor.RIGHT,motor.CCW,my_variable);
- }
- else {
- if ((df_state >= 8)) {
- motor.motorRun(motor.LEFT,motor.CW,my_variable);
- motor.motorRun(motor.RIGHT,motor.CW,my_variable);
- }
- else {
- motor.motorStop(motor.LEFT);
- motor.motorStop(motor.RIGHT);
- }
- }
- }
- }
- }
- void _663E_793A()
- {
- if (((((int)df_state) % ((int)2)) == 1)) {
- rgb_display_15.setRangeColor(-1, -1, rgb_display_15.rgbToColor(round(df_red), round(df_green), round(df_blue)));
- delay(50);
- }
- else {
- rgb_display_15.clear();
- }
- }
- void buttonACallback()
- {
- df_light = 1;
- MMatrix.print("light off");
- MMatrix.print(millis());
- delay(2000);
- Serial.println("A on");
- MMatrix.clear();
- MSound.play(0, BIRTHDAY, Once);
- }
- void buttonBCallback()
- {
- df_light = 0;
- MMatrix.print("light on");
- MMatrix.print(millis());
- delay(2000);
- Serial.println("B on");
- MMatrix.clear();
- MSound.play(0, BIRTHDAY, Once);
- }
- void setup() {
- Serial.begin(9600);
- rgb_display_15.begin(15, 4, 255);
- onEvent(ID_BUTTON_A, PRESS, buttonACallback);
- my_variable = 30;
- df_state = 0;
- df_light = 0;
- rgb_display_15.clear();
- onEvent(ID_BUTTON_B, PRESS, buttonBCallback);
- }
- void loop() {
- if (((((int)df_state) % ((int)2)) == 1)) {
- while (1) {
- df_red = 0;
- df_green = 0;
- df_blue = 255;
- for (int count = 0; count < 255; count++) {
- df_red = df_red + 1;
- df_blue = df_blue - 1;
- _663E_793A();
- _5149_63A7();
- }
- for (int count = 0; count < 255; count++) {
- df_green = df_green + 1;
- df_red = df_red - 1;
- _663E_793A();
- _5149_63A7();
- }
- for (int count = 0; count < 255; count++) {
- df_blue = df_blue + 1;
- df_green = df_green - 1;
- _663E_793A();
- _5149_63A7();
- }
- }
- }
- else {
- _5149_63A7();
- }
- }
复制代码
还可靠。
0
|
|
|
|