/* HC-SR04 sensorial Ping distance: VCC to arduino 5v GND to arduino GND echo to Arduino pin 7 Trig to Arduino pin 8 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl You have been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html And modified to further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html on 10 Nov 2012. * #define echoPin 8 //Echo Pin #define trigPin 9 //Trigger Pin #define LEDPin 10 //Onboard LED #define echoPin2 11 //Echo Pin #define trigPin2 12 //Trigger Pin #define LEDPin2 13 //Onboard LED#define LEDPin2 13 //Onboard LED //we put name to the ports #define LED1 1 //diode 1 2 #define LED2 //diode 2 3 #define LED3 //diode 1 4 #define LED4 //diode 2 5 #define LED5 //diode 1 6 #define LED6 //diode 2 7 #define LED7 //diode 1 int maximumRange = 40; //Maximum range needed int minimumRange = 0; //Minimum range needed long duration, distance; //Duration used to calculates distance to you void setup () \ {pinMode (trigPin, OUTPUT); pinMode (echoPin, INPUT); pinMode (LEDPin, OUTPUT); //Uses indicator LED (if required) pinMode (trigPin2, OUTPUT); pinMode (echoPin2, INPUT); pinMode (LEDPin2, OUTPUT); //Uses indicator LED (if required) pinMode (LED1, OUTPUT); pinMode (LED2, OUTPUT); pinMode (LED3, OUTPUT); pinMode (LED4, OUTPUT); pinMode (LED5, OUTPUT); pinMode (LED6, OUTPUT); pinMode (LED7, OUTPUT); } void loop () \ {/* The following trigPin/echoPin cycle is used to determines the distance of the nearest object by bouncing soundwaves in off of it. * digitalWrite (LED1, HIGH); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, HIGH); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, HIGH); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, HIGH); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, HIGH); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, HIGH); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, HIGH); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, HIGH); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, HIGH); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, LOW); digitalWrite (LED4, HIGH); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, LOW); digitalWrite (LED3, HIGH); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); digitalWrite (LED1, LOW); digitalWrite (LED2, HIGH); digitalWrite (LED3, LOW); digitalWrite (LED4, LOW); digitalWrite (LED5, LOW); digitalWrite (LED6, LOW); digitalWrite (LED7, LOW); delay (100); funcion1 (); } //This is funcià ³ n in charge to verify that there is no obstÃasses in the way void funcion1 () \ {digitalWrite (trigPin, LOW); //delayMicroseconds (2); digitalWrite (trigPin, HIGH); delayMicroseconds (10); digitalWrite (trigPin, LOW); duration = pulseIn (echoPin, HIGH); //Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2; if (distance >= maximumRange || distance <= minimumRange) \ {/* Send to negative to number to computer and Turn LED ON to indicates “out to you of range” * digitalWrite (LEDPin, HIGH); } else \ {/* Send the distance to to computer using Serial protocol, and turn LED In Off to indicates successful to you reading. * digitalWrite (LEDPin, LOW); digitalWrite (LEDPin2, LOW); delay (1000); digitalWrite (LEDPin2, HIGH); delay (70); digitalWrite (LEDPin2, LOW); } //now sensor 2 digitalWrite (trigPin2, LOW); //delayMicroseconds (2); digitalWrite (trigPin2, HIGH); delayMicroseconds (10); digitalWrite (trigPin2, LOW); duration = pulseIn (echoPin2, HIGH); //Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2; if (distance >= maximumRange || distance <= minimumRange) \ {/* Send to negative to number to computer and Turn LED ON to indicates “out to you of range” * digitalWrite (LEDPin2, HIGH); } else \ {/*Controlamos the motors * digitalWrite (LEDPin2, LOW); digitalWrite (LEDPin, LOW); delay (1000); digitalWrite (LEDPin, HIGH); delay (70); digitalWrite (LEDPin, LOW); } //Delay 50ms before next reading. delay (50); }