Python Forum
Count to movement according to the time pressed button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count to movement according to the time pressed button
#1
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)
	
Reply
#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) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,327 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  how to make the movement humanized isv 4 1,557 Jul-29-2022, 07:14 PM
Last Post: XavierPlatinum
  Clicker count increase by 1 each time blakefindlay 1 5,684 Feb-03-2021, 03:50 PM
Last Post: deanhystad
  Getting button pressed number Moris526 4 2,437 Dec-14-2020, 01:41 AM
Last Post: Moris526
  How to get continuous movement whilst using State Machine cjoe1993 2 1,824 Dec-10-2020, 06:36 AM
Last Post: cjoe1993
  How many times was the button pressed in pyglet rama27 0 1,919 Oct-10-2020, 10:26 AM
Last Post: rama27
  The count variable is giving me a hard time in this code D4isyy 2 1,975 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  run different functions each time the same button is pressed? User3000 6 3,309 Jul-31-2020, 11:11 PM
Last Post: User3000
  Moving mouse so that games can detect movement SheeppOSU 2 1,949 Jun-09-2020, 07:23 PM
Last Post: SheeppOSU
  need help on solving and movement Tankey2630 1 1,682 Jan-09-2020, 11:04 AM
Last Post: Tankey2630

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020