Close

Particle code

A project log for Wireless automated homecage two-bottle choice test

A wireless adaptation of https://hackaday.io/project/158279-automated-mouse-homecage-two-bottle-choice-test

lex-kravitzLex Kravitz 06/30/2018 at 21:540 Comments

The code is also a lot simpler than the original design, as this is an IoT device.  That means that it doesn't need to keep track of the time (the internet knows this), or log data (we'll use Blynk for real-time visualization and logging).  So really all the code needs to do is monitor 2 photointerrupters and count how long they are broken.  

Here's my first code!

It works and is probably good enough, but I may try to implement sleep on the Photon to save battery.  Right now I'm not doing that because I don't want to lose data while it's re-connecting to WIFI after sleep, and I can only get the Photon to wake on 1 pin, not 2.  So I could only have it wake on one sipper.  If anyone has insight into how to implement sleep in this project please drop me a line!

/******************************************************************************
Blynk virtual pins:
V0 = Battery voltage
V1 = Battery altert
V2 = left licks
V3 = right licks
V4 = total licks
V5 = left binary (1 if it's active)
V6 = right binary (1 if it's active)
V7 = Left - right (difference in seconds)

Original Creation Date: June 29, 2018

This code is released under the MIT license.
Distributed as-is; no warranty is given.
******************************************************************************/

#include <blynk.h>
#include <SparkFunMAX17043.h>

double voltage = 0; // Variable to keep track of LiPo voltage
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
bool alert; // Variable to keep track of whether alert has been triggered
char auth[] = "BLYNK TOKEN HERE";  //Blynk token (get this from the app!)
int leftPin = 2;
int rightPin = 3;
int left=0;
int right=0;
int leftbinary=0;
int rightbinary=0;
int total=0;

void setup()
{
    Serial.begin(9600); // Start serial, to output debug data
    lipo.begin(); // Initialize the MAX17043 LiPo fuel gauge
    lipo.quickStart();
    lipo.setThreshold(20); // Set alert threshold to 20%.
    Blynk.begin(auth);
    delay (500); //allow device to settle
}

void loop()
{
    if (digitalRead(leftPin)==LOW){
        leftbinary=1;
        delay (650);  //this delay slows incrementing to 1 count per second
        left++;  //increment leftlicks by 1
        total++;
    }

    if (digitalRead(rightPin)==LOW){
        rightbinary=1;
        delay (650);    //this delay slows incrementing to 1 count per second 
        right++;  //increment leftlicks by 1
        total++;
    }
    
    voltage = lipo.getVoltage();
    soc = lipo.getSOC();
    alert = lipo.getAlert();
    
    //Write data to Blynk
    Blynk.virtualWrite(V0, voltage);
    Blynk.virtualWrite(V1, alert);
    Blynk.virtualWrite(V2, left);
    Blynk.virtualWrite(V3, right);
    Blynk.virtualWrite(V4, total);
    Blynk.virtualWrite(V5, leftbinary);
    Blynk.virtualWrite(V6, rightbinary);
    Blynk.virtualWrite(V7, left-right);
    
    Blynk.run();
    delay(200);
    leftbinary=0;
    rightbinary=0;
}

Discussions