Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

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 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() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  attachInterrupt(0, doorMonitor, CHANGE);
  attachInterrupt(1, 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) {
  while (wave.isplaying) { }
  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() {
  // To do... Gotta debounce, arm/disarm.
}

void toiletMonitor() {
  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.
      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 loop() {
  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.
    }
    if (antenae.capSense(30)>5) { // Check to see if the antenae are being touched.
      isArmed = false;   // Manual disarm
      alarming = false;  // If the alarm is actively going off, stop.
      talkNow(7);  // Announce manual disarm state.
    }
  }
}

 

Revise this Paste

Your Name: Code Language: