Published © CC BY-NC-SA

Time To Live

How long do you have? A magic book to tell you exactly that!

BeginnerFull instructions provided20 hours794

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Push Button (any)
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Dremel 4300
Bookbinders Glue
Fire

Story

Read more

Code

code.py

Python
Code for the book
# Import libraries
from __future__ import print_function
import time
import datetime
import schedule
import RPi.GPIO as io
import random
io.setmode(io.BCM)

print("Hi, this is Time To Live")

#Setup the pins of the Raspberry

buttonPins = 21
io.setup(buttonPins, io.IN, pull_up_down=io.PUD_UP)

# Pins for the LCD Screen
# Register Select
lcdRs = 7
# Enable or Strobe
lcdE = 8
# Data pins
lcdData4 = 25
lcdData5 = 24
lcdData6 = 23
lcdData7 = 18
# Setup LCD Pins
io.setup(lcdE, io.OUT)
io.setup(lcdRs, io.OUT)
io.setup(lcdData4, io.OUT)
io.setup(lcdData5, io.OUT)
io.setup(lcdData6, io.OUT)
io.setup(lcdData7, io.OUT)

#LCD Constants
# Characters per line
lcdWidth = 16
lcdChr = True
lcdCmd = False
# LCD Ram address 1st line
lcdLine1 = 0x80
# LCD Ram address 2nd line
lcdLine2 = 0xC0
# Timing
lcdPulse = 0.0005
lcdDelay = 0.0005

# LCD Screen setup
def lcdByte(bits, mode):
    # Send byte to data pins
    # bits = data
    # mode = True  for character
    #        False for command

    io.output(lcdRs, mode)  # RS

    # High bits
    io.output(lcdData4, False)
    io.output(lcdData5, False)
    io.output(lcdData6, False)
    io.output(lcdData7, False)
    if bits & 0x10 == 0x10:
        io.output(lcdData4, True)
    if bits & 0x20 == 0x20:
        io.output(lcdData5, True)
    if bits & 0x40 == 0x40:
        io.output(lcdData6, True)
    if bits & 0x80 == 0x80:
        io.output(lcdData7, True)

    # Toggle 'Enable' pin
    lcdToggleEnable()

    # Low bits
    io.output(lcdData4, False)
    io.output(lcdData5, False)
    io.output(lcdData6, False)
    io.output(lcdData7, False)
    if bits & 0x01 == 0x01:
        io.output(lcdData4, True)
    if bits & 0x02 == 0x02:
        io.output(lcdData5, True)
    if bits & 0x04 == 0x04:
        io.output(lcdData6, True)
    if bits & 0x08 == 0x08:
        io.output(lcdData7, True)

    # Toggle 'Enable' pin
    lcdToggleEnable()


def lcdToggleEnable():
    # Toggle enable
    time.sleep(lcdDelay)
    io.output(lcdE, True)
    time.sleep(lcdPulse)
    io.output(lcdE, False)
    time.sleep(lcdDelay)


# Initialise LCD Display
lcdByte(0x33,lcdCmd) # 110011 Initialise
lcdByte(0x32,lcdCmd) # 110010 Initialise
lcdByte(0x06,lcdCmd) # 000110 Cursor move direction
lcdByte(0x0C,lcdCmd) # 001100 Display On,Cursor Off, Blink Off
lcdByte(0x28,lcdCmd) # 101000 Data length, number of lines, font size
lcdByte(0x01,lcdCmd) # 000001 Clear display
time.sleep(lcdDelay)

def lcdShowMessage(message, line):
    # Send string to display

    message = message.ljust(lcdWidth, " ")

    lcdByte(line, lcdCmd)

    for i in range(lcdWidth):
        lcdByte(ord(message[i]), lcdChr)
        
# We have 16 chars
lcdShowMessage("Time to live", lcdLine1)
number = None

while True:

    time.sleep(0.2)

    # Listen for the press, if pressed generate new number
    if io.input(buttonPins) == False: 

        # Generate the number of seconds to display
        number = random.randint(1,60)

        # Display  time left
        lcdShowMessage(str(number) , lcdLine2)
        time.sleep(0.8)

    #Button not pressed, we don't need to generate a new number
    elif number != None:

        # Check if there's time left
        if number > 0:
            #Lower the amount of seconds and display
            number -= 1
            time.sleep(0.8)
            lcdShowMessage(str(number) , lcdLine2)

        else:
            # None left, bye bye
            lcdShowMessage("Bye Bye" , lcdLine2)
    



        

Credits

Comments