Skip to content

Instantly share code, notes, and snippets.

@MindstormFan
Created June 10, 2019 21:21
Show Gist options
  • Save MindstormFan/c7873d6e690a5970fb6cb3e15c1a3ed3 to your computer and use it in GitHub Desktop.
Save MindstormFan/c7873d6e690a5970fb6cb3e15c1a3ed3 to your computer and use it in GitHub Desktop.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include "SR04.h"
#include "pitches.h"
#include "IRremote.h"
#include <Servo.h>
#define TRIG_PIN 12
#define ECHO_PIN 11
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
int buzzer = 7;//the pin of the passive buzzer
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
a=sr04.Distance();
// Serial.print(a);
// Serial.println("cm");
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
if(a < 30) {
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer,LOW);
}
}
void translateIR() { // takes action based on IR code received
switch(results.value) {
case 0xFFE01F: //DOWN
Serial.println(" DOWN ");
for (pos = 25; pos <= 135; 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'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 0xFF906F: //UP
Serial.println(" UP ");
for (pos = 135; pos >= 25; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
default:
Serial.println(" other button ");
}// End Case
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment