I really need some help. I have absolutely no experience with accelerometers and have a project that would work great if I could integrate one into it. I am not hinting to have some write me a code, just get me looking in the right direction.
My code (below) was written to work with 2 buttons and an LED. Basically, its nothing but a reaction timer using an RGB liquid crystal display.
Sequence of events...
- power on
- displays “push button to start”
- when panel button is pushed, the loop begins by randomly selecting a “FIRE” time when “fire” button is pressed, it echoes the time it took
- my FOR statement, allows it to loop three times before returning to the begin.
This was all what I set out to accomplish, originally. My sister has been using it at a retirement home for some of the patients to play with. Lately she asked if instead of a “fire” button it could be programmed to simply, tap the whole unit, push the unit, or even use a tether of some sort. I figured an accelerometer was the ticket.
I purchased an ADXL345, I tried playing with several sketches from GitHub. What I find is the accelerometer is ALWAYS giving out information. I thought it would actually “rest” when not being physically moved.
How can I use my ADXL345 to be at complete standstill, until it is “moved,” that it would activate a WHILE statement???
#include <Wire.h>
#include <LiquidCrystal.h>
#include <SparkFun_ADXL345.h>
ADXL345 adxl = ADXL345();
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int lcdRedPWMPin = 17;
int lcdGreenPWMPin = 16;
int lcdBluePWMPin = 15;
void setDisplayRGB(int r, int g, int b)
{
analogWrite(lcdRedPWMPin, r);
analogWrite(lcdGreenPWMPin, g);
analogWrite(lcdBluePWMPin, b);
}
void setup()
{
lcd.begin(16, 2);
lcd.clear();
pinMode(13, OUTPUT);
Serial.begin(115200);
adxl.powerOn();
adxl.setRangeSetting(2); // Accepted values are 2g, 4g, 8g or 16g
//adxl.setSpiBit(0);
adxl.setActivityXYZ(1, 1, 1);
adxl.setActivityThreshold(70);
adxl.ActivityINT(1);
pinMode(13, INPUT);
digitalWrite(13, LOW);
pinMode(9, INPUT); //panel button
digitalWrite(9, HIGH);
pinMode(8, INPUT); //FIRE button
digitalWrite(8, HIGH);
pinMode(10, OUTPUT); //FIRE led
digitalWrite(10, LOW);
// int fireButton = digitalRead(8);
int panelButton = digitalRead(9);
lcd.clear();
setDisplayRGB(100, 100, 75);
lcd.setCursor(1, 0);
lcd.print("Let's see what");
lcd.setCursor(1, 1);
lcd.print("what you got!");
delay(2000);
}
double timeToFire = 0; //time it took to (fire-trigger)
double fireTimerStarts = millis(); //fire (timer starts)
double triggerTimerStops ; //trigger (timer stops)
int ran; //random
void loop()
{
int x, y, z;
adxl.readAccel(&x, &y, &z);
// byte interrupts = adxl.getInterruptSource();
int average = (x + y + z);
while (digitalRead(9) == HIGH)
{
lcd.clear();
setDisplayRGB(40, 200, 75); //purple 40,200,75
lcd.setCursor(0, 0);
lcd.print("Press Button");
lcd.setCursor(0, 1);
lcd.print("to Start ...");
delay(50);
}
for (int panelButton = 0; panelButton < 3; panelButton++)
{
lcd.clear();
lcd.setCursor(0, 0);
setDisplayRGB(255, 255, 0); //blue
lcd.print(" ---READY---");
delay((int)random(1000, 5000));
{
ran = random(1, 3);
Serial.println(ran);
if ( ran == 1)
lcd.clear();
lcd.setCursor(0, 0);
setDisplayRGB( 0, 155, 155 ); //red
lcd.print(" <FIRE> ");
lcd.setCursor(0, 1);
lcd.print(average);
fireTimerStarts = millis();
digitalWrite(10, HIGH);
// if(adxl.triggered>activityThreshold)
while (adxl.triggered(13, HIGH))
//while (digitalRead(8) == HIGH)
{
triggerTimerStops = millis();
}
digitalWrite (13, LOW);
digitalWrite(10, LOW);
setDisplayRGB(100, 100, 225); //green
}
timeToFire = (triggerTimerStops - fireTimerStarts) / 1000;
delay(500);
{
lcd.clear();
setDisplayRGB(0, 255, 0);
lcd.setCursor(0, 1);
lcd.print(timeToFire);
lcd.setCursor(11, 1);
lcd.print("Sec's");
lcd.setCursor(0, 0);
lcd.print("Your time was ...");
delay(1500);
}
}
}