Python Forum

Full Version: Count to movement according to the time pressed button
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I need to develop a project with the codes I share with you.The details of the project are as follows.Could you help me about this issue?

button pressed for 9 seconds, move = 0
button pressed for more than 10 seconds, move = 1
and count should be every 10 seconds as a separate move.
As a result, Could you help me to keep the log as time and movement?(example :21/02/2019 - 22:17 - 10 second - move: 1)

The application should be waiting for the button to be pressed when it is opened.

the values on the first start-up will be as follows:

n = 0 number of moves
t = 0 time counter

n = n + 1 if each button is pressed t = 10 seconds

hold button t = 3 seconds n = 0
Press and hold the button t = 11 seconds n = 1
hold button t = 15 seconds n = 2
n = 2 value will not change when t = 2 seconds pressed


hold button t = 20 seconds n = 3
hold button t = 15 seconds n = 4
hold button t = 14 seconds n = 5
n = 5 value will not change when t = 5 seconds pressed

When the program is run and the button is pressed, it must record every movement as log in the example below.
n = 1 18.02.2018 - 08:01:12, n = 2 18.02.2018 - 08:01:27


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(7, GPIO.IN, GPIO.PUD_UP)

while True:
    # waiting for switch on
    print " waiting for first button press"
    while GPIO.input(7) == 1:
		time.sleep(0.2)
    else:
        on = time.time()
        start = (time.strftime("%a %b %d %Y %H:%M:%S"))

        print "switch on at ", start
          
    time.sleep(2)
    # waiting for switch off
    print " waiting for second button press"
    while GPIO.input(7) == 1:
		time.sleep(0.2)
    else:
        off = time.time()
        stop = (time.strftime("%a %b %d %Y %H:%M:%S"))

        difference = off - on
        
        print "switch off at ", stop
        print "time since switch on"
        seconds = difference
        minutes = seconds // 60
        hours = minutes // 60
        delay = "%02d:%02d:%02d" % (hours, minutes % 60, seconds % 60)
        print delay
        
        print " "

        
        result = open("/home/pi/times.txt","a")
        result.write("switch on " + start + "\n")
        result.write("switch off " + stop + "\n")
        result.write("time between button presses " + delay + "\n")
        result.write(" \n")
        result.close()        
	time.sleep(2)
	
Hi,

I need to develop a project with the codes I share with you.The details of the project are as follows.Could you help me about this issue?

button pressed for 9 seconds, move = 0
button pressed for more than 10 seconds, move = 1
and count should be every 10 seconds as a separate move.
As a result, Could you help me to keep the log as time and movement?(example :21/02/2019 - 22:17 - 10 second - move: 1)

def my_callback(channel):
    global start
    global end
    if GPIO.input(25) == 1:
        start = time.time()
    if GPIO.input(25) == 0:
        end = time.time()
        elapsed = end - start
        print(elapsed)

  GPIO.add_event_detect(25, GPIO.BOTH, callback=my_callback, bouncetime=200)