Created
September 7, 2016 21:57
-
-
Save szczys/56fcea281f91df65a5acc5fd45d2e3ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
BeanHid.enable(); | |
// Serial port is initialized automatically; we don't have to do anything | |
} | |
void loop() { | |
AccelerationReading accel = Bean.getAcceleration(); | |
int16_t x = accel.xAxis; | |
int16_t y = accel.yAxis; | |
int16_t z = accel.zAxis; | |
// Add a deadzone. If Bean is reasonabally flat, don't move the mouse pointer | |
if (abs(x) < 20) { | |
x = 0; | |
} | |
if (abs(y) < 20) { | |
y = 0; | |
} | |
// we want to map the movements to reasonable values. | |
// This constrains the accelerometer to -20 to 20. | |
int16_t mousex = map(x, -60, 60, -20, 20); | |
int16_t mousey = map(y, -60, 60, 20, -20); | |
BeanHid.moveMouse(mousex, mousey); | |
if (Serial.available()) { | |
byte read = Serial.read(); | |
if (read == '1') { | |
// send the ASCII number 1 to type a message | |
BeanHid.sendKeys("Hello, world!"); | |
} else if (read == '2') { | |
// send the ASCII number 2 to use a media command | |
BeanHid.sendMediaControl(PLAY); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment