Progress Review: Journey Controller (wearable)
Sketch:
Schematic:
Video:
Code:
//Libraries
#include <bluefruit.h>
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
//bluetooth variables
BLEDis bledis;
BLEHidAdafruit blehid;
int debounce = 100;
void setup() {
Bluefruit.begin();
Bluefruit.setTxPower(4);
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather 52");
bledis.begin();
blehid.begin();
startAdv();
Serial.begin(9600);
while (!Serial);
CircuitPlayground.begin();
pinMode(4, INPUT_PULLDOWN);
}
void loop() {
if (Bluefruit.connected()) {
//variables for aix value and sound sensor value
float m = CircuitPlayground.mic.soundPressureLevel(10);
float x = CircuitPlayground.motionX();
float y = CircuitPlayground.motionY();
float z = CircuitPlayground.motionZ();
//checking the values
Serial.print(m);
Serial.print("\t");
Serial.print(x);
Serial.print("\t");
Serial.print(y);
Serial.print("\t");
Serial.println(z);
//sound sensor reading controls the whislte of the character ("p" key).
if (m > 70) {
blehid.keyPress('p');
}
//Section for reading the air pressure input. Controls the character jump. ("Space" key)
//accelerometer controlls the character movement.("WASD" keys)
if (x > -2 && x < 2 && y > -10 && y < -8 && z > -2 && z < 2 && m < 70) {
blehid.keyRelease();
}
if (x > -2 && x < 2 && y > -10 && y < -8 && z < -2) {
blehid.keyPress('w');
}
if (x > -2 && x < 2 && y > -10 && y < -8 && z > 2) {
blehid.keyPress('s');
}
if (x < -2 && y > -10 && y < -8 && z > -2 && z < 2) {
blehid.keyPress('d');
}
if (x > 2 && y > -10 && y < -8 && z > -2 && z < 2) {
blehid.keyPress('a');
}
delay(debounce);
}
}
//bluetooth connection.
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_HID_KEYBOARD);
// Include BLE HID service
Bluefruit.Advertising.addService(blehid);
// There is enough room for the dev name in the advertising packet
Bluefruit.Advertising.addName();
/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)
For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.