Teensy 3.1, ADXL345, and buttons for keyboard inputs

Status
Not open for further replies.

mattkuntz

New member
Hi everyone,

I'm working on a Teensy 3.1 project that combines buttons and an ADXL345 to input keyboard buttons that will play video games on the web. The ADXL345 will hopefully hit the arrow buttons based upon how it is tilted.

I'd love to get some guidance on my connections and any thoughts on the code.

Thank you,
matt


CONNECTIONS

GND -> AGND
VCC -> 3.3V
CS -> 3.3V
INT1 ->
INT2 ->
SDO ->
SDA -> PIN 18
SCL -> PIN 19



CODE

#include <Wire.h>
#include <ADXL345.h>


ADXL345 adxl; //variable adxl is an instance of the ADXL345 library

const int buttonPinA = 4;
const int buttonPinC = 5; // input pin for pushbutton

const int buttonPinW = 6; // input pin for pushbutton
const int buttonPinX = 7;
const int buttonPinZ = 8; // input pin for pushbutton
const int buttonPinSP = 9;
int previousButtonState = HIGH; // for checking the state of a pushButton
int previousButtonStateC = HIGH;
int previousButtonStateW = HIGH; // for checking the state of a pushButton
int previousButtonStateX = HIGH;
int previousButtonStateZ = HIGH; // for checking the state of a pushButton
int previousButtonStateSP = HIGH;


int xdirectiionCheck =0;
int ydirectiionCheck =0;
int zdirectiionCheck =0;

int counter = 0; // button push counter
int x,y,z;
void setup() {

Serial.begin(9600);
adxl.powerOn();

//set activity/ inactivity thresholds (0-255)
adxl.setActivityThreshold(3); //62.5mg per increment
adxl.setInactivityThreshold(95); //62.5mg per increment
adxl.setTimeInactivity(1); // how many seconds of no activity is inactive?

//look of activity movement on this axes - 1 == on; 0 == off
adxl.setActivityX(1);
adxl.setActivityY(1);
adxl.setActivityZ(1);

//look of inactivity movement on this axes - 1 == on; 0 == off
adxl.setInactivityX(1);
adxl.setInactivityY(1);
adxl.setInactivityZ(1);

//look of tap movement on this axes - 1 == on; 0 == off
adxl.setTapDetectionOnX(0);
adxl.setTapDetectionOnY(0);
adxl.setTapDetectionOnZ(1);

//set values for what is a tap, and what is a double tap (0-255)
adxl.setTapThreshold(50); //62.5mg per increment
adxl.setTapDuration(15); //625μs per increment
adxl.setDoubleTapLatency(80); //1.25ms per increment
adxl.setDoubleTapWindow(200); //1.25ms per increment

//set values for what is considered freefall (0-255)
adxl.setFreeFallThreshold(7); //(5 - 9) recommended - 62.5mg per increment
adxl.setFreeFallDuration(45); //(20 - 70) recommended - 5ms per increment

//setting all interupts to take place on int pin 1
//I had issues with int pinACW ACW 2, was unable to reset it
adxl.setInterruptMapping( ADXL345_INT_SINGLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_DOUBLE_TAP_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_FREE_FALL_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_ACTIVITY_BIT, ADXL345_INT1_PIN );
adxl.setInterruptMapping( ADXL345_INT_INACTIVITY_BIT, ADXL345_INT1_PIN );

//register interupt actions - 1 == on; 0 == off
adxl.setInterrupt( ADXL345_INT_SINGLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_DOUBLE_TAP_BIT, 1);
adxl.setInterrupt( ADXL345_INT_FREE_FALL_BIT, 1);
adxl.setInterrupt( ADXL345_INT_ACTIVITY_BIT, 1);
adxl.setInterrupt( ADXL345_INT_INACTIVITY_BIT, 1);


// make the pushButton pin an input:
pinMode(buttonPinA, INPUT); pinMode(buttonPinC, INPUT); pinMode(buttonPinW, INPUT); pinMode(buttonPinX, INPUT); pinMode(buttonPinZ, INPUT); pinMode(buttonPinSP, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}

void loop() {


accelerometerfunc();


// read the pushbutton:
int buttonState = digitalRead(buttonPinA);
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {

Keyboard.write(65);
}
// save the current button state for comparison next time:
previousButtonState = buttonState;



int buttonStateC = digitalRead(buttonPinC);
// if the button state has changed,
if ((buttonStateC != previousButtonStateC)
// and it's currently pressed:
&& (buttonStateC == HIGH)) {


Keyboard.write(67);

}
// save the current button state for comparison next time:
previousButtonStateC = buttonStateC;

int buttonStateW = digitalRead(buttonPinW);
// if the button state has changed,
if ((buttonStateW != previousButtonStateW)
// and it's currently pressed:
&& (buttonStateW == HIGH)) {

Keyboard.write(87);

}
// save the current button state for comparison next time:
previousButtonStateW = buttonStateW;




int buttonStateX = digitalRead(buttonPinX);
// if the button state has changed,
if ((buttonStateX != previousButtonStateX)
// and it's currently pressed:
&& (buttonStateX == HIGH)) {


Keyboard.write(88);

}
// save the current button state for comparison next time:
previousButtonStateX = buttonStateX;



int buttonStateZ = digitalRead(buttonPinZ);
// if the button state has changed,
if ((buttonStateZ != previousButtonStateZ)
// and it's currently pressed:
&& (buttonStateZ == HIGH)) {


Keyboard.write(90);

}
// save the current button state for comparison next time:
previousButtonStateZ = buttonStateZ;



int buttonStateSP = digitalRead(buttonPinSP);
// if the button state has changed,
if ((buttonStateSP != previousButtonStateSP)
// and it's currently pressed:
&& (buttonStateSP == HIGH)) {

Keyboard.write(32);

}
// save the current button state for comparison next time:
previousButtonStateSP = buttonStateSP;
}



void accelerometerfunc(){

x=0; y = 0; z = 0;
//Boring accelerometer stuff

adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables x,y,z



// Output x,y,z values - Commented out
Serial.print(" X: ");Serial.print(x);Serial.print(" y: ");
Serial.print(y);Serial.print(" Z: ");
Serial.println(z);

//read interrupts source and look for triggerd actions

//getInterruptSource clears all triggered actions after returning value
//so do not call again until you need to recheck for triggered actions
byte interrupts = adxl.getInterruptSource();

// freefall
if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
Serial.println("freefall");
//add code here to do when freefall is sensed
}

//inactivity
if(adxl.triggered(interrupts, ADXL345_INACTIVITY)){
Serial.println("inactivity");
//add code here to do when inactivity is sensed
}

//activity
if(adxl.triggered(interrupts, ADXL345_ACTIVITY)){
Serial.println("activity");
/////////////////////////////////////////////////////////////////////////////////////////////////
if(x>255){x=255;} if(y>255){y=255;} if(z>255){z=255;}
xdirectiionCheck =x-xdirectiionCheck;
if(xdirectiionCheck < -4){

Keyboard.write(KEY_LEFT_ARROW);


}else if(xdirectiionCheck >4){ Keyboard.write(KEY_RIGHT_ARROW);
}xdirectiionCheck=x;

ydirectiionCheck =y-ydirectiionCheck;
if(ydirectiionCheck < -4){

Keyboard.write(KEY_DOWN_ARROW);


}else if(ydirectiionCheck >4){ Keyboard.write(KEY_UP_ARROW);
}ydirectiionCheck=y;
///////////////////////////////////////////////////////////////////////////////////////////////
//add code here to do when activity is sensed
}

//double tap
if(adxl.triggered(interrupts, ADXL345_DOUBLE_TAP)){
Serial.println("double tap");
//add code here to do when a 2X tap is sensed
}

//tap
if(adxl.triggered(interrupts, ADXL345_SINGLE_TAP)){
Serial.println("tap");
//add code here to do when a tap is sensed
}



}
 
I'm leading towards these connections.

CONNECTIONS

GND -> AGND
VCC -> 3.3V
CS -> 3.3V
INT1 -> PIN 1
INT2 -> PIN 2
SDO -> PIN 3
SDA -> PIN 18
SCL -> PIN 19
 
Status
Not open for further replies.
Back
Top