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

    how to put variable in file inside Flash: pycom

    Scheduled Pinned Locked Moved Expansion Board
    6 Posts 3 Posters 57 Views 3 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.
    • H Offline
      HAMMOUTI
      last edited by robert-hh

      from machine import ADC
      import time
      adc = ADC()
      fichier=open('valeurenmv.py',"a")
      while True :
          bat_voltage = adc.channel(attn=ADC.ATTN_11DB, pin='P16')
          vbat = bat_voltage.value()
          # note that the expansionboard 3 has a voltage divider of 1M / 1M to account for
          # 1M / 1M, ratio = 1:2
          vbat = vbat*2
          time.sleep(3)
          print('battery voltage:', vbat, 'mV')
          fichier.write('\nvbat')
      

      Hi , can you help me to put result of variable vbat in file

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

        @HAMMOUTI There are several way in Python to create a formatted string with variable. The most common ones are the % operator on strings or the .format method of strings. Both are implemented in MicroPython and well documented in every Python textbook. Just a few simple examples for your case:
        fichier.write('\nvbat=%f' % vbat)
        or, if you just want the number:
        fichier.write('\n%f' % vbat)
        or using the format method:
        fichier.write('\nvbat={}'.format(vbat))
        or
        fichier.write('\n{}'.format(vbat))

        1 Reply Last reply Reply Quote 0
        • H Offline
          HAMMOUTI
          last edited by

          e9db6ef5-a68b-43f2-badd-e215fb67efc2-image.png

          think you for your help, i try this code, but i have this error see capture .
          how to fix this error.

          robert-hhR D 3 Replies Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @HAMMOUTI
            last edited by

            @HAMMOUTI What is the problem. I can't spot it in the capture. The only odd I see is the reported battery voltage.

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

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • D Offline
                DEFRAINE.JM @HAMMOUTI
                last edited by

                @HAMMOUTI try to change the name of your file "valeurenmv.py" by "valeurenmv.txt" !
                Jiemde

                1 Reply Last reply Reply Quote 0

                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