Back to Parent

Code for Bus Tracker
//Code for Bus Tracker
int bPin = D6;
int yPin = D4;
int rPin = D2;

int busTime = 0;

void setup() {
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/hook-response/bustime", predictionHandler);
  Serial.begin(9600);
  
    pinMode(bPin, OUTPUT);
    pinMode(yPin, OUTPUT);
    pinMode(rPin, OUTPUT);
}

void predictionHandler(const char *event, const char *data) {
  // Handle the integration response
  String dataStr = String(data);
  busTime = dataStr.toInt();
}

void loop() {

  Serial.print("Bus Time: ");
  Serial.print(busTime);
  Serial.println(" min");
  //Serial.print("brightness: ");
  //Serial.println(bright);

    if (busTime <= 10) {
        Serial.println("Setting red Pin on");
        Serial.println("Bus coming in 10 min");
        
        digitalWrite(yPin, LOW);
        digitalWrite(bPin, LOW);
        
        int brightness = map(busTime, 0, 10, 255, 100);
        Serial.print("Brightness: ");
        Serial.println(brightness);
        Serial.println();

        for(int i=0;i<brightness;i++){
            analogWrite(rPin,i);
            delay(50);
        }
        for(int i=brightness;i>=0;i--){
            analogWrite(rPin,i);
            delay(50);
        }
        
        
    } else if (busTime <= 15 && busTime > 10) {
        Serial.println("Setting yellow Pin on");
        Serial.println("Bus coming, go downstairs");

        digitalWrite(rPin, LOW);
        digitalWrite(bPin, LOW);
        
        int brightness = map(busTime, 10, 15, 255, 100);
        Serial.print("Brightness: ");
        Serial.println(brightness);
        Serial.println();
        
        
        for(int i=0;i<brightness;i++){
            analogWrite(yPin,i);
            delay(80);
        }
        for(int i=brightness;i>=0;i--){
            analogWrite(yPin,i);
            delay(80);
        }
        
        
    } else if (busTime <= 25 && busTime > 15) {
        Serial.println("Setting blue Pin on");
        Serial.println("Bus coming, get prepared");

        digitalWrite(yPin, LOW);
        digitalWrite(rPin, LOW);
        
        int brightness = map(busTime, 15, 25, 255, 100);
        Serial.print("Brightness: ");
        Serial.println(brightness);
        Serial.println();
        
        for(int i=0;i<brightness;i++){
            analogWrite(bPin,i);
            delay(150);
        }
        for(int i=brightness;i>=0;i--){
            analogWrite(bPin,i);
            delay(150);
        }
        
        
    } else {
        Serial.println("Turning off all LEDs");
        Serial.println("No bus coming in 20 min");
        Serial.println();
        
        digitalWrite(rPin, LOW);
        digitalWrite(yPin, LOW);
        digitalWrite(bPin, LOW);
    }

  Particle.publish("hook-response/bustime");
  // Wait 60 seconds
  delay(15000);
}
Click to Expand

Content Rating

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

0