If you could replace one sensor on this controller, what sensor would it be, and what sensor would you replace it with?
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>
#include <Keyboard.h>
#include <Adafruit_ICM20948.h>
#include <Mouse.h>
const int verticalPotPin = A0; // Pin for vertical potentiometer
const int horizontalPotPin = A7; // Pin for horizontal potentiometer
// Angle for tilt detection (left/right)
const float yawThreshold = 15; // Yaw threshold for W/S detection in degrees
float yaw = 0;
Adafruit_ICM20948 icm;
const float liftThreshold = 3.0; // Z-axis threshold for lift detection
bool inAir = false; // Tracks if the sensor is lifted
void setup() {
CircuitPlayground.begin();
pinMode(CPLAY_SLIDESWITCHPIN, INPUT_PULLUP);
pinMode(A6, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
Serial.begin(115200);
while (!Serial) delay(10);
Serial.begin(115200);
Mouse.begin();
// Initialize ICM20948
if (!icm.begin_I2C()) {
Serial.println("Failed to find ICM20948 chip");
while (1) delay(10);
}
Serial.println("ICM20948 Found!");
// Initialize Keyboard
Keyboard.begin();
}
void loop() {
sensors_event_t accel, gyro, mag, temp;
icm.getEvent(&accel, &gyro, &mag, &temp);
yaw += gyro.gyro.z; // Update yaw based on gyroscope data
// Check for lift
if (accel.acceleration.z < liftThreshold) {
if (!inAir) {
Keyboard.press(' '); // Press Space when first lifted
delay(100); // Small delay to ensure keypress is registered
Keyboard.release(' ');
inAir = true; // Set flag indicating sensor is in the air
}
} else {
inAir = false; // Reset flag if not lifted
}
if(analogRead(A2) > 650){ // Read Analog Direction to move forward
Keyboard.press('w');
} else {
Keyboard.release('w');
}
if(analogRead(A2) < 400){ // Read Analog Direction to move backward
Keyboard.press('s');
} else {
Keyboard.release('s');
}
if(digitalRead(A3)){ // toggle switch that activates shift
Keyboard.press(KEY_LEFT_SHIFT);
} else{
Keyboard.release(KEY_LEFT_SHIFT);
}
if(!digitalRead(A1)){ //Reads a digital input to move right
Keyboard.press('a');
} else{
Keyboard.release('a');
}
if(!digitalRead(A6)){ //Reads a digital input to move left
Keyboard.press('d');
} else{
Keyboard.release('d');
}
// Reset yaw if near neutral to avoid drift
if (abs(yaw) < yawThreshold / 2) {
yaw = 0;
}
// Read potentiometer values
int horizontalValue = analogRead(horizontalPotPin);
// Map potentiometer values to mouse movement
int horizontalMovement = map(horizontalValue, 0, 1023, -10, 10); // Adjust range as needed
// Move the mouse
Mouse.move(horizontalMovement, 0);
// Add a small delay to avoid excessive movement
delay(50);
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.