Menu

How to get the total value of an array?

Help
2016-11-22
2016-11-24
  • Fábio Almeida

    Fábio Almeida - 2016-11-22

    I want to get the total value of an array, how do I do this?

    I already tried to do this, it did not work:
    Dim valuetotal as byte
    Dim myarray(8)

    valuetotal.0 = myarray(1)
    valuetotal.1 = myarray(2)
    valuetotal.2 = myarray(3)
    valuetotal.3 = myarray(4)
    valuetotal.4 = myarray(5)
    valuetotal.5 = myarray(6)
    valuetotal.6 = myarray(7)
    valuetotal.7 = myarray(8)

    Detail: array values are 1 or 0
    In the theory the value of valuetotal should be 0 to 255,
    But when I read the value of In valuetotal it is always shown 0

     
  • Anobium

    Anobium - 2016-11-22

    Use Setwith method. This will sort this.

     
  • David Stephenson

    I think the problem you are having is due to myarray() being a byte and valuetotal being a bit.
    Maybe if you put valuetotal.0=myarray(1).0 it would maybe work. Or...

    k=1
    for i=1 to 8
    if myarray(i) > 0 then valuetotal=valuetotal+k
    k=k*2
    next i
    
     

    Last edit: David Stephenson 2016-11-24
    • Anobium

      Anobium - 2016-11-24

      The FAQ in the Help shows why this cannot work. The SetWith method will resolve.

       
  • David Stephenson

    I use bit-wise assignments similar to this quite a lot. Are you saying that I need to be more careful as unexpected things can happen? I've picked out a particularly ugly piece of code that is part of my driver for an e-paper display which works.

    k5=0xAA
    k5.6=k4_h.7
    k5.4=k4_h.6
    k5.2=k4_h.5
    k5.0=k4_h.4
    spitransfer k5,r1 
    wait 1 us
    k5=0xAA
    k5.6=k4_h.3
    k5.4=k4_h.2
    k5.2=k4_h.1
    k5.0=k4_h.0
    spitransfer k5,r1 
    
     
  • Anobium

    Anobium - 2016-11-24

    Nothing unexpected should be happen.

    @David. I am assuming you are on v.0.95.010
    @Fábio Almeida ... I am thinking you are not on v.0.95.010. Let us know. As the code below works correctly (prints 32). Also, which chip?

      #chip 16f877a
    
      'USART settings
      #define USART_BAUD_RATE 9600
    
      Dim valuetotal as byte
    
      Dim myarray(8)
    
      myarray(6) =1
      valuetotal.0 = myarray(1)
      valuetotal.1 = myarray(2)
      valuetotal.2 = myarray(3)
      valuetotal.3 = myarray(4)
      valuetotal.4 = myarray(5)
      valuetotal.5 = myarray(6)
      valuetotal.6 = myarray(7)
      valuetotal.7 = myarray(8)
    
      HSerPrintbyteCRLF valuetotal
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.