firmata.set_pin_mode(BUTTON_SWITCH, firmata.INPUT, firmata.DIGITAL)

# Arm pin A2 for latching a value >= 678
firmata.set_analog_latch(POTENTIOMETER, firmata.ANALOG_LATCH_GTE, 678)

# Arm pin 12 for latching when the pin goes high
firmata.set_digital_latch(BUTTON_SWITCH, firmata.DIGITAL_LATCH_HIGH)

print "start waiting"
# wait for 5 seconds to allow user interaction with switch and pot
# during this time press and release the switch and turn the pot to maximum

time.sleep(5)

print 'end waiting'
# get and print the digital latched data
print firmata.get_digital_latch_data(BUTTON_SWITCH)

# get and print the analog data latched data
a_latch = firmata.get_analog_latch_data(POTENTIOMETER)
print a_latch
# print gmtime for the time stamp
print time.gmtime(a_latch[firmata.LATCHED_TIME_STAMP])

# wait 2 more seconds and see that the latch entry data is now cleared

time.sleep(2)
print firmata.get_digital_latch_data(BUTTON_SWITCH)
print firmata.get_analog_latch_data(POTENTIOMETER)

firmata.close()
signal.signal(signal.SIGINT, signal_handler)

# set pin modes
board.set_pin_mode(GREEN_LED, board.OUTPUT, board.DIGITAL)
board.set_pin_mode(RED_LED, board.PWM, board.DIGITAL)
board.set_pin_mode(PUSH_BUTTON, board.INPUT, board.DIGITAL)
board.set_pin_mode(POTENTIOMETER, board.INPUT, board.ANALOG)
board.set_analog_latch(POTENTIOMETER, board.ANALOG_LATCH_GTE, 1000)


# do nothing loop - program exits when latch data event occurs for potentiometer
while 1:
    count += 1
    if count == 300:
        print('bye bye')
        board.close()
    analog = board.analog_read(POTENTIOMETER)
    board.analog_write(RED_LED, analog // 4)

    digital = board.digital_read(PUSH_BUTTON)
    board.digital_write(GREEN_LED, digital)
    latch = board.get_analog_latch_data(POTENTIOMETER)
    if latch[1] == board.LATCH_LATCHED:
        board.analog_write(RED_LED, 0)
        board.digital_write(GREEN_LED, 0)
        print('Latching Event Occurred at: ' + time.asctime(time.gmtime(latch[3])))
        board.close()
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Set pin modes
board.set_pin_mode(GREEN_LED, board.OUTPUT, board.DIGITAL)
board.set_pin_mode(RED_LED, board.PWM, board.DIGITAL)
board.set_pin_mode(PUSH_BUTTON, board.INPUT, board.DIGITAL)
board.set_pin_mode(POTENTIOMETER, board.INPUT, board.ANALOG)
board.set_analog_latch(POTENTIOMETER, board.ANALOG_LATCH_GTE, 1000)

# Do nothing loop - program exits when latch data event occurs for
# potentiometer
while 1:
    count += 1
    if count == 300:
        print('Terminated')
        board.close()
    analog = board.analog_read(POTENTIOMETER)
    board.analog_write(RED_LED, analog // 4)

    digital = board.digital_read(PUSH_BUTTON)
    board.digital_write(GREEN_LED, digital)
    latch = board.get_analog_latch_data(POTENTIOMETER)
    if latch[1] == board.LATCH_LATCHED:
        board.analog_write(RED_LED, 0)
        board.digital_write(GREEN_LED, 0)
        print('Latching Event Occurred at: ' + \
              time.asctime(time.gmtime(latch[3])))
        board.close()
        sys.exit(0)
firmata.set_pin_mode(BUTTON_SWITCH, firmata.INPUT, firmata.DIGITAL)

# Arm pin A2 for latching a value >= 678
firmata.set_analog_latch(POTENTIOMETER, firmata.ANALOG_LATCH_GTE, 678)

# Arm pin 12 for latching when the pin goes high
firmata.set_digital_latch(BUTTON_SWITCH, firmata.DIGITAL_LATCH_HIGH)

print "start waiting"
# wait for 5 seconds to allow user interaction with switch and pot
# during this time press and release the switch and turn the pot to maximum

time.sleep(5)

print 'end waiting'
# get and print the digital latched data
print firmata.get_digital_latch_data(BUTTON_SWITCH)

# get and print the analog data latched data
a_latch = firmata.get_analog_latch_data(POTENTIOMETER)
print a_latch
# print gmtime for the time stamp
print time.gmtime(a_latch[firmata.LATCHED_TIME_STAMP])

# wait 2 more seconds and see that the latch entry data is now cleared

time.sleep(2)
print firmata.get_digital_latch_data(BUTTON_SWITCH)
print firmata.get_analog_latch_data(POTENTIOMETER)

firmata.close()