Created
October 17, 2011 16:52
-
-
Save Addidis/1293063 to your computer and use it in GitHub Desktop.
playing around with an led and a Tautic accelerometer board.
This file contains 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
// Shake switched led by addidis | |
// HW : Tautic accelerometer board +4.7k pull ups, uno32, led +330 resistor to led pin | |
// Accelerometer connections : SDA - A4 , SCL - A5, INT - 7, gnd - gnd, vdd - 3.3v 4.7k pull ups on SDA SCL | |
// LED connections: pin 6 to led, to 330 ohm to ground . | |
//notes - not debounced so it flickers. Really, just playing around with the features of the accelerometer. | |
#include <Wire.h> | |
int accelintpin = 7; | |
int ledpin = 6; | |
volatile int state = 0; | |
void setup() | |
{ | |
pinMode(ledpin, OUTPUT); | |
digitalWrite(ledpin, LOW); | |
pinMode(accelintpin, INPUT); | |
attachInterrupt(2,accelISR, FALLING); | |
Serial.begin(38400); // start serial for output | |
Wire.begin(); | |
} | |
void accelISR(void) | |
{ | |
if (state== 0){ | |
digitalWrite(ledpin, HIGH); | |
state=1; | |
} | |
else { | |
digitalWrite(ledpin, LOW); | |
state=0; | |
} | |
} | |
void loop() | |
{ | |
digitalWrite(ledpin, LOW); | |
delay(10); | |
Wire.beginTransmission(0x4C); //Set MMA7660FCR1 to active mode | |
Wire.send(0x06); | |
Wire.send(0xE0); | |
Wire.send(0x01); | |
Wire.endTransmission(); | |
delay(100); | |
char x,y,z,tilt,sample,mode,fac; | |
while(1) | |
{ | |
unsigned char val; | |
Wire.beginTransmission(0x4C); | |
Wire.send(0x00); // register to read | |
Wire.endTransmission(); | |
Wire.requestFrom(0x4C, 11); // read a byte | |
while(!Wire.available()) { } // wait for data | |
x = Wire.receive(); | |
y = Wire.receive(); | |
z = Wire.receive(); | |
tilt = Wire.receive(); | |
sample = Wire.receive(); | |
Wire.receive(); //throw away byte | |
Wire.receive(); //throw away byte | |
mode = Wire.receive(); | |
Wire.receive(); //throw away byte | |
Wire.receive(); //throw away byte | |
Wire.receive(); //throw away byte | |
fac = Wire.receive(); | |
Serial.print("X="); | |
Serial.print(x, DEC); | |
Serial.print(", Y="); | |
Serial.print(y, DEC); | |
Serial.print(", Z="); | |
Serial.print(z, DEC); | |
Serial.print(", Mode="); | |
Serial.print(mode,DEC); | |
Serial.print(", Tilt="); | |
Serial.print(tilt,DEC); | |
Serial.print(", sample="); | |
Serial.print(sample,DEC); | |
Serial.print(", fac="); | |
Serial.print(fac,DEC); | |
Serial.println("."); | |
delay(100); | |
} | |
} | |
void wrReg(unsigned char reg, unsigned char val) | |
{ | |
Wire.beginTransmission(0x4C); | |
Wire.send(reg); | |
Wire.send(val); | |
Wire.endTransmission(); | |
} | |
unsigned char rdReg(unsigned char reg) | |
{ | |
unsigned char val; | |
Wire.beginTransmission(0x4C); | |
Wire.send(reg); // register to read | |
Wire.endTransmission(); | |
digitalWrite(A4, HIGH); | |
delay(5); | |
digitalWrite(A4, LOW); | |
delay(5); | |
Wire.requestFrom(0x4C, 1); // read a byte | |
while(!Wire.available()) { } // wait for data | |
return Wire.receive(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment