Trigger on Different Conditions

You can use the multitude of sensors Adafruit carries to monitor things and trigger voice prompts based on the sensor results. 

The program below expands on the code used in the Adafruit tutorial Make It Sense. In that guide, the various sensors on a Circuit Playground Express trigger various actions. Those actions could trigger the board to speak various words or phrases also! We'll again use CircuitPython as MakeCode does not have wav file support.

You can also see examples of Circuit Playground Express and CircuitPython code in the Adafruit tutorial CircuitPython Made Easy on Circuit Playground Express.

For how to use external switches with microcontrollers, see the Adafruit guide Make It Switch.

An Example: Temperature

Take the sample CircuitPython code for temperature in Make it Sense. Rather than plot the temperature, you can use a voice to say something depending on the temperature. For example, > 90 degrees Fahrenheit it says "too hot!" and 32 degrees F or less it says "too cold!".

import time
from adafruit_circuitplayground.express import cpx
 
while True:
   if cpx.temperature > 37.0:  # Celsius fever reading
       cpx.play_file("fever.wav") 
   time.sleep(5)

For Circuit Playground Express and sensors available beyond the thermistor, see the Adafruit Circuit Playground Express guide.

Saying Numbers

When it comes to numbers, there are literally an infinite number of them. And recording an infinite combination of files would be very tough. All the commercial voice programs break a number into the numeric places and voice those. So for the number 25, you would say the word "Twenty" followed by the number "Five" instead of recording "Twenty Five".

The following program expands on the example above, saying the temperature from -299 to 299 degrees Celsius or Fahrenheit.. The numbers have been recorded from the IBM BlueMix site mentioned earlier. Click Download Project Zip to get the file say-temp.py and a subdirectory containing the WAV files.

Copy say-temp.py to the CIRICUITPY drive on a Circuit Playground Express as code.py.

Besides the numbers, there are some additional files for projects for humidity, clocks, and the words thousand and million that are not needed for this project. The other files should be copied to the CIRCUITPY drive in the subdirectory /numbers.

# CircuitPython Speaking Thermometer Example
# Coded for Circuit Playground Express but it may be
# modified for any CircuitPython board with changes to
# button, thermister and audio board definitions.
# Mike Barela for Adafruit Industries, MIT License

import time
import board
import adafruit_thermistor
import audioio
import audiocore
from digitalio import DigitalInOut, Direction, Pull

# Enables the speaker for audio output
spkrenable = DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = Direction.OUTPUT
spkrenable.value = True

D1 = board.BUTTON_A
D2 = board.BUTTON_B

# Button A setup (Celsius)
button_a = DigitalInOut(D1)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
# Button B setup (Fahrenheit)
button_b = DigitalInOut(D2)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN

# Set up reading the Circuit Playground Express thermistor
thermistor = adafruit_thermistor.Thermistor(
    board.TEMPERATURE, 10000, 10000, 25, 3950)

# Audio playback object and helper to play a full file
aout = audioio.AudioOut(board.A0)

# Play a wave file
def play_file(wavfile):
    wavfile = "/numbers/" + wavfile
    print("Playing", wavfile)
    with open(wavfile, "rb") as f:
        wav = audiocore.WaveFile(f)
        aout.play(wav)
        while aout.playing:
            pass

# Function should take an integer -299 to 299 and say it
# Assumes wav files are available for the numbers
def read_temp(temp):
    play_file("The temperature is.wav")
    if temp < 0:
        play_file("negative.wav")
        temp = - temp
    if temp >= 200:
        play_file("200.wav")
        temp = temp - 200
    elif temp >= 100:
        play_file("100.wav")
        temp = temp - 100
    if (temp >= 0 and temp < 20) or temp % 10 == 0:
        play_file(str(temp) + ".wav")
    else:
        play_file(str(temp // 10) + "0.wav")
        temp = temp - ((temp // 10) * 10 )
        play_file(str(temp) + ".wav")
    play_file("degrees.wav")

while True:
    if button_a.value:
        read_temp(int(thermistor.temperature))
        play_file("celsius.wav")
    if button_b.value:
        read_temp(int(thermistor.temperature * 9 / 5 + 32))
        play_file("fahrenheit.wav")
    time.sleep(0.01)

Going Further With Numbers

See the pre-made WAV files files located in the GitHub repository and see if you'd like to make a clock, a humidity display, or reading another type of number. Feel free to make your own phrases to go along with the ones we have provided.

This guide was first published on Dec 19, 2018. It was last updated on Dec 19, 2018.

This page (Numbers) was last updated on Jun 21, 2021.

Text editor powered by tinymce.