I'm using 2 ultrasonic sensors to reverse the direction of a ram using a couple of relays to reverse the polarity.

Basically when the head of the ram comes within range of one sensor it switches the polarity and should travel back until it's within range of the second sensor.

However as far as I can see, as soon as the ram moves outside the range of the first sensor its distance resets and the ram then moves back and forth to just inside and outside the range of the sensor.

Is there a way to program the first output to latch and reset when the second sensor is triggered? Or would I have to build an external latching circuit to achieve this?

  • \$\begingroup\$ Assuming your title indirectly indicates your reading the sensor outputs with the Arduino, i recommend asking this question on stackoverflow. It is easier/ cheaper / etc to program than adding a flipflop electronically. \$\endgroup\$ – Huisman Feb 12 at 18:01
  • \$\begingroup\$ This would be pretty easy to implement in a state machine. \$\endgroup\$ – Ron Beyer Feb 12 at 18:04
  • \$\begingroup\$ Please move this over to the Arduino thread, and provide the code you are currently using, as well as wiring diagrams. There is not enough information here. \$\endgroup\$ – HavocRC Feb 12 at 18:16

You need to keep track of your direction with a variable.

Pseudo code.

void setup{
  dir = 0;                    //Default move left.

void loop{
  if (!dir && leftSense) {    //Moving left and left sensor turns on ...
    dir = 1;                  //... move right
  if (dir && rightSense) {    //Moving right and right sensor turns on
    dir = 0;                  //... move left
  // Other stuff
  • \$\begingroup\$ Copy'n'paste to be exact! The curse of convenience. Thank you. \$\endgroup\$ – Transistor Feb 12 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.