Blynk Controlled Car using Accelerometer
I used to create a radio controlled control in Blynk’s ble communication.
https://homemadegarbage.com/en/blynk_car01-2
There is an update to Blynk (v0.4.4) and BLE communication is formally introduced rather than the beta version, and further, since the acceleration sensor information of the smartphone has been able to obtain, I tried to modify the radio controlled to tilt the smartphone.
I do not play hard at all (^o^).
目次
Blynk Library for Arduino
Latest library is located in the following: https://github.com/blynkkk/blynk-library
Adafruit Feather 32u4 Bluefruit LE is used. So, Arduino code is generated in reference to the following.
[Example] -> [Blynk] -> [Boards_Bluetooth]-> [Adafruit_Feather_32u4_BLE]
Blynk settings
Create a new project. Select the Arduino UNO in HARDWARE MODEL, because there is no Adafruit Feather 32u4 BLE yet. CONNECTION TYPE is BLE. AUTH TOKEN will make a note so you use it at the time of Arduino code generation (or to send mail).
Place the BLE widget and the Accelerometer widget.
Accelerometer setting. The values are output in virtual pin V0.
Arduino IDE
[Example] -> [Blynk] -> [Boards_Bluetooth]-> [Adafruit_Feather_32u4_BLE] code generated by reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
//#define BLYNK_DEBUG #define BLYNK_PRINT Serial #define BLYNK_USE_DIRECT_CONNECT #include <BlynkSimpleSerialBLE.h> #include <SPI.h> #include <Adafruit_BLE.h> #include <Adafruit_BluefruitLE_SPI.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "YourAuthToken"; // SHARED SPI SETTINGS (see adafruit webpages for details) #define BLUEFRUIT_SPI_CS 8 #define BLUEFRUIT_SPI_IRQ 7 #define BLUEFRUIT_SPI_RST 4 // Optional but recommended, set to -1 if unused #define BLUEFRUIT_VERBOSE_MODE true // Create ble instance, see pinouts above Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST); //pin name const int forwardL = 11; const int rearL = 10; const int forwardR = 5; const int rearR = 6; long Speed; long SpeedL, SpeedR; BLYNK_WRITE(V0) { int x = param[0].asFloat(); int y = param[1].asFloat(); x = map(x, -10, 10, 255, -255); y = map(y, -10, 10, 255, -255); Serial.print("x: "); Serial.print(x); Serial.print(" y: "); Serial.print(y); Speed = sqrt(x*x+y*y); if(Speed > 255){ Speed = 255; } Serial.print(" Speed: "); Serial.println(Speed); if(y >= 0){ if(x >= 0){ SpeedL = 0; SpeedR = abs(x); }else{ SpeedL = abs(x); SpeedR = 0; } analogWrite(forwardL, Speed - SpeedL); analogWrite(rearL, 0); analogWrite(forwardR, Speed - SpeedR); analogWrite(rearR, 0); }else{ if(x >= 0){ SpeedL = 0; SpeedR = abs(x); }else{ SpeedL = abs(x); SpeedR = 0; } analogWrite(forwardL, 0); analogWrite(rearL, Speed - SpeedL); analogWrite(forwardR, 0); analogWrite(rearR, Speed - SpeedR); } } void setup() { Serial.begin(9600); ble.begin(BLUEFRUIT_VERBOSE_MODE); ble.factoryReset(); // Optional ble.setMode(BLUEFRUIT_MODE_DATA); Blynk.begin(auth, ble); } void loop() { Blynk.run(); } |
Operate the radio controlled by the horizontal (gravity) acceleration value [x, Y] of the smartphone screen.
Blynk ble connection
Click on the BLE widget of the project.
Click on the “Connect BLE device”.
Then connect to “Adafruit Bluefruit LE” by clicking “OK”.
Operating
Thanks to Blynk, it seems to be able to do various things intuitively crispy because the cooperation of the smartphone and the device can be insanely easy ♪