Back to Parent

int trigPin = D2;

int echoPin = D3;

int led = D4;

int led2 = D5;

int sittingTime = 0;
int sit = false;
int count = 0;

int buttonPin = D6;
int buttonState = 0;
int previousState = 0;

Timer timer(1000, print_every_second);

void print_every_second()
{
    if (sit == true && count > 900){
      Serial.println(count++);
      Particle.publish("SittingTooLong", "YES");
      count = 0;
    } else if (sit == true){
      Serial.println(count++);
    } else {
      count = 0;
    }
}

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led,LOW);

  pinMode(led2, OUTPUT);
  digitalWrite(led,LOW);

  pinMode(buttonPin, INPUT);

  timer.start();
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance < 10) {  // This is where the LED On/Off happens
    digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
    sit = true;
    digitalWrite(led2,LOW);

  } else {
    sit = false;
    digitalWrite(led,LOW);
    digitalWrite(led2,HIGH);
  }
/*  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }*/
  //Switch control
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH && previousState == LOW){
    //do something
    Particle.publish("buttonPress", "HIGH");
  }

  previousState = buttonState;
  Serial.println(buttonState);
  delay(500);

}
Click to Expand

Content Rating

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

0