zealen
Posts: 8
Joined: Thu Jul 28, 2016 8:14 pm

A beginner needs help with the last function of the code

Mon Oct 10, 2016 8:21 pm

I am building a box that opens every 5 kilo i loose in weight. I have a button on the box and a screen. For every button press one hekto is subtracted from the 5 kilo on the screen. But I need help with what happens when it reaches 0. When the screen reaches 0 I want the servo to open and then when I press one more time and the screen reaches -0.1 I want the servo to close again and the number restore to 5 again so I can start over. I have tried to write this function in several different ways, but have not got it to work yet, I just started with programing and am a bit lost now. When the value reaches 0 it just continue to -0.1, -0.2 and so on, nothing happens.

How should I write the last part?

Code: Select all

# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time
from Adafruit_LED_Backpack import SevenSegment

display = SevenSegment.SevenSegment()
display.begin()
colon = False

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) #knapp

GPIO.setup(18,GPIO.OUT) #servo
GPIO.setup(5,GPIO.OUT)
GPIO.setup(6,GPIO.OUT)


p = GPIO.PWM(18,50)

p.start(12)


hekto = 4.9

GPIO.output(6, True)

numbers = [5.0]

input_state = GPIO.input(17)

print('Press Ctrl-C to quit.')
while True:
    for i in numbers:
        display.clear()
        display.print_float(i, decimal_digits=2)
        display.set_colon(colon)
        display.write_display()
        time.sleep(0.2)
        input_state = GPIO.input(17)
        if input_state == False:
            numbers[0] = hekto
            hekto -= 0.1
            print('knapp')
        
            if numbers[0] == 0.0:       #open servo
                p.ChangeDutyCycle(12.5)

            if numbers[0] == -0.1:      #close servo reset number 
                p.ChangeDutyCycle(3.5)
                numbers[0] = 5.0 
        

User avatar
paddyg
Posts: 2393
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: A beginner needs help with the last function of the code

Mon Oct 10, 2016 8:46 pm

you're setting numbers[0] to hekto but not resetting hekto when you set numbers[0] back to 5.0. I don't really understand why you have a list of only one value and why you duplicate it in hekto. There doesn't seem to be functionality to append to numbers so not clear why list and why for loop. Does it do some kind of pulse counting?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: A beginner needs help with the last function of the code

Mon Oct 10, 2016 9:02 pm

paddyg wrote:you're setting numbers[0] to hekto but not resetting hekto when you set numbers[0] back to 5.0. I don't really understand why you have a list of only one value and why you duplicate it in hekto. There doesn't seem to be functionality to append to numbers so not clear why list and why for loop. Does it do some kind of pulse counting?
Also numbers[0] never equals -0.1

Code: Select all

            if numbers[0] == -0.1:      #close servo reset number 
instead just check if numbers[0] is less than 0

Code: Select all

            if numbers[0] < 0:      #close servo reset number 

Return to “Python”