Noah Jillson
Published

Particle Argon IoT Bike Accident Detection

A bike accident detector that emails my parents if I I get in an accident.

BeginnerFull instructions provided221
Particle Argon IoT Bike Accident Detection

Things used in this project

Hardware components

Argon
Particle Argon
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×5
Resistor 220 ohm
Resistor 220 ohm
×1
4000mAh Portable Battery (Out: 5V/1A)
×1
Aethox Tilt Switch
×1
Elegoo Shock Switch Sensor
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
IFTTT Particle

Story

Read more

Schematics

Schematics.jpeg

Code

BikeAccidentDetector

C/C++
//Sensor Pins
const int shockSensor = D2;
const int tiltSwitch = D6;

//Debounce Values in milliseconds
const int shockDebounceBuffer = 10000;
const int tiltErrorBuffer = 200;

//Values used for debouncing that hold the time in milliseconds since the program began
unsigned long timeWhenShockDetected;
unsigned long timeOfTiltStateChange;

//Values that hold a boolean value of either TRUE or FALSE depending on the 1 or 0 read from their repsective digital sensors
bool shockState;
bool tiltState;

//Holds True if a shock has been detected for the duration of the shockDebounceBuffer
bool shockDetected;

//Holds the tiltState value from the previous iteration of loop()
bool prevTiltState;

//Is equal to 1 if a crash has been detected
int crashDetected;


void setup() {
    Particle.variable("crashDetected", crashDetected);//Makes the crashDetected variable a Particle Variable and detectable by IFTTT
    
    pinMode(shockSensor, INPUT); //Sets the pin connected to the shock sensor to recieve INPUT
    pinMode(tiltSwitch, INPUT_PULLUP); //Sets the pin connected to the tilt sensor to recieve INPUT
    
    Serial.begin(9600); //Begins the serial monitor at a baud rate of 9600 bits per second
}

void loop() {
    /* 
    reads the values of the sensors connected to the shock sensor pin and
    the tilt switch pin and sets the booleans equal to their respective
    values
    */
    shockState = digitalRead(shockSensor);
    tiltState = digitalRead(tiltSwitch);
    
    /*
    Evaluates if the current state of the tilt sensor is different than 
    the previos state and
    records the time in milliseconds when this is true. This records the 
    time that a change in state 
    has occured
    */
    if(tiltState != prevTiltState)
    {
        timeOfTiltStateChange = millis();
    }
    
    /* 
    Detects if a shock has occured and sets the value of the shockDetectd
    to true and records the time the shock occurs.
    */
    if(!shockState)
    {
        shockDetected = true;
        timeWhenShockDetected = millis();
    }
    
    //Tests if a shock has occured regardless of loop instance, 
    if(shockDetected)
    {
        /*
        If statement ensures that the code within it only runs during a
        certain period of time after a shock has been detected. The
        interval of time is controlled by the shockDebounceBuffer
        variable.
        */
        if(millis() - timeWhenShockDetected <= shockDebounceBuffer)
        {
            /*
            Checks if a tilt has been detected. tiltState reads false
            when the sensor is tilted.
            */
            if(!tiltState)
            {
                /*
                This if statement checks to see if the time since the
                last state changes is long enough to rule out errors and
                momentary tilts. This makes sure that a tilt value is
                significant by only running the code within if the
                duration of the tilt is equal to or surpasses the
                duration defined to errors and insignificant tilts.
                */
                if(millis() - timeOfTiltStateChange >= tiltErrorBuffer)
                {
                    /*
                    Sets the Particle.Variable() to 1 allowing for
                    interaction with IFTTT and for an email to be sent
                    */
                    crashDetected = 1;
                    
                    /*
                    Ensures that only one crash is detected per shock by
                    setting the shockDetected value to false. 
                    This value can only be set to true if another shock
                    is detected
                    */
                    shockDetected = false;
                }
            }
        }
        /*
        Sets shockDetected to false and stops the process of checking
        for a tilt value from the tilt switch
        */
        else
        {
            shockDetected = false;
        }
    }
    
    //records the previous tilt state
    prevTiltState = tiltState;
}

Credits

Noah Jillson

Noah Jillson

3 projects • 4 followers

Comments

Add projectSign up / Login