time millis() in My aduino project

Hello guys, I’m new here. Last week I was trying to make a speedometer project with distance and timer (for a bike). Everything is fine until i have a struggle with valtime as you can see in the sketch. Speed value shows constant value like 6.02 all the time because valtime does not change ( millis()-previous ).
Idk why and how can i fix this. Need help:( (Sorry for my bad english)

unsigned long previous, valtime, impulses;
float speedometer
void setup(){
  
  attachInterrupt(0, detect, RISING);
} 
void loop() {
  delay(1000); 
  detachInterrupt(0);
  valtime=millis()-previous;
  speedometer=(circuit*3600.0 /valtime);
  previous=millis();
  delay(100); 
  Lcd();  
  attachInterrupt(0, detect, RISING);

}

void detect(){
  impulses++;
}

_1.ino (361 Bytes)

Your example doesn't compile. There are syntax errors (first off: a missing semicolon on line 2) and you seem to have included some libraries or something you aren't telling us about? And why are you detaching, then reattaching your interrupt every 1.1 seconds? You could use cli() and sei() if that's really necessary.

All variables see both inside and outside an ISR ( interrupt service routine ) must be delclaired volatile.

In that code you increment one value in the ISR but never do anything with it. The variable valTime is never altered and the variable circuit is never defined. So in short that is not your code, because it could never run like you claim you did.

Thank you guys for the comment. This is the whole code of my project(Speed, Distance and time )
A bit messy with a keypad code.

#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 20, 4);

////keypad//////
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
int v1 = 0;
int v2 = 0;
int v3 = 0;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}};
byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3}; 
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/////////////////


unsigned long previous, triptime, valtime = 0, impulses;
float speedometer, dist, aspeed;
float circuit=2.0; //2 m circumference of wheel


void setup() {

  Serial.begin(115200); 
  lcd.begin(); 
  lcd.setCursor(1, 1);
  lcd.print("PROJECT : CYCLING");
  lcd.setCursor(2, 2);
  lcd.print("DISTANCE MEASURE");
  delay(1500);
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("SetDistance!:");
  lcd.setCursor(12,1);
  v1 = GetNumber();
  lcd.setCursor(14,1); 
  lcd.print(v1);

  v2 = GetNumber();
  lcd.setCursor(13,1);
  lcd.print(v2);

  v3 = v1+(v2*10);
  lcd.setCursor(1,2);
  lcd.print("Installing..:");
  lcd.print(v3);
  lcd.print("  km");
  delay(3000);
  lcd.clear(); 

  attachInterrupt(0, detect, RISING);
} 


void loop() {
  delay(1000); 
  detachInterrupt(0);
  valtime=millis()-previous;
  speedometer=(circuit*3600.0 /valtime);
  previous=millis();
  delay(100); 
  Lcd();  
  attachInterrupt(0, detect, RISING);

}


void Lcd(){
  lcd.setCursor(0, 1);
  lcd.print("Speed:");
  lcd.setCursor(8, 1);
  lcd.print(speedometer);
  lcd.setCursor(14,1);
  lcd.print("km/h");

  //displays averagr speed
  aspeed= dist/(millis()/1000.0)*3600.0;
  lcd.setCursor(0, 2);
  lcd.print("A.speed:");
  lcd.setCursor(8, 2);
  lcd.print(aspeed);
  lcd.setCursor(14,2);
  lcd.print("km/h");

  triptime=millis()/60000;
  lcd.setCursor(0, 3);
  lcd.print("Time:");
  lcd.setCursor(7, 3);
  lcd.print(triptime);
  lcd.setCursor(10, 3);
  lcd.print("m");


  lcd.setCursor(0, 0);
  lcd.print("Dist:");
  dist = impulses*circuit/1000.00;
  lcd.setCursor(6,0);
  lcd.print(dist);
  lcd.print("/");
  lcd.print(v3);
  lcd.setCursor(14,0);
  lcd.print("km");
}                       

void detect(){
  impulses++;
}


int GetNumber()
{  
  int num = 0;
  char key = kpd.getKey();
  while(key!='#'){
    switch (key)
    {
    case NO_KEY:
      break;

    case '0': case '1':  case '2': 
    case '3': case '4':  case '5': 
    case '6': case '7': 
    case '8': case '9':
     lcd.setCursor(13,0);
     lcd.print(key);
     num =  (key - '0');
     break;

    case '*':
     num = 0;
     lcd.clear();
     lcd.setCursor(0,1);
     lcd.print("SetDistance!:");
     lcd.setCursor(14,1);
     lcd.print(v1);
     break;
    } 
    key = kpd.getKey();
  }

  return num;

}

The variable impulses should be a volatile byte unless your bike goes extremely fast.

Where is circuit declared?

Why turn the interrupt off and on? Copy impulses then set it to 0, takes less than a microsecond.

Speed is distance / time and your time should be 1 second, but not the way you do it. Get rid of the delay,

float circumference = 2; // 2 meters, only a guess

void loop() {
  if ( millis()-previous >= 1000UL ) {
  byte revs = impulses;
  impulses = 0;
  speedometer=((float)revs * circumference * 3600.0 / metersperkilometer); distance/sec times seconds/hour = distance/hour
  previous += 1000UL; // self-correcting
  Lcd(); 
 }
}

With sensing only 1x per rev this will never be much good at low speed.

Why turn the interrupt off and on?

This is so that a multi byte number is not changed part way through accessing it.

To the OP, when we make suggestions what happens is that you should incorporate them into your code, test the code and report back posting your amended code.

Make it easy for other learners: When you use these kind of lines:

    #include <LiquidCrystal_I2C.h>

include the url of your library as a comment like this:

    #include <LiquidCrystal_I2C.h> // https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

That way we are all on the same page.