Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Write data just when the output changes

Fri Jun 12, 2020 6:25 am

Hi, I have a problem with my code in python. I use E18 D80NK proximity sensor for counting item. I need to rwrite output just when the output changes from 1 ti 0 or 0 to 1. My code now writes every time when sensor read the input. Example:
Menunggu Proses ke - 1
Menunggu Proses ke - 1
Menunggu Proses ke - 1
Menunggu Proses ke - 1
Proses Tapper Roll ke 1
Proses Tapper Roll ke 1
Proses Tapper Roll ke 1
Proses Tapper Roll ke 1
Proses Tapper Roll ke 1
Menunggu Proses ke - 2
Menunggu Proses ke - 2
Menunggu Proses ke - 2
Menunggu Proses ke - 2
Menunggu Proses ke - 2
Menunggu Proses ke - 2
Menunggu Proses ke - 2
Proses Tapper Roll ke 2
Proses Tapper Roll ke 2
Menunggu Proses ke - 3
Menunggu Proses ke - 3
Menunggu Proses ke - 3
Menunggu Proses ke - 3
Menunggu Proses ke - 3
Menunggu Proses ke - 3
Menunggu Proses ke - 3
Menunggu Proses ke - 3



and i need to write like just like this (only when the input change from 0 to 1::

Proses Tapper Roll ke 1
Proses Tapper Roll ke 2
Proses Tapper Roll ke 3
Proses Tapper Roll ke 4






This is my code for counting:


Code: Select all

mport RPi.GPIO as GPIO
import time

GPIN = 25
Pinstate = False
counting = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

try:

  while True:

    if GPIO.input(GPIN) and not Pinstate:
      counting += 1
      Pinstate = True

    elif not GPIO.input(GPIN) and Pinstate:
        Pinstate = False

    time.sleep(1)
    print (('Menunggu Proses ke - %s' if Pinstate else 'Proses Tapper Roll ke %s') % counting)

except KeyboardInterrupt:
        GPIO.cleanup()


Do you guys have any idea for solving my problem?

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

Re: Write data just when the output changes

Fri Jun 12, 2020 8:02 am

Try this variation of your code.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIN = 25
Pinstate = False
counting = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

try:
    print (('Proses Tapper Roll ke %s') % counting)
    while True:

        if GPIO.input(GPIN):
            counting += 1
            print (('Proses Tapper Roll ke %s') % counting)

            while GPIO.input(GPIN) == 1:
                time.sleep(0.1)
            

        time.sleep(1)
    

except KeyboardInterrupt:
    GPIO.cleanup()
    
    
the code now waits for the input to change from 1 to 0 before continuing.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jun 12, 2020 8:13 am

Why if I run your code I get this from the first execute before my sensor sense?

Proses Tapper Roll ke 0
Proses Tapper Roll ke 1



Can we start from "Proses Tapper Roll ke - 1"?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Write data just when the output changes

Fri Jun 12, 2020 8:23 am

There is a line in the code that outputs the count before the GPIO pins are checked and the count is increased.
Shouldn't be difficult for you to remove that print statement.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

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

Re: Write data just when the output changes

Fri Jun 12, 2020 8:42 am

To remove the first print just remove this line

Code: Select all

try:
    print (('Proses Tapper Roll ke %s') % counting)  <<<<<<<<<<<<<<<<<<<<<<<<<
    while True:
    
    
from the code .

if the program is outputting a 1 count before the sensor detects anything then you may not have things wired correctly or your sensor is not outputting what your expect, the program should not count unless a high is present on the gpio.

So does your sensor output a low signal when there is no object detection and a high when an object is detected ?

Edit...

Try this test version of the code I have added a print of the gpio pin state.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIN = 25
Pinstate = False
counting = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIN, GPIO.IN)

try:
    
    while True:
        print (GPIO.input(GPIN))
        if GPIO.input(GPIN) == 1:
            counting += 1
            print (('Proses Tapper Roll ke %s') % counting)

            while GPIO.input(GPIN) == 1:
                time.sleep(0.1)
            

        time.sleep(1)
    

except KeyboardInterrupt:
    GPIO.cleanup()
    
So you can see what the pi is reading on the gpio pin, if things are wired correctly you should just see zero's until an object is detected.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jun 12, 2020 8:48 am

Yes, my sensor output when low signal is 0, then when the object detected is 1. Thanks for your help.


Next question, can we make a button to clear all data and make it start again without exit from terminal? because i will implement this sensor using raspberry pi without keyboard and mouse. so that my operator in machine can start and finish easily? just push the button

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jun 12, 2020 8:53 am

Then, can we put time while the sensor detected object? because I need to count cycle time ( time between 2 detection object). or can we add to program to count that?

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

Re: Write data just when the output changes

Fri Jun 12, 2020 8:57 am

Anugerah Nuri Satria wrote:
Fri Jun 12, 2020 8:48 am
Yes, my sensor output when low signal is 0, then when the object detected is 1. Thanks for your help.


Next question, can we make a button to clear all data and make it start again without exit from terminal? because i will implement this sensor using raspberry pi without keyboard and mouse. so that my operator in machine can start and finish easily? just push the button
Just add the button as another gpio input then this input goes high reset the counting variable to zero.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Write data just when the output changes

Fri Jun 12, 2020 9:03 am

Anugerah Nuri Satria wrote:
Fri Jun 12, 2020 8:53 am
Then, can we put time while the sensor detected object? because I need to count cycle time ( time between 2 detection object). or can we add to program to count that?
adding the detection time just needs a single line like this

Code: Select all

try:
    
    while True:
        print (GPIO.input(GPIN))
        if GPIO.input(GPIN) == 1:
            counting += 1
            print (('Proses Tapper Roll ke %s') % counting)
            print(time.strftime("%H:%M:%S"))

            while GPIO.input(GPIN) == 1:
                time.sleep(0.1)
            
adding the time between detections is a little more complex, but do able.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jun 12, 2020 9:10 am

Just add the button as another gpio input then this input goes high reset the counting variable to zero.



Can you give me example in my python code for clearing my data without quit teriminal? I dont know how to make it.

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

Re: Write data just when the output changes

Fri Jun 12, 2020 9:21 am

With a 1K resistor in series with the switch , wired between gpio 21 and ground , this code will give you a count reset switch and the time the detector triggered.

Code: Select all

import RPi.GPIO as GPIO
import time

counting = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    
    while True:
        
        if GPIO.input(25) == 1:
            counting += 1
            print (('Proses Tapper Roll ke %s') % counting)
            print(time.strftime("%H:%M:%S"))

            while GPIO.input(25) == 1:
                time.sleep(0.1)
            
        
        if GPIO.input(21) == 0:
            print("reset counter")
            counting = 0
            while GPIO.input(21) == 0:
                time.sleep(0.1)
                
        time.sleep(0.1)
    

except KeyboardInterrupt:
    GPIO.cleanup()
    
tested as working using 2 switches for input.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Write data just when the output changes

Fri Jun 12, 2020 9:34 am

This version of the code will also give you the number of seconds between detections

Code: Select all

import RPi.GPIO as GPIO
import time

counting = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    start  = time.time()
    while True:
        
        if GPIO.input(25) == 1:
            counting += 1
            print (('Proses Tapper Roll ke %s') % counting)
            print(time.strftime("%H:%M:%S"))
            now = time.time()
            print ("time between detections", round((now - start), 2), "seconds")

            while GPIO.input(25) == 1:
                time.sleep(0.1)
            start = time.time()
            
        
        if GPIO.input(21) == 0:
            print("reset counter")
            counting = 0
            while GPIO.input(21) == 0:
                time.sleep(0.1)
            start = time.time()    
                
        time.sleep(0.1)
    

except KeyboardInterrupt:
    GPIO.cleanup()
    
again tested working using 2 switches.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jun 12, 2020 10:14 am

Which switch do i should use? NC or NO?
Then why we use 2 switch?

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

Re: Write data just when the output changes

Fri Jun 12, 2020 10:23 am

Anugerah Nuri Satria wrote:
Fri Jun 12, 2020 10:14 am
Which switch do i should use? NC or NO?
Then why we use 2 switch?
you need to use a N/O switch for the reset switch

I used 2 switches for testing as I don't have your object detector so I replaced it with a switch.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Mon Jun 15, 2020 1:20 am

Can we show its terminal via 7 segment? Or do u have idea to make HMI for that?

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

Re: Write data just when the output changes

Mon Jun 15, 2020 10:09 am

Yes you probably can , there are lots of example of using a seven segment display with a pi on the web, just google it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Wed Jul 01, 2020 3:32 am

Hi pcmanbob I have read a lot of seven segment for my output, but i also need to show time between to my operator. is there any HMi for raspberry pi that can show number of count and time between 2 sensing?

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

Re: Write data just when the output changes

Wed Jul 01, 2020 9:16 am

Anugerah Nuri Satria wrote:
Wed Jul 01, 2020 3:32 am
Hi pcmanbob I have read a lot of seven segment for my output, but i also need to show time between to my operator. is there any HMi for raspberry pi that can show number of count and time between 2 sensing?
Not sure what you are asking , HMi ?

But if you are asking about a display that can show more information I would suggest a 16x2 LCD or a 20x4 LCD , using the HDD44780 driver.

example 20x4 LCD on one of my pi's

Image

and I would recommend RPLCD as the driver software for it. https://pypi.org/project/RPLCD/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Thu Jul 02, 2020 1:20 am

So, i can write this exactly like this pict in lcd ?

WhatsApp Image 2020-06-18 at 08.58.42.jpeg
WhatsApp Image 2020-06-18 at 08.58.42.jpeg (114.65 KiB) Viewed 636 times
that is your project that you gave before

should we make another program to show output in that lcd? do u have the example for that?

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

Re: Write data just when the output changes

Thu Jul 02, 2020 9:52 am

Yes you could show the 4 lines that your python program outputs on the type of display is suggested.

I can write the code to output to the display until you have decided which type of display you are going to use, one that uses direct gpio connection or one that uses an I2C back pack.

I suggest you follow the link I posted to RPLCD and read the documentation , then get the display and install and set up RPLCD , then once the display is working Its just a simple job to change the print statements to output the information on the display instead.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jul 03, 2020 1:24 am

Probably i will use i2c because if i decided to use i2C, it means that i should buy display and i2C module, right?

Basicly, what is the differences between i2C and gpio?
Last edited by Anugerah Nuri Satria on Fri Jul 03, 2020 2:28 am, edited 1 time in total.

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Fri Jul 03, 2020 2:22 am

Anugerah Nuri Satria wrote:
Fri Jul 03, 2020 1:24 am
Probably i will use gpio because if i decided to use i2C, it means that i should buy display and i2C module, right?

Basicly, what is the differences between i2C and gpio?
But i think wiring using i2c is easier than using gpio, right?

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

Re: Write data just when the output changes

Fri Jul 03, 2020 11:41 am

Have a look at these 2 videos on youtube , they will show you the difference in the wiring .

https://www.youtube.com/watch?v=3XLjVChVgec

https://www.youtube.com/watch?v=wUaQxLfZ2KA

You would still have to do the RPLCD set up because I cant install software on your pi, once you have it installed and you can get the display to show you the test text , making your program output to the display is the easy bit.


I personally prefer the GPIO method , but just because that's how I have always done it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Anugerah Nuri Satria
Posts: 35
Joined: Tue Mar 13, 2018 11:10 am

Re: Write data just when the output changes

Thu Jul 09, 2020 1:27 am

I have purchased LCD 20x4 I2C.
and i have installed RPLCD.

Then, what should I do to show my counter system?
Attachments
WhatsApp Image 2020-07-09 at 07.28.35.jpeg
WhatsApp Image 2020-07-09 at 07.28.35.jpeg (73.62 KiB) Viewed 445 times

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

Re: Write data just when the output changes

Thu Jul 09, 2020 9:06 am

Anugerah Nuri Satria wrote:
Thu Jul 09, 2020 1:27 am
I have purchased LCD 20x4 I2C.
and i have installed RPLCD.

Then, what should I do to show my counter system?
Follow the instructions and connect your display to your pi and get it to display some basic text first so you know its working correctly.

https://rplcd.readthedocs.io/en/stable/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”