After making my first obstical ovoidence robot with the HC-SR04, I decided to get a few other shields to play with. Picked up a seeed relay sheild and was able to get it hooked up with a old Christmas santa and get it to blink. Now I'm trying to intergrate the ultrasonic board with it...
The more little projects I make, the more I understand the programming side of it. I'm kinda stuck on this one. This is where I'm at but kinda confused about the loop process of it. All i want it to do is trigger the relay when you get close. ( just turn the light on ) I'm really trying to grasp the concept of what im doing so maybe alittle help? I really want to understand it. Can't really figure out how to do it without delays.. If you take that off it just goes crazy.
Any help?
int pingPin = 3; // Out trig int echoPin = 4; // In echo int MotorControl = 5;
void setup() {
pinMode(MotorControl, OUTPUT); // (motorcontol) I was trying it with a motor first, now it's a light.
}
void loop() {
// establish variables for duration of the ping, // and the distance result in inches and centimeters: // long duration, inches, cm; long duration, inches, cm;
// The HC-SR04 is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the PING))), there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf // We'll use the same info for the HC-SR04 return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. // We'll use the same info for the HC-SR04 return microseconds / 29 / 2; }
In your setup() you should define any pins that are going to be OUPUTs for the duration of your program. You correctly called out the MotorControl pin as OUTPUT, but, you failed to set the pingPin as OUTPUT. And, as Max pointed out, you never trigger the US sensor. Your program should not work as written.
On a different note, why not use available libraries to make your life easier? The NewPing lib will make using one or more US sensors very easy, and, it is part of learning to program. At somepoint you may make a lib that you want to be able to use across multiple projects. Knowing how to make use of the lib here will make it easier to use a lib you make across multiple projects.
If you want to learn to write your program with non blocking delays, look at the BlinkWithoutDelay example under Digital in the arduino IDE.
OK I redid everything to make it more simple… After looking through other projects this is what I got. I understand what its doing alot better now. The best way I learn ( being a noob to this ) is to just keep creating simple projects over and over again. This one is my first not going off any tutorial or using really any examples. What I made “works” but when its set to low, the relay clicks… as its just going through the delay. Is there any way to fix that? I understand it needs to be in a loop to be able to work. Right?
Thanks!
#define trigPin 3 #define echoPin 4 int Switch = 5;
I dont know if this will help…but in my own adventure to coding and robot building…I wrote a little code to turn a led on and of using the sensor…the same one you have…in the hopes it could be use to avoid stuff…
#include <NewPing.h>
#define LED_PIN 4 #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. boolean trig = false;
voidsetup(){ Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
voidloop(){ delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsignedint uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(“cm”); if(trig == true){
Engaged();}
{ delay(50); unsignedint uS = sonar.ping(); unsignedint distance = uS / US_ROUNDTRIP_CM;
Any code I modified was commented out and then either moved or just left out. While C/C++/arduino don’t care much about whitespace, Making your code “pretty” does help readability. I did, however, completely delete an extraneous pair of braces you had in your code. It is the open brace ‘{’ just after Engaged();} and then one ‘}’ of the 3 at the very end of your code.
Hopefully the comments I added are helpful. If I am being too pushy, let me know. At the very least I am glad to see you using braces with if statements when you only have a single statement inside of them.
When I was rewriting your program, I originally deleted the trig = false; line. After thinking about it, I realized that not having it there would mean after the first close call the robot wouldn’t know it had moved away.