#include "HttpClient.h"
//Initialize HTTP Client and variables
HttpClient http;
http_request_t request;
http_response_t response;
//Default HTTP Header settings
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ NULL, NULL }
};
//Initializes the hall effect sensor variable and tilt sensor variable
int hallPin = D2;
int tiltPin = D3;
//Records the state of the hall effect sensor and tilt sensor
int tiltState = 0;
int hallState = 0;
//Tracks dumbbell position rep counting, activation, and deactivation
int dumbbellStatus = 0;
int dumbbellPosition = 0;
//Tracks reps and sets
int repCounter = 0;
int setCounter = 0;
void setup ()
{
//Set the hall effect and tilt sensors as input
pinMode (hallPin, INPUT);
pinMode (tiltPin, INPUT);
//HTTP rrequest setup/configuration. No changes should be made
request.hostname = "nomu.co";
request.port = 80;
request.body = NULL;
//request.path = "/lab/photon-gym-mirror/command.php?event=checkin";
}
void loop ()
{
monitorRFID();
monitorReps();
}
/*The monitorRFID function handles the activation and deactivation
of the device (via a hall effect sensor)*/
void monitorRFID()
{
//Reads the status of the hall effect sensor
hallState = digitalRead (hallPin);
//If a magnet is present and the dumbbell isn't already activated...
if (hallState == LOW && dumbbellStatus == 0)
{
//Send a checkin event to the magic mirror
request.path = "/lab/photon-gym-mirror/command.php?event=checkin";
http.get(request, response, headers);
//Delay for five seconds to prevent overlap
delay (5000);
//Send a set start event to the magic mirror
request.path = "/lab/photon-gym-mirror/command.php?event=set-start";
http.get(request, response, headers);
//Note that the dumbbell is currently activated
dumbbellStatus = 1;
}
//Reads the status of the hall effect sensor
hallState = digitalRead (hallPin);
//If the magnet is present and the dumbbell is already activated
if (hallState == LOW && dumbbellStatus == 1)
{
//Send a checkout event to the magic mirror
request.path = "/lab/photon-gym-mirror/command.php?event=checkout";
http.get(request, response, headers);
// Delay five seconds to prevent overlap
delay (5000);
//Note that the dumbbell is currently inactivated
dumbbellStatus = 0;
}
}
/*The monitorReps function handles the rep-counting feature of the
connected dumbbells (via a tilt sensor)*/
void monitorReps()
{
//Read the status of the tilt sensor
tiltState = digitalRead (tiltPin);
//If the dumbbells are activated...
if (dumbbellStatus == 1)
{
//And if the tilt sensor has been activated
if (dumbbellPosition == 0 && tiltState == HIGH)
{
//Set the dumbbell position
dumbbellPosition = 1;
//Increment the rep counter for later use
repCounter++;
//Send a rep increment event to the magic mirror
request.path = "/lab/photon-gym-mirror/command.php?event=reps-increm ent";
http.get(request, response, headers);
//Delay for one second to prevent overlap
delay(1000);
}
//If the tilt sensor is deactivated
else if (dumbbellPosition == 1 && tiltState == LOW)
{
//Set the dumbbell position
dumbbellPosition = 0;
//Delay for one second to prevent overlap
delay(1000);
}
//Once the rep count reaches five...
if (repCounter == 5)
{
//Send a set end event to the magic mirror
request.path = "/lab/photon-gym-mirror/command.php?event=set-end";
http.get(request, response, headers);
//Set the rep count to zero
repCounter = 0;
//Increment the set count
setCounter++;
//Delay for seven seconds to prevent overlap
delay(7000);
}
//If the set count is equal to or greater than one...
if (setCounter >= 1)
{
//Send a set start event to the magic mirror (indicating a new set)
request.path = "/lab/photon-gym-mirror/command.php?event=set-start";
http.get(request, response, headers);
//Decrement the set count
setCounter--;
}
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .