#!/usr/bin/python
import pibrella
import time

# Turn the power on to the sensor
pibrella.output.e.on()

# Set a flag saying whether the button is pressed
# It starts out not pressed (false)
is_button_pressed=False

# What to do when the button is pressed
def button_pressed(pin):
  global is_button_pressed
  is_button_pressed=True
pibrella.button.pressed(button_pressed)

# Loop forever (or until CTRL-C is pressed)
while True:

  # Allow time for owner to leave the room
  print "I'm waiting..."
  pibrella.light.yellow.blink(0.5,0.5)
  time.sleep(5)
  pibrella.light.off()
  pibrella.light.green.on()

  # Look out for goblins
  print "I'm looking for movement..."
  while not pibrella.input.a.read():
    time.sleep(0.25)

  # Goblins detected - sound the alarm!
  print "I saw you move!"
  pibrella.light.off()
  pibrella.light.red.blink(0.1,0.1)
  pibrella.buzzer.fail()
  is_button_pressed=False

  # Keep sounding the alarm until the button is pressed
  while not is_button_pressed:
    time.sleep(0.25)

  # Turn off the alarm after the button has been pressed
  print "You pressed the button, so I'll reset now."
  pibrella.light.off()
  pibrella.buzzer.off()
