├── .gitignore ├── Blinker_Arduino_project ├── Blinker_AQI_Detector │ ├── AQI_Detector_BLE │ │ └── AQI_Detector_BLE.ino │ ├── AQI_Detector_WiFi │ │ └── AQI_Detector_WiFi.ino │ └── README.md ├── Blinker_LED_WS2812 │ ├── README.md │ ├── WS2812_BLE │ │ └── WS2812_BLE.ino │ └── WS2812_WiFi │ │ └── WS2812_WiFi.ino └── Blinker_Wireless_CAR │ ├── CAR_BLE │ └── CAR_BLE.ino │ └── CAR_WiFi │ └── CAR_WiFi.ino ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | __vm/ 2 | Debug/ 3 | *.vcxproj 4 | *.filters 5 | .vs/ 6 | *.sln 7 | .vscode/ 8 | -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_AQI_Detector/AQI_Detector_BLE/AQI_Detector_BLE.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Blinker AQI Detector 3 | */ 4 | 5 | #define BLINKER_PRINT Serial 6 | #define BLINKER_BLE 7 | 8 | #include 9 | 10 | // U8g2 lib, https://github.com/olikraus/U8g2_Arduino 11 | #include 12 | 13 | #ifdef U8X8_HAVE_HW_I2C 14 | #include 15 | #endif 16 | 17 | U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /*reset Pin*/ 13); 18 | 19 | // PMS5003ST, https://github.com/i3water/Blinker_PMSX003ST 20 | #include 21 | 22 | #if defined(ESP32) 23 | HardwareSerial pmsSerial(2);// Pins 16(RX),17(TX) 24 | #else 25 | #include 26 | SoftwareSerial pmsSerial(4, 5); 27 | #endif 28 | 29 | BLINKER_PMSX003ST pms; 30 | 31 | uint32_t os_time_ms; 32 | 33 | #define PM_1 "PM1.0" 34 | #define PM_2_5 "PM2.5" 35 | #define PM_10 "PM10" 36 | #define PMS_FA "FA" 37 | #define PMS_RH "RH" 38 | #define PMS_TP "TP" 39 | 40 | BlinkerNumber PM1(PM_1); 41 | BlinkerNumber PM25(PM_2_5); 42 | BlinkerNumber PM10(PM_10); 43 | BlinkerNumber HCHO(PMS_FA); 44 | BlinkerNumber HUMI(PMS_RH); 45 | BlinkerNumber TEMP(PMS_TP); 46 | 47 | void drawAQI() 48 | { 49 | u8g2.setFont(u8g2_font_helvR10_te); 50 | u8g2.setCursor(0, 13); 51 | u8g2.print("PM1.0:" + String(pms.getPmAto(1.0))); 52 | u8g2.setCursor(0, 27); 53 | u8g2.print("PM2.5:" + String(pms.getPmAto(2.5))); 54 | u8g2.setCursor(0, 41); 55 | u8g2.print("PM10:" + String(pms.getPmAto(10))); 56 | 57 | u8g2.setCursor(75, 13); 58 | u8g2.print("FA:" + String(pms.getForm())); 59 | u8g2.setCursor(75, 27); 60 | u8g2.print("RH:" + String((int)pms.getHumi()) + "%"); 61 | u8g2.setCursor(75, 41); 62 | u8g2.print("TP:" + String((int)pms.getTemp()) + "°"); 63 | 64 | u8g2.drawLine(0, 46, 128, 46); 65 | 66 | u8g2.setFont(u8g2_font_helvR10_te); 67 | u8g2.setCursor(0, 63); 68 | u8g2.print("Blinker AQI detector"); 69 | } 70 | 71 | void getPMS() 72 | { 73 | if (millis() - os_time_ms > 1000UL) { 74 | #ifndef ESP32 75 | if (!pmsSerial.isListening()) { 76 | pmsSerial.listen(); 77 | delay(100); 78 | } 79 | #endif 80 | 81 | pms.request(); 82 | if(!pms.read()){ 83 | return; 84 | } 85 | 86 | os_time_ms = millis(); 87 | } 88 | } 89 | 90 | void detectorDisplay() 91 | { 92 | getPMS(); 93 | 94 | u8g2.firstPage(); 95 | do { 96 | drawAQI(); 97 | } while ( u8g2.nextPage() ); 98 | } 99 | 100 | void u8g2Init() 101 | { 102 | u8g2.begin(); 103 | u8g2.setFlipMode(0); 104 | u8g2.clearBuffer(); 105 | u8g2.enableUTF8Print(); 106 | } 107 | 108 | void pmsInit() 109 | { 110 | pmsSerial.begin(9600); 111 | pms.begin(pmsSerial); 112 | pms.setMode(PASSIVE); 113 | } 114 | 115 | void setup() 116 | { 117 | Serial.begin(115200); 118 | 119 | u8g2Init(); 120 | pmsInit(); 121 | 122 | Blinker.begin(); 123 | } 124 | 125 | void loop() 126 | { 127 | Blinker.run(); 128 | 129 | if (Blinker.available()) { 130 | BLINKER_LOG2("Blinker.readString(): ", Blinker.readString()); 131 | 132 | Blinker.vibrate(); 133 | 134 | uint32_t BlinkerTime = millis(); 135 | Blinker.print(BlinkerTime); 136 | Blinker.print("millis", BlinkerTime); 137 | } 138 | 139 | detectorDisplay(); 140 | 141 | PM1.unit("ug/m3"); 142 | PM1.print(pms.getPmAto(1.0)); 143 | PM25.unit("ug/m3"); 144 | PM25.print(pms.getPmAto(2.5)); 145 | PM10.unit("ug/m3"); 146 | PM10.print(pms.getPmAto(10)); 147 | HCHO.unit("ug/m3"); 148 | HCHO.print(pms.getForm()); 149 | HUMI.unit("%"); 150 | HUMI.print(pms.getHumi()); 151 | TEMP.unit("°C"); 152 | TEMP.print(pms.getTemp()); 153 | 154 | Blinker.delay(1000); 155 | } -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_AQI_Detector/AQI_Detector_WiFi/AQI_Detector_WiFi.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Blinker AQI Detector 3 | */ 4 | 5 | #define BLINKER_PRINT Serial 6 | #define BLINKER_WIFI 7 | 8 | #include 9 | 10 | char ssid[] = ""; 11 | char pswd[] = ""; 12 | 13 | // U8g2lib, https://github.com/olikraus/U8g2_Arduino 14 | #include 15 | 16 | #ifdef U8X8_HAVE_HW_I2C 17 | #include 18 | #endif 19 | 20 | U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /*reset Pin*/ 13); 21 | 22 | // BLINKER_PMSX003ST, https://github.com/i3water/Blinker_PMSX003ST 23 | #include 24 | 25 | #if defined(ESP32) 26 | HardwareSerial pmsSerial(2);// Pins 16(RX),17(TX) 27 | #else 28 | #include 29 | SoftwareSerial pmsSerial(14, 15); 30 | #endif 31 | 32 | BLINKER_PMSX003ST pms; 33 | 34 | uint32_t os_time_ms; 35 | 36 | #define PM_1 "PM1.0" 37 | #define PM_2_5 "PM2.5" 38 | #define PM_10 "PM10" 39 | #define PMS_FA "FA" 40 | #define PMS_RH "RH" 41 | #define PMS_TP "TP" 42 | 43 | BlinkerNumber PM1(PM_1); 44 | BlinkerNumber PM25(PM_2_5); 45 | BlinkerNumber PM10(PM_10); 46 | BlinkerNumber HCHO(PMS_FA); 47 | BlinkerNumber HUMI(PMS_RH); 48 | BlinkerNumber TEMP(PMS_TP); 49 | 50 | void drawAQI() 51 | { 52 | u8g2.setFont(u8g2_font_helvR10_te); 53 | u8g2.setCursor(0, 13); 54 | u8g2.print("PM1.0:" + String(pms.getPmAto(1.0))); 55 | u8g2.setCursor(0, 27); 56 | u8g2.print("PM2.5:" + String(pms.getPmAto(2.5))); 57 | u8g2.setCursor(0, 41); 58 | u8g2.print("PM10:" + String(pms.getPmAto(10))); 59 | 60 | u8g2.setCursor(75, 13); 61 | u8g2.print("FA:" + String(pms.getForm())); 62 | u8g2.setCursor(75, 27); 63 | u8g2.print("RH:" + String((int)pms.getHumi()) + "%"); 64 | u8g2.setCursor(75, 41); 65 | u8g2.print("TP:" + String((int)pms.getTemp()) + "°"); 66 | 67 | u8g2.drawLine(0, 46, 128, 46); 68 | 69 | u8g2.setFont(u8g2_font_helvR10_te); 70 | u8g2.setCursor(0, 63); 71 | u8g2.print("Blinker AQI detector"); 72 | } 73 | 74 | void getPMS() 75 | { 76 | if (millis() - os_time_ms > 1000UL) { 77 | #ifndef ESP32 78 | if (!pmsSerial.isListening()) { 79 | pmsSerial.listen(); 80 | delay(100); 81 | } 82 | #endif 83 | 84 | pms.request(); 85 | if(!pms.read()){ 86 | return; 87 | } 88 | 89 | os_time_ms = millis(); 90 | } 91 | } 92 | 93 | void detectorDisplay() 94 | { 95 | getPMS(); 96 | 97 | u8g2.firstPage(); 98 | do { 99 | drawAQI(); 100 | } while ( u8g2.nextPage() ); 101 | } 102 | 103 | void u8g2Init() 104 | { 105 | u8g2.begin(); 106 | u8g2.setFlipMode(0); 107 | u8g2.clearBuffer(); 108 | u8g2.enableUTF8Print(); 109 | } 110 | 111 | void pmsInit() 112 | { 113 | pmsSerial.begin(9600); 114 | pms.begin(pmsSerial); 115 | pms.setMode(PASSIVE); 116 | } 117 | 118 | void setup() 119 | { 120 | Serial.begin(115200); 121 | 122 | u8g2Init(); 123 | pmsInit(); 124 | 125 | Blinker.begin(ssid, pswd); 126 | } 127 | 128 | void loop() 129 | { 130 | Blinker.run(); 131 | 132 | if (Blinker.available()) { 133 | BLINKER_LOG2("Blinker.readString(): ", Blinker.readString()); 134 | 135 | Blinker.vibrate(); 136 | 137 | uint32_t BlinkerTime = millis(); 138 | Blinker.print(BlinkerTime); 139 | Blinker.print("millis", BlinkerTime); 140 | } 141 | 142 | detectorDisplay(); 143 | 144 | PM1.unit("ug/m3"); 145 | PM1.print(pms.getPmAto(1.0)); 146 | PM25.unit("ug/m3"); 147 | PM25.print(pms.getPmAto(2.5)); 148 | PM10.unit("ug/m3"); 149 | PM10.print(pms.getPmAto(10)); 150 | HCHO.unit("ug/m3"); 151 | HCHO.print(pms.getForm()); 152 | HUMI.unit("%"); 153 | HUMI.print(pms.getHumi()); 154 | TEMP.unit("°C"); 155 | TEMP.print(pms.getTemp()); 156 | 157 | Blinker.delay(1000); 158 | } -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_AQI_Detector/README.md: -------------------------------------------------------------------------------- 1 | # Blinker_AQI_Detector 2 | Blinker AQI Detector example, detect PM1.0 PM2.5 PM10 HCHO Humidity Temperature. Support Arduino, ESP8266, ESP32. 3 | 4 | # Prerequisites 5 | You should have the following ready before beginning with any board: 6 | * Arduino boards, ESP8266, ESP32 7 | * Plantower PMSX003ST sensor 8 | * 12864(controller SSD1306) monochrome OLED 9 | * Install the [blinker-library](https://github.com/i3water/blinker-library) library via the Arduino Library Manager 10 | * Install the [Blinker_PMSX003ST](https://github.com/i3water/Blinker_PMSX003ST) library via the Arduino Library Manager 11 | * Install the [U8g2](https://github.com/olikraus/U8g2_Arduino) library via the Arduino Library Manager 12 | 13 | # 准备工作 14 | 开始使用前你需要做好如下准备: 15 | * Arduino boards, ESP8266, ESP32 16 | * 攀藤 PMSX003ST 传感器 17 | * 12864(SSD1306) 单色 OLED 18 | * 使用 Arduino IDE 的库管理器安装 [blinker-library](https://github.com/i3water/blinker-library) 19 | * 使用 Arduino IDE 的库管理器安装 [Blinker_PMSX003ST](https://github.com/i3water/Blinker_PMSX003ST) 20 | * 使用 Arduino IDE 的库管理器安装 [U8g2](https://github.com/olikraus/U8g2_Arduino) 21 | -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_LED_WS2812/README.md: -------------------------------------------------------------------------------- 1 | # Blinker_LED_WS2812 2 | Blinker WS2812 example. Support Arduino, ESP8266, ESP32. 3 | 4 | # Prerequisites 5 | You should have the following ready before beginning with any board: 6 | * Arduino boards, ESP8266, ESP32 7 | * WS2812 8 | * Install the [blinker-library](https://github.com/i3water/blinker-library) library via the Arduino Library Manager 9 | * Install the [Adafruit_NeoPixel](https://github.com/adafruit/Adafruit_NeoPixel) library via the Arduino Library Manager 10 | 11 | # 准备工作 12 | 开始使用前你需要做好如下准备: 13 | * Arduino boards, ESP8266, ESP32 14 | * WS2812 15 | * 使用 Arduino IDE 的库管理器安装 [blinker-library](https://github.com/i3water/blinker-library) 16 | * 使用 Arduino IDE 的库管理器安装 [Adafruit_NeoPixel](https://github.com/adafruit/Adafruit_NeoPixel) 17 | -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_LED_WS2812/WS2812_BLE/WS2812_BLE.ino: -------------------------------------------------------------------------------- 1 | #define BLINKER_PRINT Serial 2 | #define BLINKER_BLE 3 | 4 | #include 5 | 6 | #include 7 | #ifdef __AVR__ 8 | #include 9 | #endif 10 | 11 | #define PIN 8 12 | #define NUMPIXELS 9 13 | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); 14 | 15 | #define RGB_1 "RGBKey" 16 | 17 | BlinkerRGB RGB1(RGB_1); 18 | 19 | void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value) 20 | { 21 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); 22 | BLINKER_LOG2("R value: ", r_value); 23 | BLINKER_LOG2("G value: ", g_value); 24 | BLINKER_LOG2("B value: ", b_value); 25 | BLINKER_LOG2("Rrightness value: ", bright_value); 26 | 27 | uint8_t colorR = map(r_value, 0, 255, 0, bright_value); 28 | uint8_t colorG = map(g_value, 0, 255, 0, bright_value); 29 | uint8_t colorB = map(b_value, 0, 255, 0, bright_value); 30 | 31 | for(int i = 0; i < NUMPIXELS; i++){ 32 | pixels.setPixelColor(i, pixels.Color(colorR,colorG,colorB)); 33 | pixels.show(); 34 | } 35 | } 36 | 37 | void setup() 38 | { 39 | Serial.begin(115200); 40 | 41 | pinMode(LED_BUILTIN, OUTPUT); 42 | digitalWrite(LED_BUILTIN, LOW); 43 | 44 | Blinker.begin(); 45 | 46 | pixels.begin(); 47 | 48 | RGB1.attach(rgb1_callback); 49 | } 50 | 51 | void loop() 52 | { 53 | Blinker.run(); 54 | 55 | if (Blinker.available()) { 56 | BLINKER_LOG2("Blinker.readString(): ", Blinker.readString()); 57 | 58 | Blinker.vibrate(); 59 | 60 | uint32_t BlinkerTime = millis(); 61 | Blinker.print(BlinkerTime); 62 | Blinker.print("millis", BlinkerTime); 63 | } 64 | } -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_LED_WS2812/WS2812_WiFi/WS2812_WiFi.ino: -------------------------------------------------------------------------------- 1 | #define BLINKER_PRINT Serial 2 | #define BLINKER_WIFI 3 | 4 | #include 5 | 6 | char ssid[] = "Your WiFi network SSID or name"; 7 | char pswd[] = "Your WiFi network WPA password or WEP key"; 8 | 9 | #include 10 | #ifdef __AVR__ 11 | #include 12 | #endif 13 | 14 | #define PIN 0 15 | #define NUMPIXELS 9 16 | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); 17 | 18 | #define RGB_1 "RGBKey" 19 | 20 | BlinkerRGB RGB1(RGB_1); 21 | 22 | void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value) 23 | { 24 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); 25 | BLINKER_LOG2("R value: ", r_value); 26 | BLINKER_LOG2("G value: ", g_value); 27 | BLINKER_LOG2("B value: ", b_value); 28 | BLINKER_LOG2("Rrightness value: ", bright_value); 29 | 30 | uint8_t colorR = map(r_value, 0, 255, 0, bright_value); 31 | uint8_t colorG = map(g_value, 0, 255, 0, bright_value); 32 | uint8_t colorB = map(b_value, 0, 255, 0, bright_value); 33 | 34 | for(int i = 0; i < NUMPIXELS; i++){ 35 | pixels.setPixelColor(i, pixels.Color(colorR,colorG,colorB)); 36 | pixels.show(); 37 | } 38 | } 39 | 40 | 41 | void setup() 42 | { 43 | Serial.begin(115200); 44 | 45 | pinMode(LED_BUILTIN, OUTPUT); 46 | digitalWrite(LED_BUILTIN, LOW); 47 | 48 | Blinker.begin(ssid, pswd); 49 | 50 | pixels.begin(); 51 | 52 | RGB1.attach(rgb1_callback); 53 | } 54 | 55 | void loop() 56 | { 57 | Blinker.run(); 58 | 59 | if (Blinker.available()) { 60 | BLINKER_LOG2("Blinker.readString(): ", Blinker.readString()); 61 | 62 | Blinker.vibrate(); 63 | 64 | uint32_t BlinkerTime = millis(); 65 | Blinker.print(BlinkerTime); 66 | Blinker.print("millis", BlinkerTime); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_Wireless_CAR/CAR_BLE/CAR_BLE.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Blinker BLE CAR, support Arduino UNO 3 | */ 4 | 5 | #define BLINKER_PRINT Serial 6 | #define BLINKER_BLE 7 | 8 | #include 9 | 10 | #define L_DIR_PIN 7 11 | #define L_PWM_PIN 6 12 | #define R_DIR_PIN 4 13 | #define R_PWM_PIN 5 14 | #define C_BAR_PIN 9 15 | 16 | uint32_t car_os_time = millis(); 17 | bool isWarn = false; 18 | 19 | #define JOY_1 "JOYKey" 20 | 21 | BlinkerJoystick JOY1(JOY_1); 22 | 23 | void joystick1_callback(uint8_t xAxis, uint8_t yAxis) 24 | { 25 | BLINKER_LOG2("Joystick1 X axis: ", xAxis); 26 | BLINKER_LOG2("Joystick1 Y axis: ", yAxis); 27 | 28 | uint8_t L_PWM = 0; 29 | uint8_t R_PWM = 0; 30 | bool L_DIR = false; 31 | bool R_DIR = false; 32 | 33 | blinker_car_parse(xAxis, yAxis, L_PWM, L_DIR, R_PWM, R_DIR); 34 | 35 | blinker_car_control(L_PWM, L_DIR, R_PWM, R_DIR); 36 | } 37 | 38 | void blinker_car_init() 39 | { 40 | pinMode(L_DIR_PIN, OUTPUT); 41 | pinMode(L_PWM_PIN, OUTPUT); 42 | pinMode(R_DIR_PIN, OUTPUT); 43 | pinMode(R_PWM_PIN, OUTPUT); 44 | 45 | pinMode(C_BAR_PIN, INPUT_PULLUP); 46 | } 47 | 48 | void blinker_car_parse(uint8_t xAxis, uint8_t yAxis, uint8_t &_L_PWM, bool &_L_DIR, uint8_t &_R_PWM, bool &_R_DIR) 49 | { 50 | bool isStop = false; 51 | 52 | if (yAxis < 128) { 53 | _L_DIR = true; 54 | _R_DIR = true; 55 | 56 | _L_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 57 | _R_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 58 | } 59 | else if (yAxis > 128) { 60 | _L_DIR = false; 61 | _R_DIR = false; 62 | 63 | _L_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 64 | _R_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 65 | } 66 | else { 67 | isStop = true; 68 | 69 | _L_PWM = 0; 70 | _R_PWM = 0; 71 | } 72 | 73 | if (xAxis < 128) { 74 | if (isStop) { 75 | _L_DIR = false; 76 | _R_DIR = true; 77 | 78 | _L_PWM = map(abs(128-xAxis), 0, 128, 128, 255); 79 | _R_PWM = map(abs(128-xAxis), 0, 128, 128, 255); 80 | } 81 | else { 82 | _L_PWM -= map(abs(128-xAxis), 0, 128, 0, 128); 83 | } 84 | } 85 | else if (xAxis > 128) { 86 | if (isStop) { 87 | _L_DIR = true; 88 | _R_DIR = false; 89 | } 90 | else { 91 | _R_PWM -= map(abs(128-xAxis), 0, 128, 0, 128); 92 | } 93 | } 94 | else { 95 | if (isStop) { 96 | _L_PWM = 0; 97 | _R_PWM = 0; 98 | } 99 | } 100 | } 101 | 102 | void blinker_car_control(uint8_t cl_pwm, bool cl_dir, uint8_t cr_pwm, bool cr_dir) 103 | { 104 | digitalWrite(L_DIR_PIN, cl_dir); 105 | digitalWrite(R_DIR_PIN, cr_dir); 106 | analogWrite(L_PWM_PIN, cl_pwm); 107 | analogWrite(R_PWM_PIN, cr_pwm); 108 | } 109 | 110 | void blinker_car_detect() 111 | { 112 | if (!digitalRead(C_BAR_PIN)) { 113 | if (!isWarn) { 114 | Blinker.vibrate(); 115 | isWarn = true; 116 | car_os_time = millis(); 117 | } 118 | else if (millis() - car_os_time >= 5000) { 119 | isWarn = false; 120 | } 121 | } 122 | else { 123 | isWarn = false; 124 | } 125 | } 126 | 127 | void setup() 128 | { 129 | Serial.begin(115200); 130 | 131 | blinker_car_init(); 132 | 133 | Blinker.begin(); 134 | 135 | JOY1.attach(joystick1_callback); 136 | } 137 | 138 | void loop() 139 | { 140 | Blinker.run(); 141 | 142 | blinker_car_detect(); 143 | 144 | #if defined(BLINKER_CAR_DEBUG) 145 | BLINKER_LOG4("L_PWM: ", L_PWM, " | L_DIR: ", L_DIR); 146 | BLINKER_LOG4("R_PWM: ", R_PWM, " | R_DIR: ", R_DIR); 147 | Blinker.delay(2000); 148 | #endif 149 | } 150 | -------------------------------------------------------------------------------- /Blinker_Arduino_project/Blinker_Wireless_CAR/CAR_WiFi/CAR_WiFi.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Blinker WiFi CAR, support WiFiduino 3 | */ 4 | 5 | #define BLINKER_PRINT Serial 6 | #define BLINKER_WIFI 7 | 8 | #include 9 | 10 | char ssid[] = ""; 11 | char pswd[] = ""; 12 | 13 | #define L_DIR_PIN 14 14 | #define L_PWM_PIN 16 15 | #define R_DIR_PIN 4 16 | #define R_PWM_PIN 5 17 | #define C_BAR_PIN 12 18 | 19 | uint32_t car_os_time = millis(); 20 | bool isWarn = false; 21 | 22 | #define JOY_1 "JOYKey" 23 | 24 | BlinkerJoystick JOY1(JOY_1); 25 | 26 | void joystick1_callback(uint8_t xAxis, uint8_t yAxis) 27 | { 28 | BLINKER_LOG2("Joystick1 X axis: ", xAxis); 29 | BLINKER_LOG2("Joystick1 Y axis: ", yAxis); 30 | 31 | uint8_t L_PWM = 0; 32 | uint8_t R_PWM = 0; 33 | bool L_DIR = false; 34 | bool R_DIR = false; 35 | 36 | blinker_car_parse(xAxis, yAxis, L_PWM, L_DIR, R_PWM, R_DIR); 37 | 38 | blinker_car_control(L_PWM, L_DIR, R_PWM, R_DIR); 39 | } 40 | 41 | void blinker_car_init() 42 | { 43 | pinMode(L_DIR_PIN, OUTPUT); 44 | pinMode(L_PWM_PIN, OUTPUT); 45 | pinMode(R_DIR_PIN, OUTPUT); 46 | pinMode(R_PWM_PIN, OUTPUT); 47 | 48 | pinMode(C_BAR_PIN, INPUT_PULLUP); 49 | 50 | // #if defined(ESP8266) 51 | analogWriteRange(255); 52 | // #endif 53 | } 54 | 55 | void blinker_car_parse(uint8_t xAxis, uint8_t yAxis, uint8_t &_L_PWM, bool &_L_DIR, uint8_t &_R_PWM, bool &_R_DIR) 56 | { 57 | bool isStop = false; 58 | 59 | if (yAxis < 128) { 60 | _L_DIR = true; 61 | _R_DIR = true; 62 | 63 | _L_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 64 | _R_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 65 | } 66 | else if (yAxis > 128) { 67 | _L_DIR = false; 68 | _R_DIR = false; 69 | 70 | _L_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 71 | _R_PWM = map(abs(128-yAxis), 0, 128, 128, 255); 72 | } 73 | else { 74 | isStop = true; 75 | 76 | _L_PWM = 0; 77 | _R_PWM = 0; 78 | } 79 | 80 | if (xAxis < 128) { 81 | if (isStop) { 82 | _L_DIR = false; 83 | _R_DIR = true; 84 | 85 | _L_PWM = map(abs(128-xAxis), 0, 128, 128, 255); 86 | _R_PWM = map(abs(128-xAxis), 0, 128, 128, 255); 87 | } 88 | else { 89 | _L_PWM -= map(abs(128-xAxis), 0, 128, 0, 128); 90 | } 91 | } 92 | else if (xAxis > 128) { 93 | if (isStop) { 94 | _L_DIR = true; 95 | _R_DIR = false; 96 | } 97 | else { 98 | _R_PWM -= map(abs(128-xAxis), 0, 128, 0, 128); 99 | } 100 | } 101 | else { 102 | if (isStop) { 103 | _L_PWM = 0; 104 | _R_PWM = 0; 105 | } 106 | } 107 | } 108 | 109 | void blinker_car_control(uint8_t cl_pwm, bool cl_dir, uint8_t cr_pwm, bool cr_dir) 110 | { 111 | digitalWrite(L_DIR_PIN, cl_dir); 112 | digitalWrite(R_DIR_PIN, cr_dir); 113 | analogWrite(L_PWM_PIN, cl_pwm); 114 | analogWrite(R_PWM_PIN, cr_pwm); 115 | } 116 | 117 | void blinker_car_detect() 118 | { 119 | if (!digitalRead(C_BAR_PIN)) { 120 | if (!isWarn) { 121 | Blinker.vibrate(); 122 | isWarn = true; 123 | car_os_time = millis(); 124 | } 125 | else if (millis() - car_os_time >= 5000) { 126 | isWarn = false; 127 | } 128 | } 129 | else { 130 | isWarn = false; 131 | } 132 | } 133 | 134 | void setup() 135 | { 136 | Serial.begin(115200); 137 | 138 | blinker_car_init(); 139 | 140 | Blinker.begin(ssid, pswd); 141 | 142 | JOY1.attach(joystick1_callback); 143 | } 144 | 145 | void loop() 146 | { 147 | Blinker.run(); 148 | 149 | blinker_car_detect(); 150 | 151 | #if defined(BLINKER_CAR_DEBUG) 152 | BLINKER_LOG4("L_PWM: ", L_PWM, " | L_DIR: ", L_DIR); 153 | BLINKER_LOG4("R_PWM: ", R_PWM, " | R_DIR: ", R_DIR); 154 | Blinker.delay(2000); 155 | #endif 156 | } 157 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 blinker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # blinker-project-example 2 | Project example based on Blinker 3 | 4 | # blinker 项目示例 5 | 基于Blinker的项目示例 --------------------------------------------------------------------------------