Back to Parent

Inspired by "Hot Shot Hoops" by Team IOT Basketball (Carlos Roca and Michael Gurdak). Original code available at https://www.hackster.io/iot-basketball/hot-shot-hoops-539e3e
/*
 * Project homehack
 * Description: Laundry basket that determines when it is full and publishes event
 * Author: Ben Fisher
 * Date: 1/29/18
 */

 int ledPin = D0;                // Establish pin for the LED
 int proxPin = A0;               // Establish input pin for proximity sensor
 int buzzPin = D2;               // Establish input pin for piezo buzzer
 int buttonPin= D3;              // Establish input pin for reset button
 int val = 0;                    // Variable for reading the pin status
 int this_sensor=0;              // Used for delay between sensor occurrences
 int proxTriggerFlag=0;
 int binIsFullTimeDelay=3000;    // how many milliseconds the sensor stays active before reporting that the bag is full
                                 // this must be longer than the wait to update the sensor info
 bool isLoading = false;
 // Track if it's on or off
 bool appState = true;
 int alarmLength = 3000;         // max length of alarm if button is not pressed in time

// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(buzzPin, OUTPUT);     // declare sensor as input
  pinMode(proxPin, INPUT);     // declare sensor as input
  pinMode( buttonPin, INPUT_PULLUP); // sets pin as input
  Serial.begin(9600);
}

void checkIfButtonIsOff() {
  int alarmStart = millis();
checkAgain: // can we do this without goto or recursion?
  int buttonState = digitalRead( buttonPin );

  appState = buttonState;
  if( !appState ){
    digitalWrite(buzzPin, LOW); // button is correct; turn alarm off
  }else{
    if (millis()-alarmStart>=alarmLength){
      digitalWrite(buzzPin, LOW);
    }else{
      delay(500); //check again in 1/2 second
      goto checkAgain;
    }
  }
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.
  val = analogRead(proxPin);  // read input value
  Particle.variable("val",&val,INT);
  Particle.variable("this_sensor",&this_sensor,INT);

  // blink new LED for 100 ms to indicate that sensor data was published
  delay(100);
  if (val <= 2000) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    delay(100); //led test only, remove later
    digitalWrite(ledPin, LOW);  // turn LED ON
    delay(100); //led test only, remove later

    // implement a wait to ensure that any falling clothes don't trigger "full"
    // just because they pass the sensor

    // Is this the first time that this_sensor was triggered?
    if (this_sensor==0){
      this_sensor = millis(); //now we know when it is first active.
    }

    if (abs(millis()-this_sensor)>=binIsFullTimeDelay && proxTriggerFlag==1){
      //then the trigger has been active for the binIsFullTimeDelay
      // sound the alarm!
      digitalWrite(buzzPin, HIGH);
      delay(500); // useful in case button is faulty
      // The alarm is going off; it's up to the user to push the reset button
      // or wait for alarmLength to expire
      checkIfButtonIsOff();

      // Whether the button is pushed or the timer expires, publish an event
      Particle.publish("Laundry is Full!", PRIVATE); //Publish event (console)
      this_sensor=0;  // Used to reset timer for next proximity sensor trigger
      }
    digitalWrite(ledPin, LOW); // keep LED OFF
    proxTriggerFlag=1; // record flag that proximity sensor was triggered
  }else{
    proxTriggerFlag=0; // prevents two completely separate occurrences from
                       // triggering the "laundry is full" loop
                       // (only continuous triggering will keep the flag = 1)
    this_sensor=0;
  }
  // wait 1s before retrieving data again (per assignment)
  delay(1000);
}
Benjamin Fisher Click to Expand

Content Rating

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

0