Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to do a project with python 2 Its a Newspaper dispener. I\'m haveing a ha

ID: 3573248 • Letter: I

Question

I have to do a project with python 2 Its a Newspaper dispener. I'm haveing a hard time getting my lcd to add the number right. I don't no what I did any cant seem to fix it, here what I have so far it takes .5, .10, and .25 cents

import time

from time import sleep

import Adafruit_CharLCD as LCD

lcd = LCD.Adafruit_CharLCDPlate()

NICKEL = 5

DIME = 10

QUARTER = 25

total = 0

change = 0

x = .30

def print_total():

            lcd.clear()

            lcd.message('    Total: $' + str(total)[1:])

           

def print_final():

            lcd.clear()

            lcd.message('    Total: $' + str(total)[1:] + '    Change: $' + str(change)[1:])

            sleep(1)         

                       

#---------------------------------------------------------

while(1):

            lcd.clear()

            lcd.message("   Today's Paper    Only 30 Cents ")

                       

            while(total < x):

                        if lcd.is_pressed(LCD.LEFT):

                                    total = total + NICKEL

                                    print_total()

                        elif lcd.is_pressed(LCD.RIGHT):

                                    total = total + DIME

                                    print_total()

                                   

                        elif lcd.is_pressed(LCD.DOWN):

                                    total = total + QUARTER

                                    print_total()

              

            change = total-0.30

           

            for i in range(5):

                        print_final()

                        lcd.clear()

                        lcd.set_color(0,1,0)   

                        lcd.message("      Enjoy     Your Paper")

                        sleep(1)

   

            total = 0

            change = 0

Explanation / Answer

Hello,

The function lcd.is_pressed(<button>) would return the state of the button that is pressed or not. However the number of times this function is called would depend on the clock speed of the processor on which this code is running. This means that the if elif block inside the loop "while(total < x):" is probably getting executed more than once every millisecond. Thus when LCD.LEFT is pressed, the code "total = total + NICKEL" is executed more than once. This is because in all practical cases the button is pressed longer than the time required to capture the input. To correct this we use a technique called debouncing. With software debouncing we need to specify the time range for which a button must be pressed for it to be counted as one press and not more than one press.

In this code, the change would be to wait for 0.3 sec or 0.5 sec (which ever works best for the system) before accepting the next input. Even with computer keyboards, this is a configurable number, as to when does a long press of a button makes the input as "aaaaaaaaa" instead of "a"

The code would look like the following (changes in bold and italics):

[You can try and see which value in sleep works the best instead of a 0.3s delay]

import time

from time import sleep

import Adafruit_CharLCD as LCD

lcd = LCD.Adafruit_CharLCDPlate()

NICKEL = 5

DIME = 10

QUARTER = 25

total = 0

change = 0

x = .30

def print_total():

            lcd.clear()

            lcd.message('    Total: $' + str(total)[1:])

           

def print_final():

            lcd.clear()

            lcd.message('    Total: $' + str(total)[1:] + '    Change: $' + str(change)[1:])

            sleep(1)         

                       

#---------------------------------------------------------

while(1):

            lcd.clear()

            lcd.message("   Today's Paper    Only 30 Cents ")

                       

            while(total < x):

                        if lcd.is_pressed(LCD.LEFT):
   sleep(0.3)

                                    total = total + NICKEL

                                    print_total()

                        elif lcd.is_pressed(LCD.RIGHT):
   sleep(0.3)

                                    total = total + DIME

                                    print_total()

                                   

                        elif lcd.is_pressed(LCD.DOWN):
   sleep(0.3)

                                    total = total + QUARTER

                                    print_total()

              

            change = total-0.30

           

            for i in range(5):

                        print_final()

                        lcd.clear()

                        lcd.set_color(0,1,0)   

                        lcd.message("      Enjoy     Your Paper")

                        sleep(1)

   

            total = 0

            change = 0