- volatile long dispulser = 0; // Make these volatile since they are modified in ISR
- volatile long dispulsel = 0;
- unsigned long lastTriggerTime = 0;
- const unsigned long triggerInterval = 20; // Interval to trigger new measurement in ms
- void ultra_sonics_nonblocking(int trigPin, int echoPin, long &result) {
- unsigned long now = millis();
- if (now - lastTriggerTime >= triggerInterval) {
- // Trigger ultrasonic pulse
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- lastTriggerTime = now;
- }
- long duration = pulseIn(echoPin, HIGH, 26000);
- if (duration > 0) {
- result = duration * 0.034 / 2;
- }
- }
- void isrRight() {
- dispulser++;
- }
- void isrLeft() {
- dispulsel++;
- }
- void setup() {
- // youe existing code
- }
- void loop() {
- ultra_sonics_nonblocking(trig1, echo1, leftsensor);
- ultra_sonics_nonblocking(trig2, echo2, rightsensor);
- ultra_sonics_nonblocking(trig3, echo3, midsensor);
- ultra_sonics_nonblocking(trig4, echo4, right2sensor);
- ultra_sonics_nonblocking(trig5, echo5, left2sensor);
- // Rest of code
- }
Recent Pastes