Back to Parent

int hallsensor = D0; //set up hall sensor
int vib = D1; //set up vibration motor

void setup()
{
 pinMode( hallsensor , INPUT ); // set hall sensor as input
 pinMode(vib, OUTPUT); // set vibration motor as output

 Particle.subscribe(  "diot2016/bcj/wave" , vibration ); //subsribe to event from other particle
 Serial.begin(9600); // serial to test
}
void loop()
{
 checkOpen(); //run function to check whether the box is open or not
}

void vibration(const char *event, const char *data) //function of vibration
{
 digitalWrite(vib, HIGH); //turn motor on
 delay(1000); //delay
 digitalWrite(vib, LOW); //turn motor off
}

int checkOpen() //check box function
{
 int hand = digitalRead(hallsensor); //read hall sensor
 if (hand == 0) //if hall sensor reads "open"
 {
   Particle.publish("diot2016/bcj/open"); // publish event
   delay(1000); // delay 1 second
 };
}
Click to Expand

Content Rating

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

0