Cross module variable not incrementing


I’m doing a home project with a raspi and a rain sensor.

Basically, with cronjobs, i run every 30 min.

If it is raining, i want it to increment a specific variable from another module (rains.water)

If it stops raining, i want it to decrease the same variable.

here are my codes:


import RPi.GPIO as GPIO
import rains
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def raincounter():
    if (GPIO.input(4) and (0<rains.water<13)):
        rains.water-=1 #No Rain
    elif (GPIO.input(4)==0 and (0<rains.water<13)):
        rains.water+=1 #Rain

import rains
import isitraining

print (rains.water)

everytime i run the “” for the first time, it does modify the “rains.water” variable but only one time, if i run the script any other time after that, it does not increment or decrease in value.

gpio.input(4) is a rain sensor with digital input. When it’s high, it means there’s no rain and when it’s low it means it is raining.

Any thoughts?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

If I’m reading this correctly you would also need to modify file after each increment or decrement. Or use different way to record the change. Otherwise each time rains module is imported water will have the same starting value - 0.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.