Ezreal's Gauntlet Code (Movement):
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Mouse.h>
#include <Wire.h>
#include <SPI.h>
int range = 12;
int delayTimer = 2;
int center = range/2;
int threshold = range / 4;
float minMotionx = 1;
float maxMoition = 10;
bool on = false;
void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:
CircuitPlayground.begin();
Serial.begin(9600);
Mouse.begin();
CircuitPlayground.setBrightness(75);
for(int i = 0; i < 9; i++){
CircuitPlayground.setPixelColor(i,0,0,128);
}
CircuitPlayground.setAccelRange(LIS3DH_RANGE_2_G);
//need to create a variable to store the mouses last know position from the center point
// Using mouse libraray exmaple code as a base lets cook
}
float AccelReader(){
float x = 0;
float y = 0;
float z = 0;
//Should it be a one time read on call or should i have a read for a set Time?
//The examples I've seen have had a fixed read but i think that wil
for(int i = 0; i < 15; i++){
x += CircuitPlayground.motionX();
y += CircuitPlayground.motionY();
z += CircuitPlayground.motionZ();
}
//might need to trim these to be an average or make a buffer
return sqrt(x*x + y*y + z*z);
}
float lerp(float x, float x0, float x1, float y0, float y1) {
// Check if the input value (x) is outside its desired range and clamp to
// those min/max y values.
if (x <= x0) {
return y0;
}
else if (x >= x1) {
return y1;
}
// Otherwise compute the value y based on x's position within its range and
// the desired y min & max.
return y0 + (y1-y0)*((x-x0)/(x1-x0));
}
void loop() {
// put your main code here, to run repeatedly:
if(CircuitPlayground.leftButton() == HIGH && on == false){
on = true;
}
if(CircuitPlayground.rightButton() == HIGH && on == true){
on = false;
}
if(on){
float inputX = CircuitPlayground.motionX();
Serial.println(CircuitPlayground.motionX());
float inputY = CircuitPlayground.motionY();
Serial.println(CircuitPlayground.motionY());
float x_mag = abs(inputX);
float x_mouse = lerp(x_mag, minMotionx, maxMoition, 0.0, 12);
float y_mag = abs(inputY);
float y_mouse = lerp(y_mag, minMotionx, maxMoition, 0.0, 12);
// Change the mouse direction based on the direction of the acceleration.
if (inputX < 0) {
x_mouse *= -1.0;
}
if (inputY < 0) {
y_mouse *= -1.0;
}
//int mouseX = Mapping(inputX, 0);
//Serial.println(inputX);
//int mouseY = Mapping(inputY, 1);
//Serial.println(inputY);
Mouse.move((int)x_mouse, (int)y_mouse, 0);
delay(10);
if(CircuitPlayground.slideSwitch()){
Mouse.click(MOUSE_RIGHT);
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.