Back to Parent

Connected Barbell Code
#include "HttpClient.h" //Include HTTPClient Library

int i=0;
int reps=0;
int numOfReadings=45;
int div1=22;
int div2=22;
double averageDistance1;
double averageDistance2;
int readings[45];
int echoPin=D2;
int initPin=D3;
unsigned long pulseTime=0;
unsigned long distance=0;
unsigned long total1=0;
unsigned long total2=0;
int hallPin=D0;
int hallRead;
int blueLed = D7;
int arrayIndex = 0;

int threshold=5;
int delayTime=50;

//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 }
};

void setup()
{
  pinMode(initPin , OUTPUT);
  pinMode(echoPin,INPUT);
  pinMode(hallPin,INPUT);
  pinMode(blueLed, OUTPUT);

  Serial.begin(9600);

  //Initialize the readings array
  for (int thisReading = 0; thisReading < numOfReadings; thisReading++)
  {
    readings[thisReading] = 0;
  }

    //HTTP rrequest setup/configuration. No changes should be made
    request.hostname = "nomu.co";
    request.port = 80;
    request.body = NULL;

}

void loop()
{
  Login1(); //Check for hall effect reading

  Repetitory();

  //logsoneout();

  delay(100);
}

void Login1()
{
  hallRead= digitalRead(hallPin);

  if (hallRead==LOW)// checks user in
  {
    Serial.println("checkin");
    i++;
    //Particle.publish("reg");
    request.path = "/lab/photon-gym-mirror/command.php?event=checkin";
    http.get(request, response, headers);
    delay(3000);
    request.path = "/lab/photon-gym-mirror/command.php?event=set-start";
    http.get(request, response, headers);
    delay(1000);
  }
}

void Repetitory()
{
  while((i%2)==1)
  {
    digitalWrite(initPin, HIGH);                    // send 10 microsecond pulse
    delayMicroseconds(10);                          // wait 10 microseconds before turning off
    digitalWrite(initPin, LOW);                     // stop sending the pulse
    pulseTime = pulseIn(echoPin, HIGH);             // Look for a return pulse, it should be high as the pulse goes low-high-low
    distance = pulseTime/58;                        // Distance = pulse time / 58 to convert to cm.

    if (arrayIndex>0 and arrayIndex<23)
    {
      total1 = total1 - readings[arrayIndex]; // subtract the last distance
      readings[arrayIndex] = distance;        // add distance reading to array
      total1 = total1 + readings[arrayIndex];  // add the reading to the total
    }
    if (arrayIndex>=23 and arrayIndex<=45)
    {
      total2 = total2 - readings[arrayIndex];           // subtract the last distance
      readings[arrayIndex] = distance;                // add distance reading to array
      total2 = total2 + readings[arrayIndex];
    }

    arrayIndex++;
    if(arrayIndex>=45)
    {
      arrayIndex=0;
    }


    digitalWrite(blueLed, HIGH);
    delay(delayTime);
    digitalWrite(blueLed, LOW);

    //Serial.println(distance);
    //Serial.println(averageDistance1);
    //Serial.println(averageDistance2);
    //Serial.println("New Reading");

    averageDistance1 = total1 / div1;      //calculate the average distance over 0.5 seconds                      // Distance = pulse time / 58 to convert to cm.
    averageDistance2 = total2 / div2;      //calculate average distance over second 0.5 seconds

    if (averageDistance1 > averageDistance2 + threshold && arrayIndex == 44)// checks for change in direction=1 rep
    {
      reps++;

      request.path = "/lab/photon-gym-mirror/command.php?event=reps-increment";
      http.get(request, response, headers);

      //Troubleshooting
      //Serial.println("Rep Complete");
      //Serial.println(averageDistance1);
      //Serial.println(averageDistance2);

      if(reps>=5)
      {
        reps=0;
        delay(1000);
        request.path = "/lab/photon-gym-mirror/command.php?event=set-end";
        http.get(request, response, headers);
        delay(3000);
        request.path = "/lab/photon-gym-mirror/command.php?event=set-start";
        http.get(request, response, headers);
        delay(1000);
      }
    }
    logsoneout();
    //delay(50);
  }
}

void logsoneout()
{
  if (digitalRead(hallPin)==LOW)// checks user out, resets rep counter
  {
    i++;
    Serial.println("Logout");
    request.path = "/lab/photon-gym-mirror/command.php?event=set-end";
    http.get(request, response, headers);
    reps=0;
    delay(3000);
    request.path = "/lab/photon-gym-mirror/command.php?event=checkout";
    http.get(request, response, headers);
    delay(2000);
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0