Back to Parent

Coding for LAT Machine
#include <stdio.h>
#include <string.h>
#include <math.h>​
int SENSORPIN = A4;
int sensorState = 0, lastState=0;
const int trigPin = 2;
const int echoPin = 4;
int rep=0;
int calories;
String data;

void setup() {
  // initialize serial communication:
  pinMode(SENSORPIN, INPUT);
  digitalWrite(SENSORPIN, HIGH);
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  sensorState = digitalRead(SENSORPIN);
  // Sensor Beam is Broken


  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
    rep=rep+1;
  }
  lastState = sensorState;
  long duration, inches, cm , weight;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  if (cm < 5)
  {weight= 10;}
  else if (cm>=5 && cm<7)
  {weight= 15;}
  else if (cm>= 7)
  {weight= 0;}
  calories= weight*rep/10;

  //Particle.publish("himanshu2016/lat/calories",+String(calories));
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.print(weight);
  Serial.print(" kg, ");
  Serial.print(rep);
  Serial.print(" repition, ");
  Serial.print(calories);
  Serial.print(" calories");
  Serial.println();
  announcedata( calories );

  //else if (cm>=7)
  //{return -1;}
  delay(1000);
}
void announcedata( int calories )
{

  String data = String(calories);
  // and share as a public event
  Particle.publish("himanshu2016/lat",data);

}
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
Click to Expand

Content Rating

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

0