-1

I have a little problem with variables and statements. So i'm working on a project and I have to use different statements.

Materials:

Pycom Lopy4 + 3.0 expansion board, Flexiforce weightsensor, LoRa

This is my code now:

import pycom
import machine
import time

############### Weightsensor ###############
def main():
    adc = machine.ADC()             # create an ADC object
    apin = adc.channel(pin='P16')   # create an analog pin on P16
    val = apin()                    # read an analog value

    if val < 50:
        print(val)
        print("Value to high")
        time.sleep(5)

if __name__ == "__main__":
    main()

What do I want to achieve?

Something like this:

def main():
    while True:
        adc = machine.ADC()             # create an ADC object
        apin = adc.channel(pin='P16')   # create an analog pin on P16
        val = apin()                    # read an analog value

        if val < 20:
            print(val)
            print("Value to high")
            time.sleep(2.5)


if __name__ == "__main__":
    main()

But I don't know how te get it work because the "val = apin()" is my reading, and I want to use that as a variable. So that if the value gets higher that it says that.

WORKING CODE:

def main():
    while True:
        adc = machine.ADC()             # create an ADC object
        apin = adc.channel(pin='P16')   # create an analog pin on P16
        val = apin()                    # read an analog value

        if val < 20:
            print(val)
            print("Weight is good")
            time.sleep(2.5)
        if val > 20:
            print(val)
            print("Weight is to high")
            time.sleep(2.5)



if __name__ == "__main__":
    main()

Thanks for any help in advance!

Kind regards!

Zino
  • 1
  • 4
  • 3
    `What do I want to achieve?` Yes, we would like to know that as well. You need to give some examples of what `apin()` might return and what you want your program to do in response. – quamrana Jan 14 '21 at 14:32
  • 1
    It's not clear what you want to loop over. If `apin` returns an integer, you can't iterate over that. In your second example, you are trying to compare the string `"50"` to the integer `20`, which isn't allowed. Do you want to repeated *call* `apin` until its value is less than 50? – chepner Jan 14 '21 at 14:33
  • 1
    Unclear. What does `apin()` return? Do you want to use a loop? Under what condition does the loop stop? – 001 Jan 14 '21 at 14:33
  • Sorry i found my mistake for the loop.. I forgot "while True" at the beginning. i will update it now in the messages. but thanks for reacting so quick! – Zino Jan 14 '21 at 15:03

2 Answers2

2

I think you want a while loop to repeated call apin until it returns a value less than 50.

def main():
    adc = machine.ADC()             # create an ADC object
    apin = adc.channel(pin='P16')   # create an analog pin on P16    
    while True:
        val = apin()
        if value > 50:
            break
        print(val)
        print("Value not high enough")
        time.sleep(5)

    print("Now value is high enough")
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Actually if apin() returns a single value, periodic measures as you proposed is the best approach. I consider apin() to be like an array of pins, to itetate through, but believe your logic makes more sense – Ignacio Alorre Jan 14 '21 at 14:38
0

If you want to compare each element in val, then:

for x in val:
   if x > 50:
       print(val)
       print('Value too high')
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94