Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <IRremote.h>
  3. #define trigPin 13
  4. #define echoPin 12
  5. #define led 7
  6. #define led2 8
  7. #define led3 4
  8. Servo myservo;
  9. Servo myservo1;
  10. Servo myservo2;
  11. int pos = 0;
  12. int IR_Recv = 3; //IR Receiver Pin 3
  13. IRrecv irrecv(IR_Recv);
  14. decode_results results;
  15.  
  16. void setup() {
  17. Serial.begin (9600);
  18. pinMode(trigPin, OUTPUT);
  19. pinMode(echoPin, INPUT);
  20. pinMode(led, OUTPUT);
  21. pinMode(led2, OUTPUT);
  22. pinMode(led3, OUTPUT);
  23. myservo.attach(9);
  24. myservo1.attach(10);
  25. myservo2.attach(11);
  26. irrecv.enableIRIn(); // Starts the receiver
  27. }
  28.  
  29. void loop() {
  30. if (irrecv.decode(&results)){
  31. long int decCode = results.value;
  32. long duration, distance;
  33. Serial.println(decCode);
  34. if( results.value == 16753245)
  35. irrecv.resume();
  36. digitalWrite(led3,LOW);
  37. digitalWrite(trigPin, LOW); // Added this line
  38. delayMicroseconds(2); // Added this line
  39. digitalWrite(trigPin, HIGH);
  40. // delayMicroseconds(1000); - Removed this line
  41. delayMicroseconds(10); // Added this line
  42. digitalWrite(trigPin, LOW);
  43. duration = pulseIn(echoPin, HIGH);
  44. distance = (duration/2) / 29.1;
  45. if (distance < 20) { // This is where the LED On/Off happens
  46. digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  47. digitalWrite(led2,LOW);
  48. for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
  49. { // in steps of 1 degree
  50. myservo.write(pos); // tell servo to go to position in variable 'pos'
  51. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  52. myservo2.write(pos); // tell servo to go to position in variable 'pos'
  53. delay(5); // waits 15ms for the servo to reach the position
  54. }
  55. for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
  56. {
  57. myservo.write(pos); // tell servo to go to position in variable 'pos'
  58. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  59. myservo2.write(pos); // tell servo to go to position in variable 'pos'
  60. delay(5);
  61. }
  62. }
  63. else {
  64. digitalWrite(led,LOW);
  65. digitalWrite(led2,HIGH);
  66. for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 180 degrees
  67. { // in steps of 1 degree
  68. myservo.write(pos); // tell servo to go to position in variable 'pos'
  69. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  70. myservo2.write(pos); // tell servo to go to position in variable 'pos'
  71. delay(5); // waits 15ms for the servo to reach the position
  72. }
  73. for(pos = 20; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
  74. {
  75. myservo.write(pos); // tell servo to go to position in variable 'pos'
  76. myservo1.write(pos); // tell servo to go to position in variable 'pos'
  77. myservo2.write(pos); // tell servo to go to position in variable 'pos'
  78. delay(5);
  79. }
  80. }
  81. if (distance >= 200 || distance <= 0){
  82. Serial.println("Out of range");
  83. }
  84. else {
  85. Serial.print(distance);
  86. Serial.println(" cm");
  87. }
  88. delay(300);
  89. }
  90. else digitalWrite(led3,HIGH);
  91. if( results.value == 16736925)
  92. digitalWrite(led3,HIGH);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement