0

Picture 1 shows my setup. I connected the RPi3 Input parallel to an I/O terminal. The inputs are connected to a pull down resistor setup. Pictures 2 and 3 show the setup more in detail.

Program-> event detection: printing Pin number and datetime while sensor triggered Checking the pins simultaneously.

Pictures 4 and 5 show the result. Picture 1: enter image description here Picture 2: enter image description here Picture 3: enter image description here The first few lines show one sensor triggered each. Then 7 and 12 together. Picture 4: enter image description here 16 and another one together just show 16 alone. All together stops the print. Picture 5: enter image description here First program:

import threading
import RPi.GPIO as GPIO
import time
import time
from datetime import datetime


PIN = 7
PIN2 = 12
PIN3 = 16

class GPIOThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)              

    def run(self):
        while True:
            while (GPIO.input(PIN) == True): # adjust this statement as per your pin status i.e HIGH/LOW

                print("PIN 7",str(datetime.now()))
                time.sleep(0.1)

            while GPIO.input(PIN2) == True: # adjust this statement as per your pin status i.e HIGH/LOW

                print("PIN 12",str(datetime.now()))
                time.sleep(0.1)

            while GPIO.input(PIN3) == True: # adjust this statement as per your pin status i.e HIGH/LOW

                print("PIN 16",str(datetime.now()))
                time.sleep(0.1)


def main():

      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(PIN,GPIO.IN,pull_up_down=GPIO.PUD_UP)    
      GPIO.setup(PIN2,GPIO.IN,pull_up_down=GPIO.PUD_UP)
      GPIO.setup(PIN3,GPIO.IN,pull_up_down=GPIO.PUD_UP) 
      GPIO.add_event_detect(PIN,GPIO.RISING)

      gpio_thread = GPIOThread()
      gpio_thread.start() 




if __name__=="__main__":
    main()

Corrected statechange code (now as it is on the RPi):

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
from time import sleep

GPIO.setup(7, GPIO.IN) 
GPIO.setup(8, GPIO.IN)
GPIO.setup(12, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)


buttonstate0 = 0;
lastbuttonstate0 = 0;
buttonstate1 = 0;
lastbuttonstate1 = 0;
buttonstate2 = 0;
lastbuttonstate2 = 0;
buttonstate3 = 0;
lastbuttonstate3 = 0;
buttonstate4 = 0;
lastbuttonstate4 = 0;
buttonstate5 = 0;
lastbuttonstate5 = 0;

while True:
    buttonstate0 = GPIO.input(7)
    buttonstate1 = GPIO.input(8)
    buttonstate2 = GPIO.input(12)
    buttonstate3 = GPIO.input(16)
    buttonstate4 = GPIO.input(20)
    buttonstate5 = GPIO.input(21)

    if buttonstate0 !=lastbuttonstate0:
        if buttonstate0 == 1:
            print("Sensor1 an")
        sleep(0.1)
    buttonstate0 = lastbuttonstate0

    if buttonstate1 !=lastbuttonstate1:
        if buttonstate1 == 1:
            print("Sensor2 an")
        sleep(0.1)
    buttonstate1 = lastbuttonstate1

    if buttonstate2 !=lastbuttonstate2:
        if buttonstate2 == 1:
            print("Sensor3 an")
        sleep(0.1)
    buttonstate2 = lastbuttonstate2

    if buttonstate3 !=lastbuttonstate3:
        if buttonstate3 == 1:
            print("Sensor4 an")
        sleep(0.1)
    buttonstate3 = lastbuttonstate3

    if buttonstate4 !=lastbuttonstate4:
        if buttonstate4 == 1:
            print("Sensor5 an")
        sleep(0.1)
    buttonstate4 = lastbuttonstate4

    if buttonstate5 !=lastbuttonstate5:
        if buttonstate5 == 1:
            print("Sensor6 an")
        sleep(0.1)
    buttonstate5 = lastbuttonstate5

How can i implement, printing out the current input state once it goes high?

  • I want to check the sensors (inputs) state simultanously. The second program print out the state of one sensor continuously when triggered, but has to print it out ones. But no program can do it all at ones. – Steven Nov 30 '17 at 15:55
  • As both answers now show, your code is wrong in the second one - You're overwriting all of the pin inputs except 21. If that's an error in what you posted here that's not in your actual system, please revise your question carefully. If it's accurate to your system, then the code is clearly broken. – Brick Nov 30 '17 at 16:05
0

May I turn the question back on you?

What does the following code do?

buttonstate = GPIO.input(7)
buttonstate = GPIO.input(8)
buttonstate = GPIO.input(12)
buttonstate = GPIO.input(16)
buttonstate = GPIO.input(20)
buttonstate = GPIO.input(21)
  • The current input state is saved in the variables. I forgot the numbers buttonstate0..buttonstate1 etc here. The current state is than compared to the new state. If it changed, the program prints out a message – Steven Nov 30 '17 at 15:57
  • All the errors so far have been in the software. You need to ensure any listings you give are correct. There is no point in looking at this question until you have given assurances that the posted code is what you are using. – joan Nov 30 '17 at 16:36
  • I checked the code again. I corrected it in the question i posted here. I didn't forget to change the buttonstate for each input in the code on the RPi3 – Steven Nov 30 '17 at 16:48
0

You have this code in your second program:

buttonstate = GPIO.input(7)
buttonstate = GPIO.input(8)
buttonstate = GPIO.input(12)
buttonstate = GPIO.input(16)
buttonstate = GPIO.input(20)
buttonstate = GPIO.input(21)

So you keep writing to the variable buttonstate. At the end of these statements you have the state of pin 21. None of the other pins influence the outcome of the program.

As far as simultaneously reading all of the sensors: That cannot be done with infinite precision. If you do true multithreading, you can get it pretty close, but only up to the number of cores that you have on your machine and still subject to misalignment in where the various threads are in the execution path due to thread scheduling at the OS level.

It's also worth noting that you cannot do true multithreading in Python (at least with the default interpreter) because of limitations at the interpreter level. Depending on what you're doing and how precisely you want to make this read simultaneously, that may or may not matter in practical terms.

  • You can read GPIO 0-31 simultaneously. The hardware supports such usage as does my pigpio library. abyz.me.uk/rpi/pigpio/python.html#read_bank_1 – joan Nov 30 '17 at 16:04
  • @joan In a single clock cycle? That's interesting if true, but in any case requires programming lower-level / through a different API call than what's shown by the OP. The OP seems to be confused about software issues related to threading, as I understand the question. – Brick Nov 30 '17 at 16:07
  • 1
    The GPIO levels are held in two 32-bit hardware registers. Bank 1 for GPIO 0-31, bank 2 for GPIO 32-53. So all that is required is a 32-bit read which is single instruction. – joan Nov 30 '17 at 16:11
  • I tried to change the resistors from e.g. 10k to 5k. The result: I can check the sensors each or none. I don't want to check all GPIO 0-31. When i check a given number 00000100010101 for the GPIO i have no idea what the input for the touchscreen would look like. – Steven Nov 30 '17 at 16:11
  • @Steven I suggest trying to reduce your problem to something simpler and revising this question down to what you want. It's now completely unclear what you're asking, and it appears that you're changing the question as you go. – Brick Nov 30 '17 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.