int ledPinRed = D3;
int ledPinGreen = D4;
int ledPinYellow = D2;
int knockSensor = A0;
bool state = false;
int timeToBus = 0;
void setup() {
//initial state of all 3 pins
pinMode(ledPinRed, OUTPUT);
digitalWrite(ledPinRed, LOW);
pinMode(ledPinGreen, OUTPUT);
digitalWrite(ledPinGreen, LOW);
pinMode(ledPinYellow, OUTPUT);
digitalWrite(ledPinYellow, LOW);
//Port Authority webhook
Particle.subscribe("hook-response/bustime", myBusTimeHandler, MY_DEVICES);
//view variable value in console
Particle.variable("timeToBus", timeToBus);
}
void loop() {
//read sensor
int sensorReading = analogRead(knockSensor);
//change state when sensor is pressed
if (sensorReading > 100){
state = !state;
}
//if sensor is pressed, lights respond to the bus time
if( state == true )
{
Particle.publish( "bustime" );
delay( 1000 );
if(timeToBus > 10)
{
digitalWrite(ledPinGreen, HIGH);
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, LOW);
}
else if(timeToBus <= 10 && timeToBus > 5)
{
digitalWrite(ledPinYellow, HIGH);
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinGreen, LOW);
}
else if(timeToBus <= 5 && timeToBus > 3)
{
digitalWrite(ledPinRed, HIGH);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, LOW);
}
else if(timeToBus <= 3)
{
digitalWrite(ledPinRed, HIGH);
delay(100);
digitalWrite(ledPinRed, LOW);
delay(100);
}
}
else //if sensor is pressed again, state changes to false, lights turn off
{
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinGreen, LOW);
}
}
//method for Port Authority webhook
void myBusTimeHandler(const char *event, const char *data) {
// Handle the integration respo
String busPrediction = String( data );
if( busPrediction.equals( "DUE" ) ){
timeToBus = 0;
}else{
timeToBus = busPrediction.toInt();
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .