element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Keeping an average in a sketch.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 6 replies
  • Answers 1 answer
  • Subscribers 416 subscribers
  • Views 787 views
  • Users 0 members are here
Related

Keeping an average in a sketch.

e14 Contributor
e14 Contributor over 13 years ago

First off, thank you to everyone at the element14 community for professionally answering my novice questions. Its really great that everyone is willing to be so helpful!

 

This time around, I need a little help on my current project. I am making a arduino clone of a basic bicycle computer. I've got the basics of it set up, and I can accurately output my current speed, and distance traveled. My next goal is to calculate my average speed. The idea of storing all of my speed readings, and trying to average them all as I go weems ludicris. I would quickly fill the arduino up with an array of speeds. I was hoping there is a better way. Has anyone ever needed to do something similar? What would be the best equation to use?

 

My inital thought was something along the lines of: new average = (previous average + new value) / 2

However, it doesnt seem to be as accurate as properly averaging the numbers.

  • Sign in to reply
  • Cancel

Top Replies

  • e14 Contributor
    e14 Contributor over 13 years ago in reply to ntewinkel +1
    Alright, then a float it is, thanks you very much!
  • ntewinkel
    0 ntewinkel over 13 years ago

    Average = sum_of_all_values / number_of_values

     

    So you could store one field named something like "sum" and keep adding to it.

    And keep a field named something like "count" and keep incrementing it.

     

    Then your average will always be sum/count.

     

    For sum, you should use a "long" datatype or something that can hold really big numbers.

     

    Hope that helps image

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to ntewinkel

    Do I need to worry about storing a potentially huge value like that? If I am adding 15-20 every time the wheel turns, over the course of hours, it would be massive.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to e14 Contributor

    Unsigned long can handle more than 4 billion: http://arduino.cc/en/Reference/UnsignedLong

    A float is even bigger (power of 38): http://arduino.cc/en/Reference/Float

     

    I suppose the other way to handle it is to add the fractional change, but that would still require a float to calculate the difference which would ultimately become a tiny tiny fraction.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to ntewinkel

    I think your best bet is a float, as you're probably tracking speed with some decimals points so you'd need that anyway.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to ntewinkel

    Alright, then a float it is, thanks you very much!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to e14 Contributor

    ps, I think the formula you had in mind, to directly add to the average every time, would look like this:

     

    newAverage = previousAverage * (count-1 / count) + newSpeed * (1 / count)

     

    That would involve 2 multiplications, 2 divisions, 1 subtract, and 1 add.

    The other method requires 2 additions (1 to add to the sum, 1 to increment the counter), and one division.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube