need help for stopwatch controlled by accelerometer

Hello,

I am trying to develop a code to accelerometer(adxl345) controlled stopwatch.

Basicly, stopwatch should start at x direction between 30 and 60.(30<x<60)

and should stop at y direction between 50 and 90(50<y<90).

lcd writing should be:

x=(value) y=(value) z=(value)
minute:second:msecond

tried to make it with if/else on this time code:

void stopWatch(){

mseconds++;
delay(1);
if (mseconds>99)

{

mseconds=0;      

seconds++;     
}

if (seconds>59)

{
minutes++;

seconds=0;

}

 

if(minutes>59)

{

minutes=0;

}
}

}

but i cant make it start and keep counting out of starting range and stop at stop range.

How can i make it?

delay(1);

Don't do that. That is one of the worst ways to count seconds.

Have you got the accelerometer working and outputting valid data?

Where is the rest of your code? Show us the best attempt so far and tell us what it does and what you want it to do different.

Have you set the accelerometer on a flat stable surface and watched the acceleration readings given? You may find that for the accelerometer, rarely, gives 0 as an output. You may find that the well calibrated accelerometer gives a noise of accelerations that may be smoothed out over the long term. Basically, the accelerometer data will need to be worked out before it will give a useful result set.

#include <StopWatch.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#include <Wire.h>

#define accel_module (0x53)
byte values[6] ;
char output[512];

int runMinutes;
int runSeconds;

const unsigned long period = 1000;


void setup() {
  
       lcd.begin(16, 2);
      
      Wire.begin();
    Serial.begin(9600);

  Wire.beginTransmission(accel_module);
  Wire.write(0x2D);
  Wire.write(0);
  Wire.endTransmission();
  Wire.beginTransmission(accel_module);
  Wire.write(0x2D);
  Wire.write(16);
  Wire.endTransmission();
  Wire.beginTransmission(accel_module);
  Wire.write(0x2D);
  Wire.write(8);
  Wire.endTransmission();

  

}

void stopwatch(int val){
  

  if(val == 1){
 int runMinutes=0;
 int runSeconds=0;
  }

  else{
  unsigned long runMillis= millis();
  }

unsigned long allSeconds=millis()/1000;
int runHours= allSeconds/3600;
int secsRemaining=allSeconds%3600;
 runMinutes=secsRemaining/60;
 runSeconds=secsRemaining%60;

}



void loop() {

 
 int xyzregister = 0x32;
int x, y, z;

Wire.beginTransmission(accel_module);
Wire.write(xyzregister);
Wire.endTransmission();

Wire.beginTransmission(accel_module);
Wire.requestFrom(accel_module, 6);

int i = 0;
while(Wire.available()){
values[i] = Wire.read();
i++;
}

x = (((int)values[1]) << 8) | values[0]; 
y = (((int)values[3])<< 8) | values[2]; 
z = (((int)values[5]) << 8) | values[4]; 
sprintf(output, "%d %d %d", x, y, z); 
Serial.print(output); 
Serial.write(10);

lcd.setCursor(0, 0);
lcd.print(int (x) + String(":") + int(y) + String(":") + int(z));


if(0<y<1){

stopwatch(1);
lcd.setCursor(0, 1);
  lcd.print((runMinutes) + String(":") + (runSeconds));
}


}

This is closest one so far.By your advice i changed the timer to this from the delay(1) one. x/y/z outputs are another problem i have, but not the most one.

0<y<1 means when y is positive timer counts, when its negative it stops.

I think i can figure out the values with some experiments but i still have to start it at some position that dedicated by me, and some position to stop it (stopping is hardest i guess). Any other positions beside start and stop point shouldnt affect the clock.

  if(val == 1){

int runMinutes=0;
int runSeconds=0;
  }

By putting the word "int" here you create two new local variables inside this {} block. This has no effect on the global variables of the same name.

 runMinutes=secsRemaining/60;

runSeconds=secsRemaining%60;

Perhaps you meant to add here? Look at the reference for the += operator.

The value that you are adding here is odd too. It is the number of milliseconds since the Arduino was powered up, not the milliseconds since you started recording time. Since you also truncated this to seconds and this loop runs at high speed, many thousands of times per second, you would only ever add zero.