Jesse LawlerJared LawlerCale Cozad
Published

Trickster Halloween Candy box

This is not your traditional Halloween candy box!

IntermediateProtip193
Trickster Halloween Candy box

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2
Argon
Particle Argon
×3
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
button
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×2
Resistor 1k ohm
Resistor 1k ohm
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Particle Pi
Particle Pi

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Drill / Driver, 20V
Drill / Driver, 20V

Story

Read more

Schematics

Ultrasonic with button

Servo Motor

Code

Ultrasonic code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <HC_SR04.h>

// Sensor gateway using the Argon board. This contains the HC-SR04
// Ultrasonic code and sends data to the other nodes to notify of a trip
int button = A1;
int led = D7;
int inputPin = D2;              // choose the input pin (for PIR sensor)
int reading = 0;
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

int calibrateTime = 5000;

double cm = 0.0;
bool beam_status = false;

int trigPin = D4;
int echoPin = D5;
unsigned long myChannelNumber = 1579110;
const char * myWriteAPIKey = "O6Q7O42THSM75XQN";

HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);

void setup() 

{
    Spark.variable("cm", &cm, DOUBLE);
    pinMode(button,INPUT);
    pinMode(led,OUTPUT);
    pinMode(inputPin, INPUT);
}

void myHandler(const char *event, const char *data) {

}

void loop() 
{
    cm = rangefinder.getDistanceCM();
    if (cm<30){
        if (beam_status==false){
            
            Particle.publish("tripStatus", "breach");
            beam_status = true;   
        }
    } else {
        if (beam_status==false){
            
        } else {
            Particle.publish("tripStatus", "all_clear");
            beam_status = false;
        }
{
    int reading = digitalRead(button);
    if (reading == LOW){
        digitalWrite(led,LOW);
        
    }
    if (reading == HIGH){
        digitalWrite(led,HIGH);
    
        Particle.publish("buttonStatus", "button_on");
        String data = String(cm);
        
    }
    }
     const char * eventName = "tripStatus";
        unsigned long myChannelNumber =1579020;
        const char * myWriteAPIKey = "O6Q7O42THSM75XQN";
        Particle.publish(eventName, "{ \"Centimeters\": \"" + String(cm) + "\", \"key\": \"" + myWriteAPIKey + "\" }", PRIVATE, NO_ACK);  
   
delay(2000);
}
}

Servo Motor Code

C/C++
Servo myservo;  // create servo object to control a servo

int pos = 120;    // variable to store the servo position

void setup()
{
    Particle.subscribe("buttonStatus", myHandler); // subscribing to the published event from the phototransistor photon
    Particle.subscribe("tripStatus", myHandler);
    myservo.attach(D2);   // attach the servo on the D0 pin to the servo object
    pinMode(D7, OUTPUT);
}

void myHandler(const char *event, const char *data)
{ 
    if(strcmp(data,"button_on")==0) {  // this will move the servo to the on position
        digitalWrite(D7, HIGH);
        myservo.write(180);
        digitalWrite(D7, LOW);
        
    }
    else if(strcmp(data,"breach")==0){     // this will move the servo to the off position
        myservo.write(120);// move servo to 0
        
    }
}

void loop()
{
}

Credits

Jesse Lawler

Jesse Lawler

1 project • 0 followers
Jared Lawler

Jared Lawler

0 projects • 0 followers
Cale Cozad

Cale Cozad

0 projects • 0 followers

Comments