Appendix

Part list (Total Cost: $90.51) Home

No. Item Name From Quantity Cost $
1 Tower Pro Micro Servo 9g Cornell 6 22.74
2 Verdental Stainless Shaker Can Amazon 6 26.62
3 Parallax Continuous Rotation Servo Lab 2 24.18
4 Bowl Zhengning 1 0
5 Furring Strip Lowe's 1 1.88
7 Colored sand Dollar Tree 3 3.24
8 Measuring set Dollar Tree 1 1.08

Here is our code for the user interface and software GPIO control. Enjoy!!!

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#yw348 Yannan Wu
#zh62 Zhengning Han

#import libraries
import pygame
from pygame.locals import *
import RPi.GPIO as GPIO
import time
import numpy as np
import os
import threading
import sys

############INITIALIZE##############################
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')
pygame.init()
pygame.mouse.set_visible(False)

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down = GPIO.PUD_UP)

GPIO.setup(6, GPIO.OUT)  #holder
GPIO.setup(13, GPIO.OUT) #holder
wheel = GPIO.PWM(6, 46.5)  
wheel_blue = GPIO.PWM(13, 46.5)
wheel.start(0)  
wheel_blue.start(0)

GPIO.setup(12, GPIO.OUT) 
GPIO.setup(19, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)

salt = GPIO.PWM(21, 50)
salt.start(5)
sugar = GPIO.PWM(16,50)
sugar.start(5)
msg = GPIO.PWM(12, 50)
msg.start(5)
garlic = GPIO.PWM(5, 50)
garlic.start(5)
ginger = GPIO.PWM(26, 50)
ginger.start(5)
chili = GPIO.PWM(19, 50)
chili.start(5)

black=0,0,0
white=255, 255, 255
blue=0,0,255
red=255,0,0
green = 0, 255, 0
pink = 255,105,180
crimson = 220,20,60
brown = 180,64,16
yellow = 255,255,0
size = width,height=320,240
screen = pygame.display.set_mode(size)
new_start = 1

values = {'salt':0, 'sugar':0, 'msg':0, 'garlic':0, 'ginger':0, 'chili':0}


def GPIO27_callback(channel):
    #emergency quit button
    GPIO.cleanup()
    quit()

############HELPER FUNCTIONS################
def render_background():
    #load the image as background
    screen.fill(black)
    bg = pygame.image.load("background.JPG")
    bgrect = bg.get_rect()
    screen.blit(bg,bgrect)    #combine bg surface with workspace surface

def render_quit():
    #load the quit touch button
    quit_font = pygame.font.Font(None, 50)
    quit_surface = quit_font.render('quit', True, white)
    quit_rect = quit_surface.get_rect(center = (160, 180))
    screen.blit(quit_surface, quit_rect)

def render_cancel():
    #load the cacel touch button
    return_font = pygame.font.Font(None, 50)
    return_surface = return_font.render('X', True, blue)
    return_rect = return_surface.get_rect(center = (310, 110))
    screen.blit(return_surface, return_rect)

def render_ok():
    #load the ok touch button
    return_font = pygame.font.Font(None, 50)
    return_surface = return_font.render('Ok', True, green)
    return_rect = return_surface.get_rect(center = (298, 45))
    screen.blit(return_surface, return_rect)
    
def render_menu(): 
    #laod the entire manu page  
    ingredients = {'salt':(40,30), 'sugar':(40,105), 'msg':(40,180), 'garlic':(200,30), 'ginger':(200,105), 'chili':(200,180)}
    my_font = pygame.font.Font(None, 40)
    value_font = pygame.font.Font(None, 30)
    render_background()
    render_ok()
    render_cancel()
    global values
    i=0

    # render ingredients names and values
    for my_text, text_pos in ingredients.items():
        # add ingredient names
        text_surface = my_font.render(my_text, True, black)
        rect = text_surface.get_rect(center = text_pos)
        screen.blit(text_surface, rect)

        # add ingredient value
        value_surface = value_font.render(str(values[my_text]), True, black)
        value_rect = value_surface.get_rect(center = (text_pos[0], text_pos[1]+30))
        screen.blit(value_surface, value_rect)

        i=i+1

    pygame.display.flip()


def render_amount(name):
    #using the global info the load the correspoding amount value for each ingredient
    render_background()
    name_font = pygame.font.Font(None, 120)
    text_surface = name_font.render(name, True, black)
    rect = text_surface.get_rect(center = (120,80))
    screen.blit(text_surface, rect)
    
    arrow_font = pygame.font.Font(None, 150)

    # add up buttons
    up_surface = arrow_font.render('+', True, red)
    up_rect = up_surface.get_rect(center = (290,100))
    screen.blit(up_surface, up_rect)

    # add down buttons
    down_surface = arrow_font.render('-', True, blue)
    down_rect = down_surface.get_rect(center = (290,220))
    screen.blit(down_surface, down_rect)

    # add ingredient values
    value_surface = name_font.render(str(values[name]), True, black)
    value_rect = value_surface.get_rect(center = (120,170))
    screen.blit(value_surface, value_rect)

    render_ok()
    
    pygame.display.flip()
    

def render_ingredient(name):
    render_amount(name)
    while True:
        if GPIO.event_detected(17):
            return
        elif GPIO.event_detected(22):
            values[name] = values[name]+1
            render_amount(name)
        elif GPIO.event_detected(27):
            if(values[name] > 0 ):
                values[name] = values[name]-1
                render_amount(name)
                
def render_dispensing():
    #load the dispensing page
    render_background()
    font = pygame.font.Font(None, 60)
    surface = font.render('DISPENSING...', True, yellow)
    rect = surface.get_rect(center = (160, 120))
    screen.blit(surface, rect)
    pygame.display.flip()
    time.sleep(3)
    
            
           
def next_bottle():
    #move the bottom plate to next bottle
    wheel.start(5)  
    wheel_blue.start(5)
    time.sleep(0.48)
    wheel.start(0)  
    wheel_blue.start(0)
        


def new_round():
    #move the bottom plate back to the initial position 
    wheel.ChangeDutyCycle(10)  
    wheel_blue.ChangeDutyCycle(10)
    time.sleep(2.7)  
    wheel_blue.start(0)
    wheel.start(0)

        
def start_h():
    #manu page 
    salt.start(0)
    sugar.start(0)
    msg.start(0)
    ginger.start(0)
    garlic.start(0)
    chili.start(0)
    render_menu()
    while True:
        # dispense according to input amount
        if GPIO.event_detected(17):
            render_dispensing()
            for i in range(0, values['salt']):                       
                salt.ChangeDutyCycle(10)
                time.sleep(0.1)
                salt.ChangeDutyCycle(5)
                time.sleep(1)
                salt.ChangeDutyCycle(0)
            time.sleep(1)
            next_bottle()
            time.sleep(1)
            for i in range(0, values['sugar']):                       
                sugar.ChangeDutyCycle(10)
                time.sleep(0.1)
                sugar.ChangeDutyCycle(5)
                time.sleep(1)
                sugar.ChangeDutyCycle(0)
            time.sleep(1)
            next_bottle()
            time.sleep(1)
            for i in range(0, values['msg']):                       
                msg.ChangeDutyCycle(10)
                time.sleep(0.1)
                msg.ChangeDutyCycle(5)
                time.sleep(1)
                msg.ChangeDutyCycle(0)
            time.sleep(1)
            next_bottle()
            time.sleep(1)
            for i in range(0, values['garlic']):                       
                garlic.ChangeDutyCycle(10)
                time.sleep(0.1)
                garlic.ChangeDutyCycle(5)
                time.sleep(1)
                garlic.ChangeDutyCycle(0)
            time.sleep(1)
            next_bottle()
            time.sleep(1)
            for i in range(0, values['ginger']):                       
                ginger.ChangeDutyCycle(10)
                time.sleep(0.1)
                ginger.ChangeDutyCycle(5)
                time.sleep(1)
                ginger.ChangeDutyCycle(0)
            time.sleep(1)
            next_bottle()
            time.sleep(1)
            for i in range(0, values['chili']):                       
                chili.ChangeDutyCycle(10)
                time.sleep(0.1)
                chili.ChangeDutyCycle(5)
                time.sleep(1)
                chili.ChangeDutyCycle(0)
            time.sleep(1)
            new_round()
            return
                           
        elif GPIO.event_detected(22):
            return
        for event in pygame.event.get():
            if (event.type is MOUSEBUTTONDOWN):
                pos = pygame.mouse.get_pos()
            elif (event.type is MOUSEBUTTONUP):
                pos = pygame.mouse.get_pos()
                x,y = pos
                #touch button detections for each ingredient
                if x>=0 and x<=70 and y>=0 and y<=65:
                    render_background()
                    render_ingredient('salt')
                    render_menu()
                elif x>=0 and x<=70 and y>=70 and y<=135:
                    render_background()
                    render_ingredient('sugar')
                    render_menu()
                elif x>=0 and x<=70 and y>=150 and y<=210:
                    render_background()
                    render_ingredient('msg')
                    render_menu()
                elif x>=160 and x<=230 and y>=0 and y<=65:
                    render_background()
                    render_ingredient('garlic')
                    render_menu()
                elif x>=160 and x<=230 and y>=70 and y<=135:
                    render_background()
                    render_ingredient('ginger')
                    render_menu()
                elif x>=160 and x<=230 and y>=150 and y<=210:
                    render_background()
                    render_ingredient('chili')
                    render_menu()                    
    
############HOME PAGE################################                   

def main():
    GPIO.add_event_detect(27, GPIO.FALLING, bouncetime=300)
    GPIO.add_event_detect(17, GPIO.FALLING, bouncetime=300)
    GPIO.add_event_detect(22, GPIO.FALLING, bouncetime=300)
    GPIO.add_event_detect(23, GPIO.FALLING, callback=GPIO27_callback, bouncetime=300)
    try:
        global new_start
        global values
        render_background()
        render_quit()
        start_font = pygame.font.Font(None, 100)
        start_surface = start_font.render('start', True, black)
        start_rect = start_surface.get_rect(center = (160, 80))
        screen.blit(start_surface, start_rect)
        pygame.display.flip()   #display workspace on screen
        while(True):
            if(new_start):
                for event in pygame.event.get():
                    if (event.type is MOUSEBUTTONDOWN):
                        pos = pygame.mouse.get_pos()
                    elif (event.type is MOUSEBUTTONUP):
                        pos = pygame.mouse.get_pos()
                        x,y = pos
                        if x>= 120 and x<= 200 and y>=80 and y<=160:
                            new_start = 0
                            start_h() #enter the manu page
                            render_background() #prepare for a new start
                            render_quit()
                            start_font = pygame.font.Font(None, 100)
                            start_surface = start_font.render('start', True, black)
                            start_rect = start_surface.get_rect(center = (160, 80))
                            screen.blit(start_surface, start_rect)
                            pygame.display.flip()   #display workspace on screen                            
                            new_start = 1
                            values = {'salt':0, 'sugar':0, 'msg':0, 'garlic':0, 'ginger':0, 'chili':0}
                        elif x>= 120 and x<= 200 and y>=140 and y<=220:
                            GPIO.cleanup()
                            return
        
    except Exception as e:
        GPIO.cleanup()
        print(e)


if __name__=="__main__":
    main()



        
            
Home

Zhengning Han zh62@cornell.edu          cornell         Yannan Wu yw348@cornell.edu