Advertisement
learnelectronics

Halloween Scareomatic

Aug 21st, 2017
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. /*
  2. Halloween Scareomatic
  3.  
  4. learnelectronics
  5. 7 AUG 2017
  6.  
  7. www.youtube.com/c/learnelectronics
  8. */
  9.  
  10. #include <NewPing.h>                                  //library for SONAR sensor
  11.  
  12. #define TRIGGER_PIN  11                               //SONAR trigger pin on digital 11
  13. #define ECHO_PIN     10                               //SONAR echo (return) pin on digital 10
  14. #define MAX_DISTANCE 200                              //maximum range for SONAR sensor in cm
  15. #define leye         5                                //left LED eye on digital 5 (a pwm pin)
  16. #define reye         6                                //right LED eyeon digital 6 (a pwm pin)
  17. #define sound        12                               //sound board trigger pin on digital 12
  18.  
  19. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);   //create instance of NewPing called sonar
  20.  
  21. void setup() {
  22.  
  23.   Serial.begin(9600);                                 //serial comms for debug
  24.   pinMode(sound,OUTPUT);                              //set sound board for output
  25.   pinMode(leye,OUTPUT);                               //set leye board for output
  26.   pinMode(reye,OUTPUT);                               //set reye board for output
  27.   digitalWrite(sound,LOW);                            //force all output pins low
  28.   digitalWrite(leye,LOW);
  29.   digitalWrite(reye,LOW);
  30.  
  31.  
  32. }
  33.  
  34. void scareMe(){                                       //define a function called scareMe
  35.   digitalWrite(leye,LOW);                             //set leye to 0
  36.   digitalWrite(reye,LOW);                             //set reye to 0
  37.   digitalWrite(sound,HIGH);                           //trigger sound board
  38.   digitalWrite(sound,LOW);                            //stop trigering sound board
  39.  
  40.     digitalWrite(leye,HIGH);                          //flash both eyes 3 times full brightness
  41.     digitalWrite(reye,HIGH);
  42.     delay(200);
  43.     digitalWrite(leye,LOW);
  44.     digitalWrite(reye,LOW);
  45.     delay(200);
  46.     digitalWrite(leye,HIGH);
  47.     digitalWrite(reye,HIGH);
  48.     delay(200);
  49.     digitalWrite(leye,LOW);
  50.     digitalWrite(reye,LOW);
  51.     delay(200);
  52.     digitalWrite(leye,HIGH);
  53.     digitalWrite(reye,HIGH);
  54.     delay(200);
  55.     digitalWrite(leye,LOW);
  56.     digitalWrite(reye,LOW);
  57.     delay(200);
  58.  
  59. }
  60.  
  61. void loop() {
  62.  
  63. for(int n = 0;n <10; n++){                            //ramp up brightness on eyes very dimm
  64.   analogWrite(leye,n);
  65.   analogWrite(reye,n);
  66.   delay(50);
  67. }
  68.  
  69. for(int n = 10;n >0; n--){                            //ramp dowm brightness on eyes to off
  70.   analogWrite(leye,n);
  71.   analogWrite(reye,n);
  72.   delay(50);
  73. }
  74.  
  75.  int x = (sonar.ping_cm());                           //check SONAR
  76.  if (x > 0 && x < 100){                               //is someone less than foot away?
  77.   scareMe();                                          //call scareMe function
  78.  }
  79.  Serial.println(sonar.ping_cm());                     //print value to serial port for debug
  80.  delay(1000);                                         //wait one second
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement