Problem using sequential loop

So im creating a program with two separate ultrasonic sensors, each is supposed to make a separate LED blink faster and faster the closer an object comes. My problem lies within the loop function. Due to the loop running sequentially (At least I think this is the problem) the LED's are blinking in accordance to the furthest object on one of the ultrasonic sensors. Im not sure how to fix this. I have my entire code listed below. This is probably something obvious Im missing - excuse my noob-ness

//#include "DHT.h";


#define DHTTYPE DHT22   // DHT Type is DHT 22 (AM2302)
#define DHTPIN 7       // DHT-22 Output Pin connection
#define trigPin 5
#define echoPin 6
#define beepPin 12

#define trigPin2 8
#define echoPin2 9
#define beepPin2 10
#define scalingfactor 10


int hum = 25;    // Stores humidity value in percent
int temp = 25;   // Stores temperature value in Celcius
float soundsp;  // Stores calculated speed of sound in M/S
float soundum;  // Stores calculated speed of sound in cm/ms

//DHT dht(DHTPIN, DHTTYPE);  // Initialize DHT sensor for normal 16mhz Arduino

int centi = 0;
int centi2 = 0;
unsigned long duration = 0;
unsigned long duration2 = 0;
unsigned long maxtime2 = 2000;
unsigned long maxtime = 2000;
unsigned long i = 1;
unsigned long y = 1;
unsigned long time2 = 0;
unsigned long time1 = 0;
unsigned long time3 = 0;
unsigned long time4 = 0;
void setup() {
  Serial.begin (9600);
 //dht.begin();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(beepPin, OUTPUT);

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(beepPin2, OUTPUT);
}
/*
void temps(){
  hum = dht.readHumidity();  // Get Humidity value
  temp= dht.readTemperature();  // Get Temperature value
}
*/
void calculate(){
      // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);

    // Convert to cm/us
    soundum = soundsp / 10000;
}

void logdistance() {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  centi = (duration* soundum)/2;
  maxtime = scalingfactor * centi;
}
void delayandcheckdistance() {
  i = 1;
  while (1) {
    if (i % 60 == 0) {
      logdistance();
    }
    delay(1);
    ++i;
    if (i >= maxtime) {
      break;
    }
  }
}


void beep() {
  digitalWrite(beepPin, HIGH);
  delay(60);
 logdistance();
  if (time2 - time1 < 30) {
    delay(60 - (time2 - time1));
   }
  digitalWrite(beepPin, LOW);
}







void logdistance2() {
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  centi2 = (duration2* soundum)/2;
  maxtime2 = scalingfactor * centi2;
}

void delayandcheckdistance2() {
  y = 1;
  while (1) {
    if (y % 60 == 0) {
      logdistance2();
    }
    delay(1);
    ++y;
    if (y >= maxtime2) {
      break;
    }
  }
}
void beep2() {
  digitalWrite(beepPin2, HIGH);
  delay(60);
  logdistance2();
  if (time4 - time3 < 30) {
    delay(60 - (time4 - time3));
  }
  digitalWrite(beepPin2, LOW);
}


void loop() {
  beep();
  delayandcheckdistance();

  beep2();
  delayandcheckdistance2();
  calculate();

  //print results
  Serial.print("c. ");
  Serial.print(centi);

   Serial.print("     ");
  Serial.print("||");
  Serial.print("     ");


  Serial.print(" ");
  Serial.print("c. ");
  Serial.print(centi2);

  
  Serial.println(" ");
  Serial.print (" ");
  Serial.println(" ");

   }

there's a lot of redundant code (copy/paste) that is prone to errors

why do you call your logdistance routines both in you delayandcheckdistance and beep routines?

do you need to have a sufficient delay between measurement of either the same or different ultrasonic emitters to allow any residual audio energy to dissipate?

It's hard to tell what your code is supposed to do because there are NO COMMENTS!

I also don't understand what you're asking. Are you saying that only one sensor is working? Which one?

Use the "BlinkWithoutDelay" example to blink your LEDs. Then you are not spending time in delay() and the two blinkers can blink individually.

Note: It's a good idea to add a third argument to pulseIn() to set a timeout in microseconds. The default is 1 second so your sketch would pause for a second if an echo is not seen (too far away or too close). 30000 is a good timeout since it is somewhat over the 5-meter maximum range of the HD-SR04.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.