Advertisement
kerelius

Arduino

Nov 19th, 2021
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <IRremote.h>
  2.  
  3. const int RECV_PIN = 11;
  4. IRrecv irrecv(RECV_PIN);
  5. decode_results results;
  6.  
  7. const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
  8. const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
  9.  
  10. const int motorA = 12;
  11. const int motorB = 13;
  12. const int buzzer = 3;
  13.  
  14. long distanceThreshold = 0;
  15. long cm = 0;
  16.  
  17. long readUltrasonicDistance(int triggerPin, int echoPin)
  18. {
  19.   pinMode(triggerPin, OUTPUT);  // Clear the trigger
  20.   digitalWrite(triggerPin, LOW);
  21.   delayMicroseconds(2);
  22.   // Sets the trigger pin to HIGH state for 10 microseconds
  23.   digitalWrite(triggerPin, HIGH);
  24.   delayMicroseconds(10);
  25.   digitalWrite(triggerPin, LOW);
  26.   pinMode(echoPin, INPUT);
  27.   // Reads the echo pin, and returns the sound wave travel time in microseconds
  28.   return pulseIn(echoPin, HIGH);
  29. }
  30.  
  31. void setup(){
  32.   Serial.begin(9600);
  33.   irrecv.enableIRIn();
  34.   irrecv.blink13(true);
  35.  
  36.   pinMode(motorA, OUTPUT);
  37.   pinMode(motorB, OUTPUT);
  38.   pinMode(buzzer, OUTPUT);
  39.  
  40.   //pinMode(pingPin, OUTPUT);
  41.   //pinMode(echoPin, INPUT);
  42. }
  43.  
  44. void loop(){
  45.    
  46.   if (irrecv.decode(&results)){
  47.     Serial.println(results.value, HEX);
  48.    
  49.     switch(results.value){
  50.       case 0xFD08F7: //"you pressed button 1"
  51.       for(int i = 1; i>0; i--){
  52.         digitalWrite(motorB, HIGH);
  53.         delay(4000);
  54.         digitalWrite(motorB, LOW);
  55.         delay(3000);
  56.       }
  57.       break;
  58.       case 0xFD8877: //"you pressed button 2"
  59.       for(int i = 2; i>0; i--){
  60.         digitalWrite(motorB, HIGH);
  61.         delay(4000);
  62.         digitalWrite(motorB, LOW);
  63.         delay(3000);
  64.       }
  65.       break;
  66.       case 0xFD48B7: //"you pressed button 3"
  67.       for(int i = 3; i>0; i--){
  68.         digitalWrite(motorB, HIGH);
  69.         delay(4000);
  70.         digitalWrite(motorB, LOW);
  71.         delay(3000);
  72.       }
  73.       break;
  74.       case 0xFD28D7: //"you pressed button 4"
  75.       for(int i = 4; i>0; i--){
  76.         digitalWrite(motorB, HIGH);
  77.         delay(4000);
  78.         digitalWrite(motorB, LOW);
  79.         delay(3000);
  80.       }
  81.       break;
  82.       default: ;
  83.     }
  84.     irrecv.resume();
  85.   }
  86.  
  87.   // set threshold distance to activate the buzzer
  88.   distanceThreshold = 50; //
  89.   // measure the ping time in cm
  90.   cm = 0.01723 * readUltrasonicDistance(7, 6);
  91.   // convert to inches by dividing by 2.54
  92.  
  93.   if (cm <= distanceThreshold) {
  94.     digitalWrite(motorA, HIGH);
  95.     digitalWrite(buzzer, HIGH);
  96.   } else {
  97.     digitalWrite(motorA, LOW);
  98.     digitalWrite(buzzer, LOW);
  99.   }
  100.  
  101.   delay(100);
  102.  
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement