File: example1.py

package info (click to toggle)
nxt-python 3.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 812 kB
  • sloc: python: 6,857; xml: 22; makefile: 20; sh: 4
file content (34 lines) | stat: -rw-r--r-- 1,007 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import nxt.locator
from nxt.sensor.hitechnic import SuperPro

"""
Experiment 1, adapted from the SuperPro demo

For this demo, attach an LED between B0 and GND, a button between 3V and A0, and
a 10kOhm resistor between GND and A0.

When you press the button, it will turn on the LED.
"""

# Find NXT, configure sensor
with nxt.locator.find() as brick:
    pro = SuperPro(brick, nxt.sensor.Port.S1)

    # Configure B0 as output
    pro.set_digital_modes_byte(0x01)

    while True:
        try:
            # The original demo doesn't convert to volts, but I think displaying volts
            # to the user is better than bits.
            analog_value = pro.get_analog_volts()["a0"]
            print(f"Analog 0: {analog_value}V")
            if analog_value > 3.3 / 2.0:
                pro.set_digital_byte(0x01)
            else:
                pro.set_digital_byte(0x00)
        except KeyboardInterrupt:
            break

    # When program stopped, turn off outputs
    pro.set_digital_byte(0x00)