0

I'm currently working on a RaspberryPi project where I'm trying to hook up a pulse water meter to the Pi via an AD converter. The ADC came with some code that lets me check the different ports' voltages. Whenever I blow into the meter the wheels start turning and when they hit a full number, the displayed voltage goes from ~1.2v to ~3.2v. However, if I stop blowing while the meter sits on a full number, the voltage stays at 3.2v. All I'm trying to do is set up a variable "waterTotal" that increases whenever the analog meter 'pulses' like this.

Problem is, I've only ever coded in C# using Unity, so Python's kinda scary... I guess in C# the code would look something like this:

    float voltage;
    bool totalHasIncreased = false;
    int waterTotal = 0;

    if (totalHasIncreased == false && voltage > 3.0)
    {
    waterTotal++
    totalHasIncreased = true;
    }
    else if (voltage < 1.5)
    {
    totalHasIncreased = false;
    }

    print(waterTotal)

I've tried to translate this to Python but the console doesn't display any change to waterTotal, and my knowledge is too shallow to figure out what exactly's going awry. Keep in mind, the issue could be super basic, like maybe I'm not working with the correct library or something. Hope someone can help!

This is the code I'm currently working with. I've 'fixed' all the error messages that Thonny's thrown my way so far. The first half of this is the code that came with the ADC, the rest is my attempt:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)

# Create the ADC object using the I2C bus
ads = ADS.ADS1115(i2c)

# Create single-ended input on channel 0
chan = AnalogIn(ads, ADS.P0)
chan_2 = AnalogIn(ads, ADS.P1)

# Create differential input between channel 0 and 1
# chan = AnalogIn(ads, ADS.P0, ADS.P1)

wTwasRaised = False
waterTotal = 0

def increaseWaterMeter(wTwR, wT):
    if wTwR == False:
        if chan.voltage > 3.0:
            wT += 1
            wTwR = True  
    if chan.voltage < 1.5:
        wTwR = False
    
print("{:>5}\t{:>5}".format("raw", "v"))

while True:   
    increaseWaterMeter(wTwasRaised, waterTotal)
    print("CHAN 0: "+"{:>5}\t{:>5.3f}".format(chan.value, chan.voltage))
    print("CHAN 1: "+"{:>5}\t{:>5.3f}".format(chan_2.value, chan_2.voltage))
    #print(waterTotal + " liters counted")
    print(" A0:%dliters "%waterTotal)
    time.sleep(1)
Jargo
  • 1
  • 1

0 Answers0