trigger event when variable equals x - python

Got a robot project? Need some help making it move?
Post Reply
nickc20
n00b
Posts: 2
Joined: Tue Jan 01, 2019 1:59 pm

trigger event when variable equals x - python

Post by nickc20 » Tue Jan 01, 2019 2:10 pm

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

User avatar
Dave
ModMyPi
Posts: 2786
Joined: Wed Jul 22, 2015 11:36 am
Contact:

Re: trigger event when variable equals x - python

Post by Dave » 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) 
Forum Administrator & Technical Support

nickc20
n00b
Posts: 2
Joined: Tue Jan 01, 2019 1:59 pm

Re: trigger event when variable equals x - python

Post by nickc20 » Wed Jan 02, 2019 10:38 pm

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!

Post Reply