Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:38:40+00:00 2026-06-14T20:38:40+00:00

Hi guys I’m writing an application in C# that gets data from an accelerometer

  • 0

Hi guys I’m writing an application in C# that gets data from an accelerometer (inside a wiimote), and then processes and graphs it.
I’m using Brian Peek’s Wiimote library, so I’m using an event handler to get the data:

void wiimote_WiimoteChanged(object sender, WiimoteChangedEventArgs e)
{
    X = e.WiimoteState.AccelState.Values.X;
    Y = e.WiimoteState.AccelState.Values.Y;
    Z = e.WiimoteState.AccelState.Values.Z;          

}

I want to graph/save/process the data at a rate of 100Hz, so I created a timer object (WiiTimer), set it’s “Tick Interval” to 10ms, then on every tick, of the timer the data is stored/processed:

private void WiiTimer_Tick(object sender, EventArgs e)
{

   //Signal Processing (Some median and low pass filtering etc.) Ive removed the code for that since it is irrelevant here

    sw.WriteLine((X * 9.8).ToString() + ", " + (Y * 9.8).ToString() + ", " + (Z*9.8).ToString() );
  //Write Processed Data to file            

}

Now the problem is, the data isnt actualy saved at 100Hz, since the Signal Processing takes some time, the timer doesnt manage to call its OnTick event handler EVERY 10ms. and hence the data storage rate depends on how fast a machine you’re running the code on, how many other programs you have running etc.

How do I solve this? I thought of running the timer on a separate thread, but that would cause other problems, as someone pointed out on another question here:
“Your timer event is likely to want to access objects from the other thread. But to do so will result in race conditions.”

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:38:41+00:00Added an answer on June 14, 2026 at 8:38 pm

    Timers aren’t particularly exact, so the best way to do this if you actually want 100 samples per second is something like this:

    private class Result
    {
        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }
    }
    
    private Result lastResult;
    
    void wiimote_WiimoteChanged(object sender, WiimoteChangedEventArgs e)
    {
        Result newResult = new Result {
            X = e.WiimoteState.AccelState.Values.X,
            Y = e.WiimoteState.AccelState.Values.Y,
            Z = e.WiimoteState.AccelState.Values.Z,
        } 
    
        lastResult = newResult;
    }
    
    void MainLoop()
    {
        DateTime nextResultTime = DateTime.Now.AddMilliseconds(10);
    
        while(true)
        {
            if (DateTime.Now >= nextResultTime)
            {
                AddResult(lastResult);
                nextResultTime = nextResultTime.AddMilliseconds(10);
            }
            else
            {
                Thread.Sleep(1);
            }
        }
    }
    

    Just run MainLoop on a background thread (this might not be necessary if the wii events fire on a background thread).

    This will get you exactly 100 samples per second unless the machine just can’t handle doing AddResult that fast. In that case I think you have to lighten the load inside AddResult and do some post processing after you have captured your data – either the machine is fast enough to do it real time or it just isn’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys. I use getpixel & getdata to retrieve data from the same pic.but some
Guys in Motion Graphics are having difficulty writing a MEL script (being that none
Guys, I am using SQL Server 2000 and executing the sp_columns stored procedure to
guys which text editor is good for Rubyonrails? i m using Windows and i
guys, I'm trying to realize viewing comments on internet portal and I'm using UITableView.
guys i got a php file that use it as xml for a flash
Guys I have been trying lots of different options from cutting up to building
guys! I've got 2 forms in application - working form (frmMain) and form of
Guys, I’ve been writing code for 15+ years, but managed to avoid Web Development
Guys, Is there an easy way to return different fields names from different models

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.