Hello. I'm doing a school project and I'm new to Arduino boards and those related things. My goal was to create a device using my Arduino Mega 2560 board, with an ultrasonic sensor and a coin vibrating motor attached. It would alert the user of objects in their surroundings, by sending different vibrations through the motor. However, none of it seems to be working. The sensor isn't sending signals, and the motor isn't vibrating. I'd also like to mention that I am using the NewPing library. Here's what I have:
Arduino Mega 2560 board
HC-SR04 ultrasonic sensor
- VCC plugged into 5v
- Trig plugged into 27
- Echo plugged into 28
- GND plugged into GND
3v coin vibrating motor (two wires)
- red (+) plugged into A8
- blue (-) plugged into GND
It doesn't show any errors in my code, that's why I'm confused as to it not working. There's no need for harsh remarks. It was my understanding that the TX light would be flashing if it were sending signals.
No, hence my confusion. There were no results of it sending or receiving anything. But, there were no errors in the code. Can I block you, you aren't of help in any way and are just be obnoxious. Really don't you have anything better to do?
I´ll consider that your wiring is ok, since you provided no schematics or photos.
There are some issues in you code that you should take a look:
21 duration = sonar.ping_cm(); //this line already gives you a distance in cm
22 distance = ((duration / 2) * 0.0343); // which makes this line nonsense
27 if (distance > 500){ //this will never happen, since you defined MAX_DISTANCE 500
PS: Ultrassonic sensor returns zero if object is out of range and it will not reach 500cm.
Useful tip is to divide your projects in small steps: learn to use HC-SR04 only; learn to use coin vibrating motor only; then put both together. This is why @anon73444976 is suggesting you to begin with the examples of NewPing library.
You understand that because your code compiles and links with no errors indicated by the IDE does not mean it will work as you intended right? You could also have a wiring issue, etc.
Use a sample program to test each device independently to ensure the hardware is working correctly. Then, and only then, attempt to debug your entire program. This is the way I debug every time I start a new project and I have been developing embedded applications for over 30 years.