Out of Range

const int tiltSensor = 2;
const int trigPin = 10;
const int echoPin = 13;


int tiltState;
int duration;
int distance;

void setup() {
  pinMode(tiltSensor, INPUT);
  Serial.begin(115200);
  attachInterrupt(1,blink,RISING);
}

void loop() {
  if(tiltState != 0) {
    tiltState = 0;
  }
}

void blink() {
  tiltState++;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = ((duration/2)*0.343);
  if (distance<20 || distance>4000) {
    Serial.println("Out of Range");
    } else {
      Serial.print("distance");
      Serial.println(" mm");
      }
      delay(150);
}

I'm trying to built a distance measurement using tilt switch.
So everytime the tilt switch is activated, the distance can be read through the ultrasonic sensor.
but what I get from the reading is always "Out of Range" reading.

The circuit should not be the problem as I verified with previous code without tiltsensor.
Can anyone please enlighten me why this code will only shows "Out of Range"?

You can't serial print from an ISR. You need to set a flag there instead and service it in loop(). I'm not sure that you can't use pulseIn() but that seems doubtful. You definitely can't use delay() in there.

Why do you think you need an interrupt to do this?

Delta_G:
The results certainly couldn't be correct if millis isn't updating.

pulseIn does not use millis() it’s actually calling a function implemented in ASM doing a bunch of loops

delayMicroseconds() just counts. So it isn't affected by micros() nor millis() and will work in an ISR (even if it’s not a great idea to wait in an ISR in general). You’ll mess up with further millis calls later on if you wait too long in the ISR though as the counter will have rolled over

The print indeed is not a good idea.

To OP: +1 with aarg => just get rid of the ISR and in your loop test the sensor position and if tilted run your distance measurement

aarg:
You can't serial print from an ISR. You need to set a flag there instead and service it in loop(). I'm not sure that you can't use pulseIn() but that seems doubtful. You definitely can't use delay() in there.

Why do you think you need an interrupt to do this?

I'm just practicing my coding skills.
Just trying to see if coding can solve daily problems.
May I ask how should it be coded?

by the way, I've just learn how to use the computer only a few years back.
I would greatly appreciate if anyone can be patient and help me with coding.
Coding can help keep my mind fresh.

In your loop you do

if (sensor is triggered) {
   // here  what you were doing in the ISR 
} else {
  // here do what you want to do when sensor is not triggered 
}

Of course you need to replace “sensor is triggered“ by the right command to Check your sensor (like a digitalRead and compare with LOW or HIGH)

J-M-L:
In your loop you do

if (sensor is triggered) {

// here  what you were doing in the ISR
} else {
  // here do what you want to do when sensor is not triggered
}




Of course you need to replace “sensor is triggered“ by the right command to Check your sensor (like a digitalRead and compare with LOW or HIGH)

Thanks.... I got it