fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

RISING & FALLING

Thu Nov 16, 2017 4:54 pm

I'm studing input status capure with python3
i'm using the event mode capture but doesnt' work fine.

i want with this code show pin status from 0 to 1 for Rising and don't show from 1 to 0
instead this casual sometime work fine and other not
with Falling is the same

pin= 4
Input= 1
loop
loop
pin= 4
Input= 1
loop
loop
loop
pin= 4
Input= 1
loop
loop
loop
pin= 4
Input= 1
loop
loop
loop
pin= 4
Input= 1
loop
loop
pin= 4
Input= 0
loop
loop
pin= 4
Input= 1
loop
loop
pin= 4
Input= 0
loop
pin= 4
Input= 1

Code: Select all

#! /usr/bin/python
import time                                         # Importa libreria per la gestione dei timer

# Inizializzazione GPIO
import RPi.GPIO as gpio                             # Importa libreria GPIO
gpio.setmode(gpio.BCM)                              # Numerazione BCM
gpio.setwarnings(False)                             # Disattiva messaggi d'avviso

PinStart = 4

gpio.setup(PinStart, gpio.IN, pull_up_down=gpio.PUD_DOWN)  # GPIO4 input, abilita pull-down

# Funzioni
def cambia_stato(pin):
        global lampeggio                                # Importa la variabile globale lampeggio
        print("pin=", pin)
        print("Input=", gpio.input(4) )

# Interrupt
gpio.add_event_detect(PinStart, gpio.RISING, callback=cambia_stato, bouncetime=200)

print("Ready!")

# Ciclo infinito
while 1:
    print ("loop")
    time.sleep(0.5)                                 # Aspetta 0.5s

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Thu Nov 16, 2017 7:36 pm

It possible that by the time you do the read of the gpio.input within your function the input has already changed for high to low, because the button has already been released.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Thu Nov 16, 2017 7:42 pm

i think no

is a 2-state switch

i change state each 2-4 seconds

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Thu Nov 16, 2017 7:43 pm

i have tried to change bouncetime to 800, 1600, 3000

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Thu Nov 16, 2017 7:56 pm

You are using gpio 4 which is also the default pin for 1-wire interface. Have you got the 1-wire interface Active by any chance.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Thu Nov 16, 2017 8:26 pm

i have change pin to 22 pin 15 and 3,3 pin 17

i have tried with a 330 resistor and without
i have tried with PI1 and PI3

it's the same or similar

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Thu Nov 16, 2017 8:51 pm

Have you tried replacing you existing switch and replacing it with a simple push button switch, to see if it works then.

You seemed to have eliminated the pi it's self being the problem as you have tried more than one so would suggest a problem external to the pi.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Fri Nov 17, 2017 4:41 pm

change the button.

is it correct that with RISING parameters the callback function is called for 1 and 0 state?

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Fri Nov 17, 2017 6:13 pm

You can set it to call back on a rising edge or a falling edge and both.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Fri Nov 17, 2017 9:36 pm

but in rising mode, when i pass from 1 to 0, i must receive a ONLY event or i must more events while the input return to 1?

now i have reset raspian os and now work in this mode:

level 1 = no event
level 0 = event
level 0 = event
level 0 = event
level 0 = event
level 1 = no event

is it correct?

i have done a script in python 2 and 3

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Fri Nov 17, 2017 9:41 pm

i think that some thinks not work fine, if i set falling or both, the result is the same

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Fri Nov 17, 2017 9:57 pm

Well if you set both then it is the same as setting on rising and falling.
So setting it to falling or both will produce the same results.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Fri Nov 17, 2017 10:37 pm

ok, but why generate more events when is RISING?
i wait ONLY an event.

i must read a impulse input, i think of count each step from 1 to 0 for each event
if callback is called more times on 1 state i cannot use the callback event for count the impulse

exist a other method or i haven't undenstant the feature of callback function?

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

Re: RISING & FALLING

Sat Nov 18, 2017 8:39 am

I have had problems that looked similar where I had assumed my switch gave a simple step change but it actually had several on/off events in the microsecond or so between one state and the next. You can check this by having an ultra simple loop that writes gpio state to a file

Code: Select all

with open('log.txt', 'w') as f:
  while True:
    f.write(time.time() + ' ' + gpio.input(4) + '\n')
The file will become massive very quickly so you need to run it and use the switch very quickly. Maybe use a for loop instead of while.
Last edited by paddyg on Sat Nov 18, 2017 10:59 am, edited 1 time in total.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Sat Nov 18, 2017 10:17 am

So using paddyg suggestion I did some testing using this code

Code: Select all

#! /usr/bin/python
import time                                        


import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
count =0
PinStart = 21

GPIO.setup(PinStart, GPIO.IN)


def my_callback(PinStart):
        global count
        print "input"
        print time.time()
        count = count + 1
# Interrupt
GPIO.add_event_detect(PinStart, GPIO.RISING, callback=my_callback, bouncetime=400)

print("Ready!")


while count < 10:
    print "loop", count
    time.sleep(0.2)

print "finish ", count
my switch has a physical resistor pulling it down and pressing the switch causes a high input.

running this I can get a reliable input from my push butting with 10 presses equalling 10 inputs.

so then I tried using the pi to drive the the input.

connected gpio 21 to gpio 26 using a 1K resistor using this code to produce the pulses.

Code: Select all

#! /usr/bin/python
import time                                        


import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

out = 26
GPIO.setup(out, GPIO.OUT)
print "start"
for x in range(1,11):
	GPIO.output(out, GPIO.HIGH)
	time.sleep(0.2)
	GPIO.output(out, GPIO.LOW)
	time.sleep(0.2)
    
print "finish"
so I had 2 ssh connections running on the one pi started the input capture program then ran the pulse program and even with just a 0.2 second pulse I was still able to record 10 pulses.

so I would say that proves its a combination of your switch bouncing and your code which is doing a read of the input pin after the edge detection, when you have no idea if the input pin is still in the same state it was at edge detection time.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: RISING & FALLING

Sat Nov 18, 2017 10:57 am

If the problem is a 'dirty' edge then you should be able to wait a short while in your callback function. This hypothesis would be consistent with your 'input=0' prints being after a switch to 1 but happening to be sampled 'on a bounce'. Like this (but inverted!):
Untitled.jpg
Untitled.jpg (26.34 KiB) Viewed 1966 times
It would be easy to test by putting in a time.sleep(0.2) (then fine tune by trial and error) before your gpio.input() call
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Sat Nov 18, 2017 7:45 pm

with sleep is a bit better

i have also 2 problems

https://www.youtube.com/watch?v=64uZuQj9McI


set english subtitle

1) why when chage the input state the callback is called again and again?

2) why when i return back state, sometimes the callback is called also for state 1?

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Sat Nov 18, 2017 8:09 pm

If you toggle switch has a bouncing contact then you will get more than one even trigger.

You are probably getting the 1 returned because you have event triggering which is detecting multiple events and by the time you have done all the second reads of the inputs the switch has gone back to the other state.

Try running my code using your switch and then you can see what the time difference is between events.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Sat Nov 18, 2017 8:32 pm

Output code with switch to 1

why callback the function for 10 times?
for me, work fine when call callback function a time for each transintion from 0 to 1
i have undenstand good?

Code: Select all

 python InputEventForum1.py
Ready!
loop 0
input
1511036973.5
loop 1
input
1511036973.9
 loop 1
loop 2
input
1511036974.3
loop 3
loop 3
input
1511036974.7
loop 4
loop 4
input
1511036975.1
loop 5
loop 5
input
1511036975.51
loop 6
loop 6
input
1511036975.91
loop 7
loop 7
input
1511036976.31
loop 8
loop 8
input
1511036976.71
loop 9
loop 9
input
1511036977.11
finish  10

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Sat Nov 18, 2017 9:01 pm

I just wrote the code so that I knew that after 10 triggers it would stop. That way if I pressed the button 10 times and got 10 triggers all was good but if I pressed the button say 8 times but got 10 responses I had a problem with contact bounce.

If you change the number in the while line

Code: Select all

 
while count < 10:
You can change the number of triggers to what ever you like.

If you make it while True: code will run for ever.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Sun Nov 19, 2017 5:54 am

I want a single event when change input status
In this case with 10 times, I must change status 10 times for exit.
Now instead when change a single status I receive 10 events without change phisical status

I want this feature if use ringing event, a single event when change status from 0 to 1.
Now I have events until state is 1

pcmanbob
Posts: 4544
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: RISING & FALLING

Sun Nov 19, 2017 9:56 am

Sorry but I don't understand what what exactly it is you want.

may be english is not your native language if this is the problem may be you would be better asking the question here viewforum.php?f=90 selecting you native language.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

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

Re: RISING & FALLING

Sun Nov 19, 2017 1:59 pm

Hi @fishwolf, it's tricky for us to track down the solution but it's also hard for you without more data (as you are finding). My approach would be a) try to find exactly what happens each time the switch makes or breaks. That's what my previous post was, basically run as simple a program as possible and see what you get (this is a slightly modified version to make a smaller file).

Code: Select all

#! /usr/bin/python
import time 
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
#PinStart = whatever you're using 
gpio.setup(PinStart, gpio.IN, pull_up_down=gpio.PUD_DOWN)
with open('log.txt', 'w') as f:
  last_state = 0
  for i in range(10000000):
    state = gpio.input(PinStart)
    if state != last_state:
      f.write('{} -> {}\n'.format(time.time(), state)
    last_state = state
#finally gpio tidy up
b) if there are lots of changes as the switch makes or breaks then I would use the system you already do but b.1) delay the re-measurement of the pin state b.2) keep track of the previous state and only count changes from 0 to 1 i.e. 1 to 1 would be ignored. c) if the changes happen randomly when the switch is either off or on then I would check the wiring, also bits of wire can act as aerials and pick up voltage fluctuations from the 'ether'. These random fluctuations should be filterable by delayed re sampling in the callback function.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: RISING & FALLING

Sun Nov 19, 2017 3:28 pm

i had a similar issue with the bounce time and used a capacitor accross the GPIO to smooth the input, (maybe a 0.33uf capacitor but dont remember exactly)
for me the issue was opening and closing relays on the 5v line was causing a voltage drop across the entire raspberry, i put a 4.5F cap on my 5v rail to smooth that out also.

in the end i used GPIO high / low within its own thread running a 0.1 sleep time so rather than looking for a falling edge im looking for a low or high.
a falling edge could be created on the input signal by numerous outside sources including the 5v power supply and a falling edge on the 3.3 rail could appear without the rail actually dropping to 0v.

fishwolf
Posts: 41
Joined: Fri Sep 13, 2013 8:33 am

Re: RISING & FALLING

Mon Nov 20, 2017 8:52 pm

I'm not getting any answers on my local forums, I'll try to explain it better.

What I want to do is to count the number of impulses in a sequence (how many times it changes from 0 to 1).
I'm trying to use the RPI.GPIO Python library and callback functions.
When the state is 0 I get no events, but when it's 1 I get continous events until it goes back to 0 where it should only create one event on the change of state.
The library's documentation says it's possible to do it but I'm not sure what I'm doing wrong, either in the code or just in understanding it.

Image
Attachments
Inpulse.jpg
Inpulse.jpg (11.73 KiB) Viewed 1698 times

Return to “Python”