Back to Parent

int pressPin = A0;
int pressRead = 0;
int ledPin   = D0;
bool ledState = FALSE;
int btn = D2;
int btnState = LOW;
bool toggle = false;
int currentButtonState = HIGH;
unsigned long lastPublishTime=60000;
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(btn, INPUT_PULLUP);
  Particle.variable("press", &pressRead, INT);
  Particle.variable("button", &currentButtonState, INT);
  digitalWrite(ledPin, LOW);
  Particle.subscribe( "busyornot" , callingMedidate );
}

void loop()
{
  TurnOn();
}
void TurnOn()
{
  int currentButtonState = digitalRead(btn);
  if( currentButtonState == LOW && btnState != currentButtonState )
  {
    toggle = !toggle;
  	digitalWrite(ledPin, toggle);
  }
  btnState = currentButtonState;
}
void callingMedidate(const char *event, const char *data)
{
    meditate();
}

void meditate()
{
  if (toggle == TRUE)
  {
  pressRead = analogRead(pressPin);
  if (millis()-lastPublishTime>10000)
    { if (pressRead > 1500)
        {
        Particle.publish("busy");
        lastPublishTime = millis();
        }
      else
        {
        Particle.publish("free");
        lastPublishTime = millis();
        }
    }
  }
  else
  {
  Particle.publish("noton");
  }
}
Click to Expand

Content Rating

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

0