Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Created October 27, 2020 07:50
Show Gist options
  • Save IdrisCytron/3e910a276813bb343315b475a37483fe to your computer and use it in GitHub Desktop.
Save IdrisCytron/3e910a276813bb343315b475a37483fe to your computer and use it in GitHub Desktop.
Soil Moisture Alarm Using CircuitPython on Seeeduino XIAO
import time
import board
import simpleio
import digitalio
from analogio import AnalogIn
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
piezo = board.A3
sensor = AnalogIn(board.A2)
NOTE_C4 = 262
NOTE_D4 = 294
NOTE_E4 = 330
NOTE_F4 = 349
NOTE_G4 = 392
NOTE_A4 = 440
NOTE_E5 = 659
NOTE_F5 = 698
# Define a list of tones/music notes to play.
COFFIN_DANCE = [
NOTE_D4, 1, NOTE_D4, NOTE_A4, NOTE_G4, 1, NOTE_F4, 1, NOTE_E4, 1,
NOTE_E4, NOTE_F4, NOTE_G4, 1, NOTE_F4, NOTE_E4, NOTE_D4, 1,
NOTE_D4, NOTE_F5, NOTE_E5, NOTE_F5, NOTE_E5, NOTE_F5, NOTE_D4, 1,
NOTE_D4, NOTE_F5, NOTE_E5, NOTE_F5, NOTE_E5, NOTE_F5
]
NOTE_DURATIONS = [
4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4,
]
# Play start melody
simpleio.tone(piezo, NOTE_C4, duration=0.1)
simpleio.tone(piezo, NOTE_G4, duration=0.1)
prev_time = time.monotonic()
alarm = False
# Main loop will go through each tone in order up and down.
while True:
if time.monotonic() - prev_time > 5:
adc = sensor.value
led_count = adc/6553
if led_count > 6:
alarm = True
else:
alarm = False
for i in range(led_count):
led.value = False
time.sleep(0.05)
led.value = True
time.sleep(0.5)
prev_time = time.monotonic()
if alarm == True:
for i in range(len(COFFIN_DANCE)):
simpleio.tone(piezo, COFFIN_DANCE[i], duration=1/NOTE_DURATIONS[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment