Im writing a program thats basically a g force meter. so to make it more accurate i need to check the accelerometer about every 10-20 ms. so i decided to try a timer interrupt. when it enters the function it hangs on the chuck.update() command.
excuse the code.
im just trying to get a proof of concept.
#include <math.h>
#include "Wire.h"
#include "WiiChuck.h"
#include "TimerOne.h"
WiiChuck chuck = WiiChuck();
float velocity =0;
float velocity_mph =0;
float velocity_acc =0;
float staccel =0;
float lataccel =0;
float gx=0;
float gy =0;
int pitch=0;
int roll=0;
int count=0;
long previous =0;
void setup(){
delay(1000);
Serial.begin(9600);
chuck.begin();
chuck.update();
clearLCD();
selectLineOne();
Serial.print("ACCELEROMETER");
selectLineTwo();
Serial.print("-----READY-----");
delay(1000);
//Timer1.initialize(500000);
// Timer1.attachInterrupt(callback);
}
int num =1;
void callback(){
help();
chuck.update();
}
void help(){
digitalWrite(13,num);
if(num==1)num=0;
else num=1;
}
void loop(){
if(millis()-previous >200){
previous = millis();
Print();
}
}
void Print(){
clearLCD();
selectLineOne();
Serial.print(" ST:");
printDouble(gx,1000);
Serial.print(" ");
Serial.print(roll);
Serial.print(" ");
printDouble(velocity_mph,10);
selectLineTwo();
Serial.print("LAT:");
printDouble(gy,1000);
Serial.print(" ");
Serial.print(pitch);
}
void get_data(){
chuck.update();
staccel+=chuck.readAccelX()/204;
lataccel+=chuck.readAccelY()/204;
count++;
velocity_acc = (staccel/50)*9.81;
velocity = velocity + velocity_acc;
velocity_mph = velocity * 2.23693629;
if(count> 5){
count = 0;
gx = staccel / 5;
gy = lataccel / 5;
staccel =0;
lataccel=0;
};
}
void selectLineOne(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(128, BYTE); //position
}
void selectLineTwo(){ //puts the cursor at line 0 char 0.
Serial.print(0xFE, BYTE); //command flag
Serial.print(192, BYTE); //position
}
void clearLCD(){
Serial.print(0xFE, BYTE); //command flag
Serial.print(0x01, BYTE); //clear command.
}
void backlightOn(){ //turns on the backlight
Serial.print(0x7C, BYTE); //command flag for backlight stuff
Serial.print(157, BYTE); //light level.
}
void backlightOff(){ //turns off the backlight
Serial.print(0x7C, BYTE); //command flag for backlight stuff
Serial.print(128, BYTE); //light level for off.
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
Serial.print(0xFE, BYTE);
}
void backlight(int val){ //turns on the backlight
val=val+128; // turn it to amounts understood by lcd
Serial.print(0x7C, BYTE); //command flag for backlight stuff
Serial.print(val, BYTE); //light level.
}
void selectcursor(int val1, int val2){ // val1 = line val2 = position
int val= 128+val2+64*val1;
Serial.print(0xFE, BYTE); //command flag
Serial.print(val, BYTE); //position
}
void printDouble( double val, unsigned int precision){
// prints val with number of decimal places determine by precision
// NOTE: precision is 1 followed by the number of zeros for the desired number of decimial places
// example: printDouble( 3.1415, 100); // prints 3.14 (two decimal places)
Serial.print (int(val)); //prints the int part
Serial.print("."); // print the decimal point
unsigned int frac;
if(val >= 0)
frac = (val - int(val)) * precision;
else
frac = (int(val)- val ) * precision;
if(frac <100)Serial.print(0);
if(frac <10)Serial.print(0);
Serial.print(frac,DEC) ;
}