Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <IRremote.h>
- #define trigPin 13
- #define echoPin 12
- #define led 7
- #define led2 8
- #define led3 4
- Servo myservo;
- Servo myservo1;
- Servo myservo2;
- int pos = 0;
- int IR_Recv = 3; //IR Receiver Pin 3
- IRrecv irrecv(IR_Recv);
- decode_results results;
- void setup() {
- Serial.begin (9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(led, OUTPUT);
- pinMode(led2, OUTPUT);
- pinMode(led3, OUTPUT);
- myservo.attach(9);
- myservo1.attach(10);
- myservo2.attach(11);
- irrecv.enableIRIn(); // Starts the receiver
- }
- void loop() {
- if (irrecv.decode(&results)){
- long int decCode = results.value;
- long duration, distance;
- Serial.println(decCode);
- if( results.value == 16753245)
- irrecv.resume();
- digitalWrite(led3,LOW);
- digitalWrite(trigPin, LOW); // Added this line
- delayMicroseconds(2); // Added this line
- digitalWrite(trigPin, HIGH);
- // delayMicroseconds(1000); - Removed this line
- delayMicroseconds(10); // Added this line
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration/2) / 29.1;
- if (distance < 20) { // This is where the LED On/Off happens
- digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
- digitalWrite(led2,LOW);
- for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
- { // in steps of 1 degree
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- myservo2.write(pos); // tell servo to go to position in variable 'pos'
- delay(5); // waits 15ms for the servo to reach the position
- }
- for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
- {
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- myservo2.write(pos); // tell servo to go to position in variable 'pos'
- delay(5);
- }
- }
- else {
- digitalWrite(led,LOW);
- digitalWrite(led2,HIGH);
- for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 180 degrees
- { // in steps of 1 degree
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- myservo2.write(pos); // tell servo to go to position in variable 'pos'
- delay(5); // waits 15ms for the servo to reach the position
- }
- for(pos = 20; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
- {
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- myservo2.write(pos); // tell servo to go to position in variable 'pos'
- delay(5);
- }
- }
- if (distance >= 200 || distance <= 0){
- Serial.println("Out of range");
- }
- else {
- Serial.print(distance);
- Serial.println(" cm");
- }
- delay(300);
- }
- else digitalWrite(led3,HIGH);
- if( results.value == 16736925)
- digitalWrite(led3,HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement