logo elektroda
logo elektroda
X
logo elektroda

Arduino ESP32: Summing v01 to v16 in a For Loop of Struct Elements

xury 558 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20112041
    xury
    Automation specialist
    I have this structure:
    Code: C / C++
    Log in, to see the code
    .
    Code: C / C++
    Log in, to see the code


    How to make in the for loop the sum of all from v01 to v16 ?
    respstruct.v01
    ......
    respstruct.v16
  • ADVERTISEMENT
  • Helpful post
    #2 20112072
    gps79
    Level 36  
    Probably the simplest way is this:
    Code: C / C++
    Log in, to see the code
    .
  • #3 20112109
    khoam
    Level 42  
    @xury In C or in C++?
  • ADVERTISEMENT
  • #4 20112877
    xury
    Automation specialist
    Arduino on a platform. So C++
  • ADVERTISEMENT
  • Helpful post
    #5 20113177
    khoam
    Level 42  
    It can be shorter:
    Code: C / C++
    Log in, to see the code
    .

    EDIT: I corrected the sum type to force uint32_t in case of overflow.
  • ADVERTISEMENT
  • #6 20115436
    xury
    Automation specialist
    Thanks mate khoam .
    However, there is a small problem, because the function seems to not sum the last element:
    Example print:
    Quote:
    1658734976,205,208,235,234,-3,330,331,329,332,331,332,333,330,331,332,331,331,332,331,331,333
    Total voltages: 4967
    .
    Counting from the seventh decimal (v01 330) to the last 333 should be 5300.
    [edit.]
    I already know why:
    "Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. "
  • Helpful post
    #7 20115454
    khoam
    Level 42  
    Add 1 to the second iterator (second argument in accumulate).
    &respstruct.v16 + 1

    Added after 41 [minutes]: .

    After correction it should be:
    Code: C / C++
    Log in, to see the code

    Sorry, I forgot that the end() iterator should point to the "after" element.

    Arduino ESP32: Summing v01 to v16 in a For Loop of Struct Elements
  • #8 20115585
    xury
    Automation specialist
    Thank you very much. The sum is counting correctly. Now I am trying to extract the min and max values and where they occur from the same range.
    I admit that for me these functions are a total novelty. Am I combining correctly by trying to use https://en.cppreference.com/w/cpp/algorithm/ranges/minmax

Topic summary

The discussion revolves around summing the values from v01 to v16 in a struct named bmsresponse within an Arduino ESP32 environment. A user inquires about implementing this sum using a for loop. Responses suggest using pointers to iterate through the struct's members, with one solution employing a do-while loop. Another response proposes a more concise method using the C++ standard library's std::accumulate function, correcting the iterator range to include the last element. The user later seeks assistance in extracting minimum and maximum values from the same range, leading to a discussion about the appropriate C++ functions, with a note on the limitations of the current Arduino Core for ESP32 regarding C++20 support.
Summary generated by the language model.
ADVERTISEMENT