Page 1 of 1

trigger event when variable equals x - python

Posted: Tue Jan 01, 2019 2:10 pm
by nickc20
Hi

I am using this script - https://www.modmypi.com/blog/how-to-use ... spberry-pi - to count button presses. I would like to modify it so that when the counter variable reaches 10 an event is triggered.

The simplest way to do this I've been informed is to check the counter each time it is modified and then using an 'if' statement trigger an event when the value is 10.

Unfortunately my coding abilities are rather limited and I therefore need some pointers as to how to move forward.

Many thanks

Re: trigger event when variable equals x - python

Posted: Wed Jan 02, 2019 9:48 am
by Dave
I would highly recommend having a google and learning the basics and fundamentals of python :) otherwise you'll spend all your time waiting for replies on the forum :P

To answer your question, yes checking the value of your counter is a good way of doing this. Here is an example of how this could work (this code is untested!)

Code: Select all

counter = 0

while True:
  counter += 1
  if counter == 10:
    print("counter reached 10!")
    counter = 0 // reset the value of counter to 0 if you want to reset it
  sleep(1) 

Re: trigger event when variable equals x - python

Posted: Wed Jan 02, 2019 10:38 pm
by nickc20
Dave wrote:
Wed Jan 02, 2019 9:48 am
I would highly recommend having a google and learning the basics and fundamentals of python :) otherwise you'll spend all your time waiting for replies on the forum :P

To answer your question, yes checking the value of your counter is a good way of doing this. Here is an example of how this could work (this code is untested!)

Code: Select all

counter = 0

while True:
  counter += 1
  if counter == 10:
    print("counter reached 10!")
    counter = 0 // reset the value of counter to 0 if you want to reset it
  sleep(1) 
Many thanks. Yes I fully intend to do that after this starting from basics!