'''
Voltaat Learn (http://learn.voltaat.com)
Link to the full tutorial:
Tutorial: Building a project to count the number of prayer cycles using a Raspberry Pi Pico board
The function of this code is to count the number of prayer cycles
Note: You can use this sketch with any Raspberry Pi Pico.
'''
import machine
import utime
from machine import Pin, I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# -----------------------------
# LCD Configuration
# -----------------------------
I2C_ADDR = 0x27
I2C_ROWS = 2
I2C_COLS = 16
i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_ROWS, I2C_COLS)
# -----------------------------
# Pins
# -----------------------------
trig = Pin(13, Pin.OUT)
echo = Pin(14, Pin.IN)
button = Pin(15, Pin.IN, Pin.PULL_UP)
# -----------------------------
# Variables
# -----------------------------
prostrations = 0
rakats = 0
isProstrating = False
prostrationDistance = 40
# -----------------------------
# Custom characters (bars)
# -----------------------------
bar1 = bytearray([0x1C,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1C])
bar2 = bytearray([0x07,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x07])
bar3 = bytearray([0x1F,0x1F,0x00,0x00,0x00,0x00,0x1F,0x1F])
bar4 = bytearray([0x1E,0x1C,0x00,0x00,0x00,0x00,0x18,0x1C])
bar5 = bytearray([0x0F,0x07,0x00,0x00,0x00,0x00,0x03,0x07])
bar6 = bytearray([0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x1F])
bar7 = bytearray([0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x0F])
bar8 = bytearray([0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00])
lcd.custom_char(1, bar1)
lcd.custom_char(2, bar2)
lcd.custom_char(3, bar3)
lcd.custom_char(4, bar4)
lcd.custom_char(5, bar5)
lcd.custom_char(6, bar6)
lcd.custom_char(7, bar7)
lcd.custom_char(8, bar8)
# -----------------------------
# Distance measuring
# -----------------------------
def measure_distance():
trig.low()
utime.sleep_us(2)
trig.high()
utime.sleep_us(10)
trig.low()
while echo.value() == 0:
pass
start = utime.ticks_us()
while echo.value() == 1:
pass
end = utime.ticks_us()
duration = utime.ticks_diff(end, start)
distance = (duration * 0.0343) / 2
return distance
# -----------------------------
# Printing large numbers
# -----------------------------
def printNumber(value, col):
lcd.move_to(col, 0)
patterns = {
0: [[2, 8, 1], [2, 6, 1]],
1: [[32,32,1], [32,32,1]],
2: [[5,3,1], [2,6,6]],
3: [[5,3,1], [7,6,1]],
4: [[2,6,1], [32,32,1]],
5: [[2,3,4], [7,6,1]],
6: [[2,3,4], [2,6,1]],
7: [[2,8,1], [32,32,1]],
8: [[2,3,1], [2,6,1]],
9: [[2,3,1], [7,6,1]]
}
top, bottom = patterns[value]
# Print top row
lcd.move_to(col, 0)
for c in top:
if c == 32:
lcd.putchar(' ')
else:
lcd.putchar(chr(c))
# Print bottom row
lcd.move_to(col, 1)
for c in bottom:
if c == 32:
lcd.putchar(' ')
else:
lcd.putchar(chr(c))
# -----------------------------
# Main Program
# -----------------------------
# عرض رسالة البداية
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(" VOLTAAT") # السطر الأول
lcd.move_to(0, 1)
lcd.putstr(" RAKAT COUNTER") # السطر الثاني
utime.sleep(2) # انتظر ثانيتين
# إعداد الشاشة للعرض الرئيسي
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Rakat Count:")
printNumber(0, 13)
# -----------------------------
# Loop
# -----------------------------
while True:
distance = measure_distance()
# Check for prostration
if distance < prostrationDistance:
if not isProstrating:
isProstrating = True
prostrations += 1
# Every two prostrations = one rakat
if prostrations % 2 == 0:
rakats += 1
lcd.move_to(0, 1)
lcd.putstr(" " * 16)
printNumber(rakats, 13)
else:
isProstrating = False
# Reset button
if button.value() == 0:
prostrations = 0
rakats = 0
isProstrating = False
lcd.move_to(0, 1)
lcd.putstr(" " * 16)
printNumber(0, 13)
utime.sleep_ms(100)