Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    LoPy4 alarm callback not repeating periodically

    Scheduled Pinned Locked Moved LoPy
    2 Posts 2 Posters 40 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      joshhulnz
      last edited by

      I have a LoPy4 which I am trying to measure an ADC voltage on periodically. I have an alarm that I am trying to trigger interrupts every five seconds with. Here is what the code I am testing with looks like:

      from machine import ADC
      from machine import Timer
      import utime
      
      adc = ADC(0)
      #ADC channels for baterry voltage monitoring
      adc_b1 = adc.channel(pin = 'P20', attn=ADC.ATTN_11DB)
      
      list = []
      
      #Measures battery voltage
      def battery_voltage():
          for i in range(0, 35):
              read = adc_b1.voltage()
              list.insert(i, read)
          avg_reading_b1 = sum(list)/len(list)
          list.clear()
      
          bat_1 = str(round(avg_reading_b1/1000, 2)) #String and round
          print(bat_1)
      
      battery_alarm = Timer.Alarm(battery_voltage(), s=5, periodic=True)
      
      def main():
          while(1):
              continue
      
      main()
      

      The functions battery_voltage() gets called once after the file has been successfully uploaded. After that, the program continues to loop but battery_voltage() does not get called again. Is there something wrong with how I am setting up the alarm?

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @joshhulnz
        last edited by robert-hh

        @joshhulnz
        a) write: Timer.Alarm(battery_voltage, s=5, periodic=True) without the brackets.
        Optimization:
        b) in the main loop, I would use a short sleep instead of the continue statement
        c) I you just need the average, you do not have to collect the values. Just add them up while reading, e.g.

        def battery_voltage():
            avg = 0
            for i in range(0, 35):
                avg += adc_b1.voltage()
            print(round(avg/35000, 2))
        
        1 Reply Last reply Reply Quote 1

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom