Coding Help. Trying to get Sensor to trigger different modes of activating mosfet?

Hello all. I’m finally all set up. I have all my parts I need for my first Arduino project! Right now, I’m dealing with an Arduino Uno, an HCSR04 Ultrasonic sensor and a mosfet and TI H-Bridge.

First off I’m trying to get my to Arduino to detect an object within two set distances using the same sensor. When an object is within a certain distance I am hoping to get it to trigger a both an H-bridge and a mosfet, but with different activation patterns when an object is between two different distances. Let me explain:

If an object is within 3-4 meters of the Sensor, I would like for the arduino to leave on the mosfet until the object is out of the path.

If an object proceeds to be within 1-2.9 meters of the sensor, I would like for the arduino to trigger the mosfet and h-bridge to turn off and on every 200 milliseconds until that object is out of the path or moved back to 3-4 meters away.

I’m not sure how to correspond all of these devices together using code, or how to set the distances to correspond. Is there anyone who can help? It would be greatly appreciated! Thank you!

Start by writing a program that measures the distance and turns on a LED for each condition.

Then move to the next stage.

Weedpharma

weedpharma: Start by writing a program that measures the distance and turns on a LED for each condition.

Then move to the next stage.

Weedpharma

thanks man! Do you know where I can find a pre-written example of this or where I can find a reference on how to write such a program? Thank you!

Do a search in the box at top of page for ultrasonic distance measuring. You should get lots of basics to write a simple program. Once you have the reading sorted and sending distances to the serial terminal, put in

If distance > min and < max
Turn on LED
Else
turn off LED

When you have that working, move to next stage.

Make sure you keep a copy of any working programs as you will need to go back a stage when the the new part of the program gets totally confused!

Weedpharma

This might help you out a bit...

https://www.youtube.com/watch?v=js4TK0U848I