HC-SR04 not interrupting main code. Help.

Hi everyone. I am trying to make a robot with a HC-SR04 sensor. I want to be able to interrupt whatever is happening when the sensor falls below a certain value. I cannot constantly have a piece of code in the main loop that delays other things to constantly check the sensor.

What I want is something that keeps checking/updating the sensor in the background. But does not affect the main loop. Then i will need some kind of interrupt that I can use the value of the sensor to interrupt the code when it falls below a certain threshold.

Any tips/tutorials on this?

Thanks, David.

Does the NewPing library fit this requirement?

If not, when you send the trigger pulse, attach an external interrupt to the echo, and start a timeout. If the timeout happens first, detach the interrupt.

Thanks! I will try that once I get some time.

david97: Hi everyone. I am trying to make a robot with a HC-SR04 sensor. I want to be able to interrupt whatever is happening when the sensor falls below a certain value. I cannot constantly have a piece of code in the main loop that delays other things to constantly check the sensor.

Have a look at the BlinkWithoutDelay sketch in the IDE at File>Examples>02.Digital>BlinkWithoutDelay. There's no need for interupts, unless you're talking about microseconds.

Ok guys. It worked well. But now I am having trouble with the arduino not able to use one of the motors and I don’t know why… Here is a very rough/untidy sketch of what I’m trying to do. This one basically turns on motors M1 and M2 and turns them off when something is in range. Very poor sketch but just a short thing to check if everything is working. Can anyone explain why M2 will not work? It’s not a hardware issue.

#include <Servo.h>
#include <NewPing.h>

Servo servo;

//Maximum motor voltage output
int Max = 153;

//Define sensor parameters
#define TRIGGER_PIN 7
#define ECHO_PIN 8
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// define motor outputs
#define M1a 6
#define M1b 9
#define M2a 10
#define M2b 11

//servo posistion
int pos = 90;

void setup() {
  Serial.begin(9600);
  
  servo.attach(12);  //attach  servo to pin 12
  
  //Declare motor pins as outputs
  pinMode(M1a, OUTPUT);
  pinMode(M1b, OUTPUT);
  pinMode(M2a, OUTPUT);
  pinMode(M2b, OUTPUT);
  //pull motor pins low
  digitalWrite(M1a , LOW);
  digitalWrite(M1b , LOW);
  digitalWrite(M2a , LOW);
  digitalWrite(M2b , LOW);
  
}

void loop() {
  //check if object is closer than 15cm & stop
  if(sonar.ping_cm() < 15) {
    Stop();
  }
  //check if objeck is futher than 14cm and go
  else if(sonar.ping_cm() > 14) {
    Foward();
  }

}

void Foward() {
  //set direction to foward
  analogWrite(M1a, Max);
  analogWrite(M1b, 0);
  analogWrite(M2a, Max);
  analogWrite(M2b, 0);
    
}
 void Stop() {
   //stop
  analogWrite(M1a, 0);
  analogWrite(M1b, 0);
  analogWrite(M2a, 0);
  analogWrite(M2b, 0);
    
 }

Can you explain what this

Can anyone explain why M2 will not work?

means?

david97: It's not a hardware issue.

I do believe (though I actually have no experience in this area) that it is in fact, a hardware issue. :D

PWM involves use of the timers. There are only, IIRC, 4 timers and one is used for millis(). You cannot have 4 PWMs.

In this respect, your code is wrong. You have (attempted to) set up four PWMs. You only have 2 motors; you only need two PWMs; one per motor.

(And I anticipate the next question, but will see who catches it first!) :D

Well worth a closer look here

AWOL:
Well worth a closer look here

Yes, well, that in addition!

So looks like this is my main problem

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

Also the use of too many timers.

Looks like I will need to upgrade to the arduino mega eventually. I have alot of other stuff I want to add to it later all using timers and PWM. For now I will try shifting to other pins and see if I still get problems.

Thanks guys. Still new to this stuff so i'm trying not to be to annoying.

david97: Looks like I will need to upgrade to the Arduino mega eventually. I have a lot of other stuff I want to add to it later all using timers and PWM. For now I will try shifting to other pins and see if I still get problems.

OK, so no-one has so far commented on the "H-bridge" trick here.

You only need two PWMs to control two motors in forward and reverse. For each motor, you have a PWM on one terminal (through the H-Bridge driver) and a "plain" digital output on the other. To drive the motor forward, you set a PWM on the first terminal and switch the second low. To drive it in reverse, you switch the second terminal high and set a PWM on the first of the complement of the value you need so that the "off" time becomes the "on" and vice versa.