• Black Vimeo Icon

©2020 BY JENNY LIN

Trick or Treat Wonder Box Presented

Updated: Nov 12, 2019

After two weeks of developing, Yunze and I finally finished our Trick or Treat Wonder Box in time!!



I realized I did not really think about how to hide the cords and arduino until it was clearly exposed in the wonder box, so I just used black tape to tape and hide all of it. Yet, both Yunze and I agreed that we could have put what we wanna present on an upper level and hide all the cords and arduino on the bottom level.







In addition, we encountered problems with our door servo and the UV LEDs since we used delay for the code for the door servo to stay open a certain amount of time. In that case, the UV LED would not light up. After looking up on the internet and tried and failed to incorporate the internal clock concept into our code so that we would not need to use delay, my buddy, Barak helped us understood the fundamental logic of it and taught us the basic codes for that.


Moreover, same as our prototype, how to mount the servo onto the door tightly to smoothly control the door was a problem. Eventually, Yunze took paper clip, bend it and tick it into the door through the wing attached to the servo, and it made a more steady mechanism. We also thought if we had chosen a wooden door, then we could have easily screwed the servo onto the door.



Before the final touches


Tick or Treat! Happy Halloween!




Codes


Arduino 1 - candy machine, tomb, and ghost

#include <Servo.h> Servo candyMachine; Servo tomb; Servo ghost;

const int trigPin = 9; const int echoPin = 10; long duration; int distance; int servoPinC = 3; int servoPinT = 6; int servoPinG = 11; int button; int ServoAngleCan = 0; int ServoAngleTomb = 90; int ServoSpeedGhost = 90; boolean state = false; int LEDPin = 2;

void setup() { Serial.begin(9600); pinMode(LEDPin, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(13, INPUT); candyMachine.attach(servoPinC); tomb.attach(servoPinT); ghost.attach(servoPinG); tomb.write(89); candyMachine.write(0); ghost.write(ServoSpeedGhost);

}

void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration / 2) / 29.1; Serial.print(distance); Serial.print(","); button = digitalRead(13);

if (button == 1) { state = true; } if (state) { if (random(2) == 0) { // treat Serial.println("0"); //Serial.print(","); ServoAngleTomb = 180; ServoAngleCan = 60; tomb.write(ServoAngleTomb); delay(3000); candyMachine.write(ServoAngleCan); delay(4000); state = false;

} else { //trick Serial.println("1"); //Serial.print(","); ServoAngleCan = 0; //no candy ServoAngleTomb = 0; // tomb turn ServoSpeedGhost = 10; // ghost move tomb.write(ServoAngleTomb); candyMachine.write(ServoAngleCan); ghost.write(ServoSpeedGhost); delay(7000); state = false; } } else { // nothing happens Serial.println("2"); ServoAngleCan = 0; //no candy ServoAngleTomb = 89; ServoSpeedGhost = 90; tomb.write(ServoAngleTomb); candyMachine.write(ServoAngleCan); ghost.write(ServoSpeedGhost); digitalWrite(LEDPin, HIGH); delay(100); digitalWrite(LEDPin, LOW); delay(100); } }


Arduino 2 - door and LEDs

#include <Servo.h>

Servo myServo;

const int servoPin = 3;

const int knockSensor = A0; // the piezo is connected to analog pin 0

const int doorOpen = 10; // threshold value to decide when the detected sound is a knock or not

int sensorReading = 0; // variable to store the value read from the sensor pin

//boolean shouldOpen = false;

int servoAngle = 0;

const int LEDPin1 = 13;

const int LEDPin2 = 7;

const int LEDPin3 = 12;

const int LEDPin4 = 11;

const int LEDPin5 = 10;


int pressDelay = 10000;

int lastPress = 0;

bool isOpen = false;


void setup() {

myServo.attach(servoPin);

pinMode(LEDPin1, OUTPUT);

pinMode(LEDPin2, OUTPUT);

pinMode(LEDPin3, OUTPUT);

pinMode(LEDPin4, OUTPUT);

pinMode(LEDPin5, OUTPUT);

Serial.begin(9600); // use the serial port

myServo.write(servoAngle);

}


void loop() {

sensorReading = analogRead(knockSensor);

unsigned long time = millis();

Serial.print(lastPress);

Serial.print(",");

Serial.println(time);

digitalWrite(LEDPin1, HIGH);

digitalWrite(LEDPin2, HIGH);

digitalWrite(LEDPin3, LOW);

digitalWrite(LEDPin4, LOW);

digitalWrite(LEDPin5, HIGH);

delay(100);

digitalWrite(LEDPin1, LOW);

digitalWrite(LEDPin2, LOW);

digitalWrite(LEDPin3, HIGH);

digitalWrite(LEDPin4, HIGH);

digitalWrite(LEDPin5, LOW);

delay(100);

if (sensorReading > doorOpen && lastPress < (time - pressDelay)) {

lastPress = time;

isOpen = true;

// Serial.println(lastPress);


onOpen();

}


if (isOpen && time > (lastPress + pressDelay)) {

isOpen = false;


onClose();

}

}


void onOpen() {

Serial.println("ON OPEN");

servoAngle = 90;

myServo.write(servoAngle);

}


void onClose() {

Serial.println("ON CLOSE");

servoAngle = 0;

myServo.write(servoAngle);

}



p5js - door creaking sound and trick or treat music

let door;

let door2;

let trick;

let treat;

let serial;

let distance = "waiting for data";

let state = "waiting for data";

let doorCanPlay = false;

let stateCanPlay = false;


function preload() {

door = loadSound('door.mp3');

door2 = loadSound('door2.mp3');

treat = loadSound('treat.mp3');

trick = loadSound('trick.mp3');

}


function setup() {

createCanvas(400, 400);

//trick.loop();


// Instantiate our SerialPort object

serial = new p5.SerialPort();

serial.open("/dev/cu.usbmodem1421");

serial.on('data', gotData);

// setInterval(function() {

// console.log("HELLO");

// serial.write(1);

// }, 1000);



}



function draw() {

if (distance > 30) {

if (doorCanPlay) {

door.play();

doorCanPlay = false;

}

} else {

doorCanPlay = true;

}

if (distance < 30) {

door.pause();

}

print(distance);


if (state == 0) {

if (stateCanPlay) {

treat.loop();

stateCanPlay = false;

}

} else if (state == 1) {

if (stateCanPlay) {

trick.loop();

stateCanPlay = false;

}

} else if (state == 2) {

stateCanPlay = true;

treat.pause();

trick.pause();

}



console.log(state)

}


function gotData() {

let currentString = serial.readStringUntil("\r\n"); // read the incoming string

trim(currentString); // remove any trailing whitespace

if (!currentString) return; // if the string is empty, do no more

//console.log(currentString); // println the string

var sensorReadings = split(currentString, ",");

if (sensorReadings.length > 1) {

distance = int(sensorReadings[0]); // save it for the draw method

state = int(sensorReadings[1]);

}

}

#ITP #PComp #PhysicalComputing #TrickOrTreat #Halloween #wonderBox