top of page

Programming Instructions

Step 1: Setting up Raspberry Pi

1. Install "NOOBS" file onto SD card: https://www.raspberrypi.org/documentation/installation/noobs.md

​

2. Connect SD card to Raspberry Pi, connect power, plug in keyboard and mouse, go through setup, and           install the Raspian software (will take a few minutes to install).

​

3. Open File Explorer, create new empty file named "distGUI.py" and go back to file location. Right click file      and select "Open With". Under "Installed Applications" and "Programming", select "Geany" and make it        the default application for the file type.

​

4. Prevent the screen from sleeping automatically (Needs restart for changes to take effect):

     https://www.ceos3c.com/misc/disable/

​

5. Copy and paste the code located below into program and run.

Optional Functions:

- Use VNC Viewer to access Raspberry Pi from laptop or phone remotely through a wifi connection:

   https://www.raspberrypi.org/documentation/remote-access/vnc/

​

- Use SMTP to send emails from the Raspberry Pi:

    https://www.youtube.com/watch?v=0kpGcMjpDcw

​

- Automatically run program when Raspberry Pi turns on:

   https://www.dexterindustries.com/howto/auto-run-python-programs-on-the-raspberry-pi/

​

- Automatically hide the taskbar to make the program full screen:

   https://raspberrypi.stackexchange.com/questions/8874/how-do-i-auto-hide-the-taskbar

Code (Copy Everything Below):

#Program Created By Navod Jayawardhane (Food Pantry Box)
from gpiozero import DistanceSensor
import time
import Tkinter as tk

root = tk.Tk()
root.title("Cans")
label1 = tk.Label(root, width=100, fg="black", bg="white")
label1.config(font=("Courier",25))
label2 = tk.Label(root, width=100, fg="black", bg="white")
label2.config(font=("Courier",25))
label3 = tk.Label(root, width=100, fg="black", bg="white")
label3.config(font=("Courier",15))
button = tk.Button (root, text="Exit", command=root.destroy)
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))

echo1 = 4
trigger1 = 3
echo2 = 17
trigger2 = 27
echo3 = 10
trigger3 = 9
echo4 = 5
trigger4 = 6
echo5 = 19
trigger5 = 26
echo6 = 14
trigger6 = 15
echo7 = 23
trigger7 = 24
echo8 = 8
trigger8 = 7
echo9 = 12
trigger9 = 16
echo10 = 20
trigger10 = 21 
ultrasonic1 = DistanceSensor(echo=echo1, trigger=trigger1)
ultrasonic2 = DistanceSensor(echo=echo2, trigger=trigger2)
ultrasonic3 = DistanceSensor(echo=echo3, trigger=trigger3)
ultrasonic4 = DistanceSensor(echo=echo4, trigger=trigger4)
ultrasonic5 = DistanceSensor(echo=echo5, trigger=trigger5)
ultrasonic6 = DistanceSensor(echo=echo6, trigger=trigger6)
ultrasonic7 = DistanceSensor(echo=echo7, trigger=trigger7)
ultrasonic8 = DistanceSensor(echo=echo8, trigger=trigger8)
ultrasonic9 = DistanceSensor(echo=echo9, trigger=trigger9)
ultrasonic10 = DistanceSensor(echo=echo10, trigger=trigger10)

def cans(distance, level):
    if(level==1):
        if(distance>36):
            return 0
        if(27.5<distance<=36):
            return 1
        if(19<distance<=27.5):
            return 2
        if(14.75<distance<=19):
            return 3
        if(10<distance<14.75):
            return '4-5'
        if(distance<=10):
            return '4-5'
    
    if(level==2):
        if(distance>40):
            return 0
        if(32<distance<=40):
            return 1
        if(24<distance<=32):
            return 2
        if(17<distance<=24):
            return 3
        if(14<distance<17):
            return '4-5'
        if(distance<=13):
            return '4-5'
        

def distances():
    distance1 = round(ultrasonic1.distance*100, 2)
    distance2 = round(ultrasonic2.distance*100, 2)
    distance3 = round(ultrasonic3.distance*100, 2)
    distance4 = round(ultrasonic4.distance*100, 2)
    distance5 = round(ultrasonic5.distance*100, 2)
    distance6 = round(ultrasonic6.distance*100, 2)
    distance7 = round(ultrasonic7.distance*100, 2)
    distance8 = round(ultrasonic8.distance*100, 2)
    distance9 = round(ultrasonic9.distance*100, 2)
    distance10 = round(ultrasonic10.distance*100, 2)
    
    print1 = cans(distance1, 1)
    print2 = cans(distance2, 1)
    print3 = cans(distance3, 1)
    print4 = cans(distance4, 1)
    print5 = cans(distance5, 1)
    print6 = cans(distance6, 2)
    print7 = cans(distance7, 2)
    print8 = cans(distance8, 2)
    print9 = cans(distance9, 2)
    print10 = cans(distance10, 2)
    
    if(print6==None):
        print6='4-5'
    if(print7==None):
        print7='4-5'
    if(print8==None):
        print8='4-5'
    if(print9==None):
        print9='4-5'
    if(print10==None):
        print10='4-5'

    label1.config(text=str(print1)+"   "+str(print2)+"   "+str(print3)+"   "+str(print4)+"   "+str(print5))
    label1.pack()
    label2.config(text=str(print6)+"   "+str(print7)+"   "+str(print8)+"   "+str(print9)+"   "+str(print10))
    label2.pack()
    label3.config(text="More Info: navodjaya28.wixsite.com/foodpantrybox")
    label3.pack()
    button.pack()

    root.after(500, distances)

root.after(0, distances)
root.mainloop()

 

bottom of page