I am trying to program a timer that interacts with an IR distance sensor that starts a timer on an LCD when an object is at a certain distance from the sensor and stays on as long as there is an object in the distance of the sensor and once an object is not longer there the timer should stop counting. I am trying to use an interrupt to trigger the timer to start and to stop the timer but for some reason its only lasting for a few seconds and I don't know why. If someone can help me with this I'll really appreciate it.
#include <TimerOne.h>
// This example uses the timer interrupt to blink an LED
// and also demonstrates how to share a variable between
// the interrupt and the main program.
#define trigPin 13
#define echoPin 12
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int sec=0;
int MIN=0;
int milli = 0;
int clockPin = A4;
void setup(void)
{
pinMode(clockPin,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.print(" Elapsed Time: ");
Timer1.initialize();
Timer1.attachInterrupt(Timer);
}
// The interrupt will blink the LED, and keep
// track of how many times it has blinked.
volatile unsigned long clocki = 0; // use volatile for shared variablesint dumb = 1;
//int counttime = 1;
int countvar = 1;
int move1 = 1;
void Timer(void)
{
if(move1== 0){
clocki = clocki++;
while (milli<99) {
delay(1);
milli=milli++;
lcd.setCursor(6,1);
lcd.print(MIN);
lcd.print(":");
lcd.print(sec);
lcd.print(":");
if (milli<10) {
lcd.print("0");
lcd.print(milli);
}
else {
lcd.print(milli);
}
}
if (milli == 99) {
milli = 0;
sec=sec++;
}
if (sec == 59)
{
sec=0;
MIN=MIN++;
}
if(clocki > 100)
{
move1 = 1;
if(countvar == 1){
countvar = 0;
}
else if(countvar == 0){
countvar = 1;
}
}
}
}
void loop(void)
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
unsigned long clock; // holds a copy of the Timer
// to read a variable which the interrupt code writes, we
// must temporarily disable interrupts, to be sure it will
// not change while we are reading. To minimize the time
// with interrupts off, just quickly make a copy, and then
// use the copy while allowing the interrupt to keep working.
noInterrupts();
clock = clocki;
interrupts();
while (distance < 10) { // This is where distance reading happens
Timer();
if(move1 == 1){
if(countvar == 1){
analogWrite(A4,HIGH);
move1 = 0;
}
else if (countvar == 0){
analogWrite(A4,LOW);
move1 = 0;
}
}
delay(100);
break;
}
}
hi, here come a silly question...
couldn't you just use the millis() function?
Detect when something comes into range and take notice of that time.
On each pass if thing is still in range, see how much time passed and print it.
if it is out or range stop counting.
I am trying to program a timer that interacts with an IR distance sensor that starts a timer on an LCD when an object is at a certain distance from the sensor and stays on as long as there is an object in the distance of the sensor and once an object is not longer there the timer should stop counting.
An IR sensor generally reports that there is, or is not, motion. It doesn't tell you how far away the person (or other warm body creature) is.
The code you show does not indicate an IR sensor is being used. Rather, it looks like an ultrasonic sensor.
I am trying to use an interrupt to trigger the timer to start and to stop the timer
Why? You know when motion was detected. You know when it is not detected. There is no need for interrupts. Interrupts are for things that need to be dealt with NOW. Nothing in your situation makes it critical to deal with the change within nanoseconds of the change being detected. Especially since the change will happen potentially long before you detect it.