Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as C++ by Unaclocker ( 15 years ago )
/* Written by Brian Gosney
   Programming Started January 27th, 2011
   Uses AdaFruit WaveShield to voice alert.
   
    Pin use notes:
    WaveShield: DI - 4, LAT - 5, CLK - 8, CCS - 10, LCS - 7
    Door trigger: Pin 2 (low on trigger)
    Water sensor: Pin 3 (high on trigger)
    "Robot head" trigger: Pin 9
    Future plans: Light Sensor Analog 0\
    
    This sketch licensed Creative Commons
*/
#include "WaveHC.h"
#include "WaveUtil.h"
#include <CapSense.h>


volatile boolean isArmed = false;
volatile boolean doorOpen = false;
volatile boolean doorAjar = false;
volatile boolean warnedAboutDoor = false;
volatile unsigned long doorTime = 0; // debounce variable
volatile unsigned long flushTime = 0;  // ditto
volatile unsigned long visitTime = 0;  // When the system arms, this is the time it armed.
volatile boolean alarming = false;
unsigned long lastWarble = 0;

// The next 5 lines are for the WaveShield WaveHC library
SdReader card;
FatVolume vol;
FatReader root;
FatReader file;
WaveHC wave;

// CapSense, so you can disarm the alarm by touching the Robot's antenae
CapSense antenae = CapSense(A1, A2);


void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(9, OUTPUT);
  digitalWrite(9, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  attachInterrupt(1, doorMonitor, RISING);
  attachInterrupt(0, toiletMonitor, RISING);
  card.init();  // Completely ignoring errors that might get returned. This is an embedded system, it should always Just Work.
  card.partialBlockRead(true);
  vol.init(card);
  root.openRoot(vol);
  doorTime = millis();
  flushTime = doorTime;
  visitTime = doorTime;
}


void talkNow(byte whatToSay) {
  Serial.println("Talking requested");
  while (wave.isplaying) { Serial.println("Waiting.. "); }
  switch (whatToSay) {
    case 1:
    file.open(root, "ENTER.WAV");
    break;
    case 2:
    file.open(root, "THANKS.WAV");
    break;
    case 3:
    file.open(root, "SHUTDOOR.WAV");
    break;
    case 4:
    file.open(root, "FORGOT.WAV");
    break;
    case 5:
    file.open(root, "TURNOFF.WAV");
    break;
    case 6:
    file.open(root, "WASHHAND.WAV");
    break;
    case 7:
    file.open(root, "DISARMED.WAV");
    break;
  }
  wave.create(file);
  wave.play();
}

void doorMonitor() {
  Serial.print("DoorTriggered ");
  delay(300); // An attempt to debounce the door trigger.
  if (digitalRead(3)==LOW) { return; } // Debounce attempt, if after 300ms the door appears to be shut rather than open, this was a door shutting bounce. 
  if (alarming) { return; }  // If the alarm is already going off, only flushing the toilet or disarming will help. 
//  talkNow(6);
  if ((millis()-doorTime) > 12000) {  // 12 seconds to open/shut door. Debounces for both the open and close. - Primary debounce routine.
    Serial.println("Doortime is more than 12 seconds");
    doorTime = millis(); // Note time of door opening for the debounce.
    lastWarble = millis() - 20000;
    if (isArmed) {  
      alarming = true; // If it's armed they entered the bathroom and are now leaving without disarming (toilet flush), set off alarm.
    } else { // need to add if statement to see if toilet recently flushed.
      if (digitalRead(2)==LOW) { // if the toilet tank is full, they're probably entering, not leaving.
      visitTime=millis();
      isArmed = true;  // If it's not armed then the door opening will arm the system.
      talkNow(1);  // weclome them to the bathroom.
      }
    }
  }
}


void toiletMonitor() {
  Serial.println("Toilet Flushed");
  if (!isArmed) { return; } // There is some "bounce" at the top of the refill cycle. This should ignore it.
  if ((millis()-flushTime) > 3000) { // Debounce, we don't want it saying the same thing several times.
      Serial.println("Toilet disarm.");
      isArmed = false;  // Disarm
      alarming = false;  // Turn off the alarm if the flush came too late.
      flushTime = millis();  // Part of debounce
      talkNow(2); // Thank them for flushing.
  }
}

void letsMakeSomeNoise(byte whatToSay) {
  if ((millis()-lastWarble)> 10000) { // Wait 10 seconds between alarm noises.
    lastWarble=millis(); // update start time of last noise.
    digitalWrite(9, LOW); // Trigger the "warble" alarm built into the robot head.
    delay(1100);  // Wait 1 second for the sound to be made.
    digitalWrite(9, HIGH);  // includes the blinking LEDs that make up the eyes and mouth.
    talkNow(whatToSay);  // Call the talking subroutine.
  }
}


void loop() {
//  Serial.println("mainloop");
  if (isArmed) {
    if ((millis()-visitTime) > 480000) {
      isArmed = false;  // If the door opens, and shuts, and remains shut
                        // for more than 8 minutes, it's a bath, not a toilet
                        // visit, so disarm the toilet check.
      return; // break out of this if statement.
    }
    if (antenae.capSense(30)>500) { // Check to see if the antenae are being touched.
      Serial.println("I feel that");
      isArmed = false;   // Manual disarm
      alarming = false;  // If the alarm is actively going off, stop.
      talkNow(7);  // Announce manual disarm state.
    }
    if (alarming) {
      letsMakeSomeNoise(4);   // I think this whole statement is hilarious.
    }
  }
  if ((digitalRead(3) == HIGH) && ((millis()-doorTime) > 20000)) {  // If the door is open, and has been for 20+ seconds
    if (!warnedAboutDoor) { // and you haven't already asked for it to be shut
      letsMakeSomeNoise(3);  // Set off alarm, and request they come shut the door.
      warnedAboutDoor = true;  // remember that we warned them so it only does it once.  -- Might add a timer so it'll continue warning every 20 seconds?
    }
  }
  if ((digitalRead(3) == LOW) && warnedAboutDoor) {
    warnedAboutDoor = false;  // reset warning trigger after the door has been shut, if it went off.
  }
}

 

Revise this Paste

Your Name: Code Language: