Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Arielle RafolsNICOLAS PALOMARESVeronica Sykes
Published © MIT

Photon Motion Sensor

We measured the level of activity within our school’s counseling department by using a range sensor at the department's entrance.

BeginnerShowcase (no instructions)Over 1 day1,676
Photon Motion Sensor

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Photon
Particle Photon
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Particle

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)

Story

Read more

Custom parts and enclosures

Customized Enclosure

Adobe Illustrator file of enclosure.

Customized Enclosure

Schematics

Fritzing Diagram by Sven Huijbrechts

Code

Code

Arduino
Written with the help of Mr. Solin and Mr. Law, this code enables us to use the sensor and Phant database.
// This #include statement was automatically added by the Particle IDE.
#include <SparkFunPhant.h>

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


/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/

#define trigPin 1
#define echoPin 0
#define led 7
#define led2 3



double value = 0; 



const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "4JRoXdyKrEhzE2N8mdA4"; // Phant public key - ALREADY CHANGED
const char privateKey[] = "b5Ee2vm7KkSdezJrBv8m"; // Phant private key  - ALREADY CHANGED
Phant phant(server, publicKey, privateKey); // Create a Phant object

const int POST_RATE = 3000; // Time between posts, in ms.
unsigned long lastPost = 0; // global variable to keep track of last post time



void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 90) {  // This is where the LED On/Off happens  
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  digitalWrite(led2,LOW);
}
  else {
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
  if (distance >= 200 || distance <= 0){  
    Serial.println("Out of range");
    
    value = 0;    //added this line
    
  
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
    
    value = distance;
    //distance = value;
    postToPhant();          //Added this line from below
    
  }
  delay(500);
  
  
  
   //value++;   


    /*Replace the if statement below with whatever your trigger is
    boolean trigger = false;                                                    //Moved above
    if (trigger)
    {
        postToPhant();
    }
    */
}

int postToPhant()
{    
    // Use phant.add(<field>, <value>) to add data to each field.
    // Phant requires you to update each and every field before posting,
    // make sure all fields defined in the stream are added here.
    phant.add("motion", value);
    //phant.add("number2", value2);
        	
    TCPClient client;
    char response[512];
    int i = 0;
    int retVal = 0;
    
    if (client.connect(server, 80)) // Connect to the server
    {
		// Post message to indicate connect success
        Serial.println("Posting!"); 
		
		// phant.post() will return a string formatted as an HTTP POST.
		// It'll include all of the field/data values we added before.
		// Use client.print() to send that string to the server.
        client.print(phant.post());
        delay(1000);
		// Now we'll do some simple checking to see what (if any) response
		// the server gives us.
        while (client.available())
        {
            char c = client.read();
            Serial.print(c);	// Print the response for debugging help.
            if (i < 512)
                response[i++] = c; // Add character to response string
        }
		// Search the response string for "200 OK", if that's found the post
		// succeeded.
        if (strstr(response, "200 OK"))
        {
            Serial.println("Post success!");
            retVal = 1;
        }
        else if (strstr(response, "400 Bad Request"))
        {	// "400 Bad Request" means the Phant POST was formatted incorrectly.
			// This most commonly ocurrs because a field is either missing,
			// duplicated, or misspelled.
            Serial.println("Bad request");
            retVal = -1;
        }
        else
        {
			// Otherwise we got a response we weren't looking for.
            retVal = -2;
        }
    }
    else
    {	// If the connection failed, print a message:
        Serial.println("connection failed");
        retVal = -3;
    }
    client.stop();	// Close the connection to server.
    return retVal;	// Return error (or success) code.
    
    
    
}

Credits

Arielle Rafols
-1 projects • 0 followers
NICOLAS PALOMARES
-1 projects • 1 follower
Veronica Sykes
-1 projects • 1 follower

Comments