Question

For Arduino (e.g. Mega 2560) how do you program two interrupts for HC-SR04 ultrasound sensors? For...

For Arduino (e.g. Mega 2560) how do you program two interrupts for HC-SR04 ultrasound sensors? For example an automated 2 wheel car with a ultrasound sound sensor on the left and right hand side, when it detects an obstacle it triggers an interrupt and changes motor direction.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You can simply use condition statements once you know the distance of an obstacle.Following code can be helpful in determining distance of an object using HC-SR04 ultrasonic sensors-

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}
Add a comment
Know the answer?
Add Answer to:
For Arduino (e.g. Mega 2560) how do you program two interrupts for HC-SR04 ultrasound sensors? For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT