CPP 245
Arduino code By arvand on 12th September 2023 01:43:57 PM
  1. volatile long dispulser = 0;  // Make these volatile since they are modified in ISR
  2. volatile long dispulsel = 0;
  3.  
  4. unsigned long lastTriggerTime = 0;
  5. const unsigned long triggerInterval = 20;  // Interval to trigger new measurement in ms
  6.  
  7. void ultra_sonics_nonblocking(int trigPin, int echoPin, long &result) {
  8.   unsigned long now = millis();
  9.   if (now - lastTriggerTime >= triggerInterval) {
  10.     // Trigger ultrasonic pulse
  11.     digitalWrite(trigPin, LOW);
  12.     delayMicroseconds(2);
  13.     digitalWrite(trigPin, HIGH);
  14.     delayMicroseconds(10);
  15.     digitalWrite(trigPin, LOW);
  16.     lastTriggerTime = now;
  17.   }
  18.  
  19.   long duration = pulseIn(echoPin, HIGH, 26000);
  20.   if (duration > 0) {
  21.     result = duration * 0.034 / 2;
  22.   }
  23. }
  24.  
  25. void isrRight() {
  26.   dispulser++;
  27. }
  28.  
  29. void isrLeft() {
  30.   dispulsel++;
  31. }
  32.  
  33. void setup() {
  34.   // youe existing code
  35. }
  36.  
  37. void loop() {
  38.   ultra_sonics_nonblocking(trig1, echo1, leftsensor);
  39.   ultra_sonics_nonblocking(trig2, echo2, rightsensor);
  40.   ultra_sonics_nonblocking(trig3, echo3, midsensor);
  41.   ultra_sonics_nonblocking(trig4, echo4, right2sensor);
  42.   ultra_sonics_nonblocking(trig5, echo5, left2sensor);
  43.  
  44.   // Rest of code
  45. }

Hightechrobo bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.