Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Halloween Scareomatic
- learnelectronics
- 7 AUG 2017
- www.youtube.com/c/learnelectronics
- */
- #include <NewPing.h> //library for SONAR sensor
- #define TRIGGER_PIN 11 //SONAR trigger pin on digital 11
- #define ECHO_PIN 10 //SONAR echo (return) pin on digital 10
- #define MAX_DISTANCE 200 //maximum range for SONAR sensor in cm
- #define leye 5 //left LED eye on digital 5 (a pwm pin)
- #define reye 6 //right LED eyeon digital 6 (a pwm pin)
- #define sound 12 //sound board trigger pin on digital 12
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //create instance of NewPing called sonar
- void setup() {
- Serial.begin(9600); //serial comms for debug
- pinMode(sound,OUTPUT); //set sound board for output
- pinMode(leye,OUTPUT); //set leye board for output
- pinMode(reye,OUTPUT); //set reye board for output
- digitalWrite(sound,LOW); //force all output pins low
- digitalWrite(leye,LOW);
- digitalWrite(reye,LOW);
- }
- void scareMe(){ //define a function called scareMe
- digitalWrite(leye,LOW); //set leye to 0
- digitalWrite(reye,LOW); //set reye to 0
- digitalWrite(sound,HIGH); //trigger sound board
- digitalWrite(sound,LOW); //stop trigering sound board
- digitalWrite(leye,HIGH); //flash both eyes 3 times full brightness
- digitalWrite(reye,HIGH);
- delay(200);
- digitalWrite(leye,LOW);
- digitalWrite(reye,LOW);
- delay(200);
- digitalWrite(leye,HIGH);
- digitalWrite(reye,HIGH);
- delay(200);
- digitalWrite(leye,LOW);
- digitalWrite(reye,LOW);
- delay(200);
- digitalWrite(leye,HIGH);
- digitalWrite(reye,HIGH);
- delay(200);
- digitalWrite(leye,LOW);
- digitalWrite(reye,LOW);
- delay(200);
- }
- void loop() {
- for(int n = 0;n <10; n++){ //ramp up brightness on eyes very dimm
- analogWrite(leye,n);
- analogWrite(reye,n);
- delay(50);
- }
- for(int n = 10;n >0; n--){ //ramp dowm brightness on eyes to off
- analogWrite(leye,n);
- analogWrite(reye,n);
- delay(50);
- }
- int x = (sonar.ping_cm()); //check SONAR
- if (x > 0 && x < 100){ //is someone less than foot away?
- scareMe(); //call scareMe function
- }
- Serial.println(sonar.ping_cm()); //print value to serial port for debug
- delay(1000); //wait one second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement