Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremote.h>
- const int RECV_PIN = 11;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
- const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
- const int motorA = 12;
- const int motorB = 13;
- const int buzzer = 3;
- long distanceThreshold = 0;
- long cm = 0;
- long readUltrasonicDistance(int triggerPin, int echoPin)
- {
- pinMode(triggerPin, OUTPUT); // Clear the trigger
- digitalWrite(triggerPin, LOW);
- delayMicroseconds(2);
- // Sets the trigger pin to HIGH state for 10 microseconds
- digitalWrite(triggerPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(triggerPin, LOW);
- pinMode(echoPin, INPUT);
- // Reads the echo pin, and returns the sound wave travel time in microseconds
- return pulseIn(echoPin, HIGH);
- }
- void setup(){
- Serial.begin(9600);
- irrecv.enableIRIn();
- irrecv.blink13(true);
- pinMode(motorA, OUTPUT);
- pinMode(motorB, OUTPUT);
- pinMode(buzzer, OUTPUT);
- //pinMode(pingPin, OUTPUT);
- //pinMode(echoPin, INPUT);
- }
- void loop(){
- if (irrecv.decode(&results)){
- Serial.println(results.value, HEX);
- switch(results.value){
- case 0xFD08F7: //"you pressed button 1"
- for(int i = 1; i>0; i--){
- digitalWrite(motorB, HIGH);
- delay(4000);
- digitalWrite(motorB, LOW);
- delay(3000);
- }
- break;
- case 0xFD8877: //"you pressed button 2"
- for(int i = 2; i>0; i--){
- digitalWrite(motorB, HIGH);
- delay(4000);
- digitalWrite(motorB, LOW);
- delay(3000);
- }
- break;
- case 0xFD48B7: //"you pressed button 3"
- for(int i = 3; i>0; i--){
- digitalWrite(motorB, HIGH);
- delay(4000);
- digitalWrite(motorB, LOW);
- delay(3000);
- }
- break;
- case 0xFD28D7: //"you pressed button 4"
- for(int i = 4; i>0; i--){
- digitalWrite(motorB, HIGH);
- delay(4000);
- digitalWrite(motorB, LOW);
- delay(3000);
- }
- break;
- default: ;
- }
- irrecv.resume();
- }
- // set threshold distance to activate the buzzer
- distanceThreshold = 50; //
- // measure the ping time in cm
- cm = 0.01723 * readUltrasonicDistance(7, 6);
- // convert to inches by dividing by 2.54
- if (cm <= distanceThreshold) {
- digitalWrite(motorA, HIGH);
- digitalWrite(buzzer, HIGH);
- } else {
- digitalWrite(motorA, LOW);
- digitalWrite(buzzer, LOW);
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement