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

Re: Write data just when the output changes

Mon Jul 13, 2020 5:59 am

Dear Mr.Pcmanbob


I have followed that instruction. But why my LCD does not ON? i have given VCC and GND correctly.
Attachments
WhatsApp Image 2020-07-13 at 12.57.01.jpeg
WhatsApp Image 2020-07-13 at 12.57.01.jpeg (49.54 KiB) Viewed 703 times

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

Re: Write data just when the output changes

Mon Jul 13, 2020 7:52 am

From the information supplied its impossible for anyone to tell you why your LCD is not working, we don't have access to your pi/LCD so we can only off advice on the information supplied.

But a guess would be that if your display is not even illuminating your have not connected something correctly.
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

Tue Jul 14, 2020 1:30 am

Hello Mr. PcManBob
Now, My LCD is ON. But I dont know how to show output from terminal to LCD in realtime for my code before.
Python Code for counting.
Attachments
WhatsApp Image 2020-07-14 at 08.26.21.jpeg
WhatsApp Image 2020-07-14 at 08.26.21.jpeg (54.48 KiB) Viewed 661 times

User avatar
DougieLawson
Posts: 39581
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Write data just when the output changes

Tue Jul 14, 2020 5:31 am

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

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

Re: Write data just when the output changes

Tue Jul 14, 2020 7:48 am

OK

So now you have it working can you post the code that produced the LCD screen as shown in your image so I can see what setup you used.

then I can make suggestions on how to add it to your existing code.
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 16, 2020 12:44 am

I still use your code Sir. because it matches for my project to count leaf spring. I dont know how to show in LCD sir. I need your suggestions.

pcmanbob wrote:
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.

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

Re: Write data just when the output changes

Thu Jul 16, 2020 8:08 am

Yes I understand you want to use the original code , but I need the code you used to make your display work , the program you ran to put the text on the LCD in your picture , as without that I don't know what setup you used for the LCD.

I cant use one of my examples becuae I connect my LCD's differently you how you have connected yours.
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 16, 2020 8:26 am

This is my code SIr

Code: Select all

import RPi.GPIO as GPIO
import time
import csv
import lcddriver

display = lcddriver.lcd()
display.lcd_display_string("Counter :", 1)

counting = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
csvfile = "countermesin"
try:
    start  = time.time()
    while True:

        if GPIO.input(25) == 1:
            print (('Proses Tapper Roll ke %s') % counting)
            counting += 1
            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()
            result = str(counting)+"item"
            display.lcd_display_string(result,2);


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

        time.sleep(0.1)



except KeyboardInterrupt:
    GPIO.cleanup()

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

Re: Write data just when the output changes

Thu Jul 16, 2020 8:38 am

looking at your code you have already worked out how to display information on the LCD , so what problem are you having in changing the print lines to output the information on the display ?
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 16, 2020 8:40 am

pcmanbob wrote:
Thu Jul 16, 2020 8:38 am
looking at your code you have already worked out how to display information on the LCD , so what problem are you having in changing the print lines to output the information on the display ?
i just found that way today sir, I still confuse how to make 2 line to make 2 output. average time between and sum of item.

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

Re: Write data just when the output changes

Thu Jul 16, 2020 10:37 am

Looking at this code you posted

Code: Select all

import RPi.GPIO as GPIO
import time
import csv
import lcddriver

display = lcddriver.lcd()
display.lcd_display_string("Counter :", 1)

counting = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
csvfile = "countermesin"
try:
    start  = time.time()
    while True:

        if GPIO.input(25) == 1:
            print (('Proses Tapper Roll ke %s') % counting)
            counting += 1
            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()
            result = str(counting)+"item"
            display.lcd_display_string(result,2);


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

        time.sleep(0.1)



except KeyboardInterrupt:
    GPIO.cleanup()
does it actually output anything to the screen ? because this line

Code: Select all

display = lcddriver.lcd()
does not have any of the setup details required to make the LCD work

it should look more like this

Code: Select all

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              backlight_enabled=True)
              
this is only an example take from the documents.

you need to provide me with the information in the first line.

expander= ????
address=????
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 17, 2020 3:13 am

if I run my code, it will give output like this
WhatsApp Image 2020-07-17 at 10.05.09.jpeg
WhatsApp Image 2020-07-17 at 10.05.09.jpeg (39.48 KiB) Viewed 492 times
For Expander and Adress.

pi@raspberrypi:~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

I dont know how to check expander sir

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

Re: Write data just when the output changes

Fri Jul 17, 2020 8:36 am

I am surprised but ok , can you run this test code to check your display works as expected regarding display positioning.

Code: Select all

import RPi.GPIO as GPIO
import time
import csv
import lcddriver

lcd = lcddriver.lcd(cols=20, rows=4)

lcd.cursor_pos = (0, 0)
lcd.write_string("line 0")

lcd.cursor_pos = (1, 1)
lcd.write_string("line 1")

lcd.cursor_pos = (2, 2)
lcd.write_string("line 2")

lcd.cursor_pos = (3, 3)
lcd.write_string("line 3")

time.sleep(30)

you should see text on all 4 lines, moving one place further to the right on each line.
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 17, 2020 9:03 am

pi@raspberrypi:~/Documents $ python saiful.py
Traceback (most recent call last):
File "saiful.py", line 6, in <module>
lcd = lcddriver.lcd(cols=20, rows=4)
TypeError: __init__() got an unexpected keyword argument 'rows'

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

Re: Write data just when the output changes

Fri Jul 17, 2020 9:23 am

Are you using RPLCD ?

if so which version did you down load ?

if no please post a link to the driver you are using....
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 Jul 20, 2020 12:50 am

pi@raspberrypi:~ $ sudo pip install RPLCD
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: RPLCD in /usr/local/lib/python2.7/dist-packages (1.3.0)

i just follow your instruction before at
https://rplcd.readthedocs.io/en/stable/ ... ation.html

pcmanbob wrote:
Fri Jul 17, 2020 9:23 am
Are you using RPLCD ?

if so which version did you down load ?

if no please post a link to the driver you are using....

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

Re: Write data just when the output changes

Mon Jul 20, 2020 8:58 am

Then I am confused because no were in the docs I can see does it say import lcddriver

the import command for RPLCD is from RPLCD.i2c import CharLCD

then you need a set up line similar to the one I posted before

Code: Select all

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              backlight_enabled=True)
              
so what instructions were you following when it saidto use import lcddriver
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

Tue Jul 21, 2020 1:01 am

Sorry, i remember that i follow this web:
https://github.com/jumejume1/pi-LCD-Ult ... nce-lcd.py
But it is used just for 2 lines.
pcmanbob wrote:
Mon Jul 20, 2020 8:58 am
Then I am confused because no were in the docs I can see does it say import lcddriver

the import command for RPLCD is from RPLCD.i2c import CharLCD

then you need a set up line similar to the one I posted before

Code: Select all

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              backlight_enabled=True)
              
so what instructions were you following when it saidto use import lcddriver

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

Re: Write data just when the output changes

Tue Jul 21, 2020 9:28 am

So if you have installed RPLCD, then if my guess at your hardware set up is correct then this should output test on all 4 lines of the LCD

Code: Select all

import RPi.GPIO as GPIO
import time

from RPLCD.i2c import CharLCD

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              compat_mode=True)

lcd.clear()
lcd.cursor_pos = (0, 0)
lcd.write_string("line 0")

lcd.cursor_pos = (1, 1)
lcd.write_string("line 1")

lcd.cursor_pos = (2, 2)
lcd.write_string("line 2")

lcd.cursor_pos = (3, 3)
lcd.write_string("line 3")

time.sleep(30)
this is only a test to see if I have guessed your hardware setup correctly, if its no right then you will have to adjust it and let me know what needs changing.
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 22, 2020 1:29 am

Yes sir, it works in my Rpi. So how can I show a counter and average time between?


pcmanbob wrote:
Tue Jul 21, 2020 9:28 am
So if you have installed RPLCD, then if my guess at your hardware set up is correct then this should output test on all 4 lines of the LCD

Code: Select all

import RPi.GPIO as GPIO
import time

from RPLCD.i2c import CharLCD

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              compat_mode=True)

lcd.clear()
lcd.cursor_pos = (0, 0)
lcd.write_string("line 0")

lcd.cursor_pos = (1, 1)
lcd.write_string("line 1")

lcd.cursor_pos = (2, 2)
lcd.write_string("line 2")

lcd.cursor_pos = (3, 3)
lcd.write_string("line 3")

time.sleep(30)
this is only a test to see if I have guessed your hardware setup correctly, if its no right then you will have to adjust it and let me know what needs changing.
Attachments
WhatsApp Image 2020-07-22 at 08.27.57.jpeg
WhatsApp Image 2020-07-22 at 08.27.57.jpeg (50.4 KiB) Viewed 281 times

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

Re: Write data just when the output changes

Wed Jul 22, 2020 10:40 am

Ok so now we have that sorted.

try this code , note its untested so there may be spacing errors and I have set it to clear the LCD on every update ,

you can change this if you make it so that you load the LCD fixed test once and then update the changing text on each pass.
It makes it a little more complex and you have to account for changing text lengths.

Code: Select all

import RPi.GPIO as GPIO
import time
from RPLCD.i2c import CharLCD

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

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              compat_mode=True)

counting = 0

try:
    start  = time.time()
    while True:
        
        if GPIO.input(25) == 1:
            counting += 1
            
            now = time.time()
            
            lcd.clear()
            lcd.cursor_pos = (0, 0)
            lcd.write_string("Proses Tapper Roll) 
            lcd.cursor_pos = (1, 0)
            lcd.write_string("Item")
            lcd.cursor_pos = (1, 6)
            lcd.write_string str(counting)
            lcd.cursor_pos = (1, 12)
            lcd.write_string(time.strftime("%H:%M:%S"))
            lcd.cursor_pos = (2, 0)
            lcd.write_string("Between detections") 
            
            DT = str(round((now - start), 2))
            DT = DT + " seconds"
            
            lcd.cursor_pos = (3, 0)
            lcd.write_string(DT)

            while GPIO.input(25) == 1:
                time.sleep(0.1)
            start = time.time()
            
        
        if GPIO.input(21) == 0:
        
            lcd.clear()
            lcd.cursor_pos = (1, 3)
            lcd.write_string("reset counter")
            counting = 0
            while GPIO.input(21) == 0:
                time.sleep(0.1)
            start = time.time()    
                
        time.sleep(0.1)
    

except KeyboardInterrupt:
    GPIO.cleanup()
    
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 23, 2020 11:01 am

What should I change to make that program start from zero while counting?
pcmanbob wrote:
Wed Jul 22, 2020 10:40 am
Ok so now we have that sorted.

try this code , note its untested so there may be spacing errors and I have set it to clear the LCD on every update ,

you can change this if you make it so that you load the LCD fixed test once and then update the changing text on each pass.
It makes it a little more complex and you have to account for changing text lengths.

Code: Select all

import RPi.GPIO as GPIO
import time
from RPLCD.i2c import CharLCD

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

lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1,
              cols=20, rows=4, dotsize=8,
              charmap='A02',
              auto_linebreaks=True,
              compat_mode=True)

counting = 0

try:
    start  = time.time()
    while True:
        
        if GPIO.input(25) == 1:
            counting += 1
            
            now = time.time()
            
            lcd.clear()
            lcd.cursor_pos = (0, 0)
            lcd.write_string("Proses Tapper Roll) 
            lcd.cursor_pos = (1, 0)
            lcd.write_string("Item")
            lcd.cursor_pos = (1, 6)
            lcd.write_string str(counting)
            lcd.cursor_pos = (1, 12)
            lcd.write_string(time.strftime("%H:%M:%S"))
            lcd.cursor_pos = (2, 0)
            lcd.write_string("Between detections") 
            
            DT = str(round((now - start), 2))
            DT = DT + " seconds"
            
            lcd.cursor_pos = (3, 0)
            lcd.write_string(DT)

            while GPIO.input(25) == 1:
                time.sleep(0.1)
            start = time.time()
            
        
        if GPIO.input(21) == 0:
        
            lcd.clear()
            lcd.cursor_pos = (1, 3)
            lcd.write_string("reset counter")
            counting = 0
            while GPIO.input(21) == 0:
                time.sleep(0.1)
            start = time.time()    
                
        time.sleep(0.1)
    

except KeyboardInterrupt:
    GPIO.cleanup()
    

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

Re: Write data just when the output changes

Thu Jul 23, 2020 11:12 am

Anugerah Nuri Satria wrote:
Thu Jul 23, 2020 11:01 am
What should I change to make that program start from zero while counting?
You should already have that option it was added along with the second button before you even added the LCD.

This code resets the counter to zero

Code: Select all

        if GPIO.input(21) == 0:
            print("reset counter")
            counting = 0
            while GPIO.input(21) == 0:
                time.sleep(0.1)
            start = time.time()  
            
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 23, 2020 11:32 am

I mean when I start firstly, that code will give output 2 directly ( 1 and 2 Items)

Or I make a mistake?

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

Re: Write data just when the output changes

Thu Jul 23, 2020 11:42 am

Well it working correctly when the results were bing output to the display , using this code

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()
    
so I suggest you try running this code again to see if your hardware is the problem.

all we changed to use the LCD was were the output was sent to , changed print statements to lcd write statements.

As your count is controlled by your sensor and I don't have such a sensor I can really help with the sensor input.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Python”