Go Down

Topic: Latching an output for 2 ultrasonic sensors (Read 267 times) previous topic - next topic

JoeEason

Hi,

I am using two HC-SR04 sensors to detect the position of a ram and basically my aim was to use a couple of relays to switch the polarity and reverse the direction of the ram.

When the first sensor is triggered by the head of the ram it should reverse direction until it hits the second sensor and reverse again. However as far as I can tell the sensor resets as soon as the ram moves away from it and so does the output which triggers it to reverse. This causes the ram to just move back and forth into and out of the range of the first sensor.

My question is, is there a way to program a latch onto the reversing output so that it ignores the first sensor until it can be reset by the second sensor being triggered?

(first time asking a question here so I'm just pasting my code in here)

the delays and relays 1 and 2 are just for circuit protection so I don't trip anything out while reversing the polarity. The relays I would want to latch are 3 and 4. (please excuse any random misprints in the code, I have been fiddling with the circuit and code all day to get it working so there may be a random bracket/ peice of code out of place)

void loop() {
  // put your main code here, to run repeatedly:
 
digitalWrite(relayPin4, LOW);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);

  //clears trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

//sets trigPin to HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

//Reads echoPin input + returns travel time
duration = pulseIn(echoPin, HIGH);

//Calculates distance
distanceCm = duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distanceCm);
Serial.print(distanceCm);
Serial.print(" cm");

  //clears trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);

//sets trigPin to HIGH for 10 microseconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

//Reads echoPin input + returns travel time
duration2 = pulseIn(echoPin2, HIGH);

//Calculates distance
distanceCm2 = duration2*0.034/2;
Serial.print("Distance: ");
Serial.println(distanceCm2);
Serial.print(distanceCm2);
Serial.print(" cm");

//relay control section
if(distanceCm < 20)
{
  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);
  delay(500);
  digitalWrite(relayPin3, HIGH);
  digitalWrite(relayPin4, HIGH);
  delay(500);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);
}

if(relayPin3, HIGH){
  if(distanceCm2 < 20)
  {
    digitalWrite(relayPin1, HIGH);
    digitalWrite(relayPin2, HIGH);
    delay(500);
    digitalWrite(relayPin3, LOW);
    digitalWrite(relayPin4, LOW);
    delay(500);
    digitalWrite(relayPin1, LOW);
    digitalWrite(relayPin2, LOW);
  }
}
else{
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);
  digitalWrite(relayPin3, LOW);
  digitalWrite(relayPin4, LOW);
}

Paul_KD7HB

This is not intended to be a criticism of the way you write programs, but I wonder how you got this far without testing each function?

Paul

JoeEason

Well I got it to a point where it was working but it only reversed and went the full way if you covered the first sensor the whole time it travelled back to the second sensor, so I was testing as much as I could... If my programming is a bit scruffy, like I said I've been messing with both my programming and my circuit to try and get the second half to work how I want it to but it just wasn't happening. I might have pasted a scuffed version so I apologise. I figured you'd get the gist of what I was doing and might have a suggestion

Paul_KD7HB

Well I got it to a point where it was working but it only reversed and went the full way if you covered the first sensor the whole time it travelled back to the second sensor, so I was testing as much as I could... If my programming is a bit scruffy, like I said I've been messing with both my programming and my circuit to try and get the second half to work how I want it to but it just wasn't happening. I might have pasted a scuffed version so I apologise. I figured you'd get the gist of what I was doing and might have a suggestion
That give absolutely no gist.

Are you only triggering the appropriate sensor when the movement sensing is needed?

Paul

Idahowalker

yea, scruffy code. *grin*

Quote
When the first sensor is triggered by the head of the ram it should reverse direction until it hits the second sensor and reverse again. However as far as I can tell the sensor resets as soon as the ram moves away from it and so does the output which triggers it to reverse. This causes the ram to just move back and forth into and out of the range of the first sensor.
So the ram comes out, hits the first sensor, and reverses, till it hits the 2nd sensor is how you want it to run?

Well when the ram hits the first sensor, the logic should be that the first sensor code is no longer active till the 2nd sensor has been triggered.

bool RamIn = false

first sensor reads ram position, RamIn goes true to disable ram out code till enabled by the ram in code.

JoeEason

Ah,

I had been trying to use an output to disable one of the sensors, but for obvious reasons that didn't work. I don't know why I didn't think of using a line with boolean. Thanks for that, I'll try and give that a run through tomorrow and tidy up some of the scruff.... :P

Cheers for the help

Idahowalker

Ah,

I had been trying to use an output to disable one of the sensors, but for obvious reasons that didn't work. I don't know why I didn't think of using a line with boolean. Thanks for that, I'll try and give that a run through tomorrow and tidy up some of the scruff.... :P

Cheers for the help
You would want to consider something in case the ram does not hit a sensor. Something like each iteration of the loop increments an int. Each sensor hit, resets the int counter to 0. After a certain number of passes, of the loop, to be determined by you, if the int hits a certain value then time to send the ram to a home position. You can take it further with another int counter that if, after a certain number of home sending attempts and this value is not set to zero, then stop all operations till attended.

Go Up