Advertisement
Guest User

modified program

a guest
Nov 21st, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <NewPing.h>
  2.  
  3. #define LED_PIN    4
  4. #define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  5. #define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
  6. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters).
  7. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  8. boolean trig = false;
  9.  
  10. void setup() {
  11.   Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  12.   pinMode(TRIGGER_PIN, OUTPUT); // don't forget to set all of your OUTPUT pins
  13.   pinMode(LED_PIN,OUTPUT);
  14.   digitalWrite(LED_PIN, LOW); // unless you are on a MEGA(?) pin 4 is likely to be digital only, no PWM. Without PWM you will have no analogWrite
  15. //  analogWrite(LED_PIN,0);
  16.   delay(1000);
  17. }
  18.  
  19. void Engaged() {
  20.   digitalWrite(LED_PIN,HIGH);
  21.   Serial.println("This Hurt!!!");
  22. }
  23.  
  24. void loop() {
  25.   delay(50);                        // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  26.   unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  27.   unsigned int distance = uS / US_ROUNDTRIP_CM;
  28.   Serial.print("Ping: ");
  29. //  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  30.   Serial.print(distance);
  31.   Serial.println("cm");
  32. //  if(trig == true) {
  33. //    Engaged();
  34. //  }
  35. //  delay(50);
  36. //  unsigned int uS = sonar.ping();
  37. //  unsigned int distance = uS / US_ROUNDTRIP_CM;
  38.   if(distance<10) {
  39.     trig = true;
  40.   } else {
  41. //  delay(50);
  42. //  if(distance>10) {
  43.     trig = false;
  44.     digitalWrite(LED_PIN,LOW);
  45.   }
  46.   if(trig == true) { // moved to respond after the distance has been checked
  47.     Engaged();
  48.   }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement