Generating an Interrupt from HC-SR04 ultrasonic sensor

Hey guys,

I am creating a conveyor belt powered by a stepper motor. The assembly line has an ultrasonic sensor that turns off the conveyor belt when detects incoming object. Once the object is taken away by a claw, the conveyor belt turns back on. The TRIG and ECHO pins of the sensor is connected to 2 and 3 respectively to generate interrupts through the following. However, when I upload, the stepper motor is unresponsive and ultrasensor doesnt detect anything.

attachInterrupt(1,turnStepperOff,RISING);
attachInterrupt(0,turnStepperOn,FALLING);

I have attached my code below for anyone to have a look and tell me where is the error.

Thanks

interrupt.ino (1.07 KB)

//Initializing the stepper motor
#include <Stepper.h>
const int stepsPerRevolution = 50;
long duration, distance;
Stepper myStepper(stepsPerRevolution,8,9,10,11);
//Initializing the ultrasonic sensor
#define trigPin 2
#define echoPin 3
//#define led 13

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myStepper.setSpeed(100);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// pinMode(led, OUTPUT);
attachInterrupt(1,turnStepperOff,RISING);
attachInterrupt(1,turnStepperOn,FALLING);

}

void loop() {
// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
myStepper.step(stepsPerRevolution);
// digitalWrite(led,HIGH);
delay(10);
}

void turnStepperOff()
{
myStepper.step(0);
//digitalWrite(led,LOW);
delay(1000);

}

void turnStepperOn()
{
myStepper.step(stepsPerRevolution);
//digitalWrite(led,HIGH);
}

The TRIG and ECHO pins of the sensor is connected to 2 and 3 respectively to generate interrupts through the following.

The trigger pin is an output pin. It will NEVER receive an interrupt.

You can NOT use delay() in an interrupt service routine. The fact that you try indicates that you do not comprehend interrupts, and should not be using them.

If the echo pin DID generate an interrupt, you have NO idea how far the object is, since you have no idea how long it has been since you triggered the outgoing pulse (except that you know you have NOT triggered an outgoing pulse).

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What else is your code doing that you need an interrupt, why not just poll the sensor on each program loop to see if a product is in range?

Tom… :slight_smile:

PaulS:
The trigger pin is an output pin. It will NEVER receive an interrupt.

You can NOT use delay() in an interrupt service routine. The fact that you try indicates that you do not comprehend interrupts, and should not be using them.

Just because I don't understand interrupts doesn't stop me from learning it. Jeez

To help you learn. Jeez.

xerxesbear:
Just because I don't understand interrupts doesn't stop me from learning it. Jeez

You want to learn something and Paul is trying to teach you something.

Sounds like a good combination to me 8)

There is no need to use interrupts in this application, so avoid them.