This is the first part of the Holloween project – the Spooky Scary Mask. There is a sonic detector to monitor how close people are, when people are close enough (about 20cm), the sound effect will go off and the “blood ball” will be squeeze , and the mask will spin randomly as if the ghost is giggling and laughing at the spooked person.
How the project would work is that there will be a motor connected to the blood splashing mask that makes it turn and spin. The solinoids will be actuated and squeeze the ball. There is also a sonic detector to detect how close people are from the setup.
Here is what I have for the first part of the project. Since I already familierized myself with motor and solinoids in the previous projects, the main focus on this part is about the soinc sensor and the sound.
Here is the sketch and the code for the setup:
Code:
//JeanZhang_yihanz //The Bloody Scary Mask - Part 1 //This code contains code and resources from this webpage: //http://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/ //Set up the pins const int trigPin = 11; //Trig - green Jumper - digital const int echoPin = 12; //Echo - yellow Jumper - pwm const int soundPin = 5; long duration, cm, inches; //This is the function for a spooky sound (doesn't have to be exactly like this) void scarySound() { for (int i = 100; i < 1000 ; i+= 20) { Serial.println(i); tone(soundPin, i); delay(50); } for (int i = 1000; i > 100; i-=20) { Serial.println(i); tone(soundPin, i); delay(50); } noTone(soundPin); // stop the sound after the trigger } void setup() { //Serial Port begin Serial.begin (9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(soundPin, OUTPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); // convert the time into a distance cm = (duration/2) / 29.1; inches = (duration/2) / 74; Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); //if the person is close enough, things will be triggered if (cm < 20){ scarySound(); } delay(250); }
Reflection:
Even though this seems really easy, it took me quite a long time to figure out that several libraries for the sonic detector is not compatible with the tone() function for arduino. For this paticular problem, even though there is not really any problem with the code, the IDE would not complie. And shows error is like this:
Tone.cpp.o (symbol from plugin): In function `timer0_pin_port': (.text+0x0): multiple definition of `__vector_7' libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit status
I spent quite sometime online to see if there is anything to do to fix this problem, which I failed to do. At the end, I have to switch to another way to make the sensor work (without using any library).