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

  • SEARCH
  • Home
  • 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 7935573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:57:50+00:00 2026-06-03T21:57:50+00:00

Background: I have a IOIO which I am using to measure the output from

  • 0

Background: I have a IOIO which I am using to measure the output from an photodiode, this is the converted into a digital output. I need to find the frequency at which the signal changes between 1 and 0. Everything I have tryed so far has hanged my test app, any suggestions?

current code:

if(diode == 1 && frequencyFound == false){
  startTime = System.currentTimeMillis();
  while((frequencyFound == false)){
     if(diode == 0){
        while(frequencyFound == false){
            if(diode == 1){
                double endTime = System.currentTimeMillis();
                time = endTime - startTime;
                frequency = (long) (1.0 / time);
                frequencyFound = true;
            }
            Thread.sleep(100);
        }
     }
     Thread.sleep(100);
  }
}
  • 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-03T21:57:51+00:00Added an answer on June 3, 2026 at 9:57 pm

    There are a couple of issues here.

    First, Android is a multi-tasking system, and you could find your timing thread put to sleep long enough to miss some signal transitions. Is there no way to be notified of a leading (or trailing) edge transition rather than sampling the input in a loop?

    What sort of frequency are you looking at? Will a 100 ms sampling interval be fine enough?

    Don’t count on Thread.sleep() to sleep for exactly the time you specify. If the interval is too short, the system might decide to return immediately or it might round the sleep time up to a larger amount.

    Your timing loop won’t record the time to any precision better than 100ms (at best), so your estimate for the frequency will be very poor.

    Zapl is right, you MUST run this from a separate thread from your UI thread.

    Watching for a single transition will give you a very imprecise estimate of the frequency. Try something like this instead:

    // Find frequency to the nearest hz (+/- 10%)
    // It's assumed that some other process is responsible for updating the "diode"
    // variable.  "diode" must be declared volatile.
    long duration = 1000;   // 1 second
    final int interval = 100;    // sampling inteval = .1 second
    int oldState = diode;
    int count = 0;
    final long startTime = System.currentTimeMillis();
    final long endtime = startTime + duration;
    while (System.currentTimeMillis() < endtime) {
      // count all transitions, both leading and trailing
      if (diode != oldState) {
        ++count;
        oldState = diode;
      }
      Thread.sleep(interval);
    }
    // find the actual duration
    duration = System.currentTimeMillis() - startTime;
    // Compute frequency. The 0.5 term is because we were counting both leading and
    // trailing edges.
    float frequency = 0.5 * count / (duration/1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background : I have a few different threads which each need to write to
Background have a custom UITableCellView in which I'm using a custom UITextField that I've
Background I have two lists, the first is items which contains around 250 tuples,
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Background I have a ror application which is continuously recording and showing on a
Background: We have a project with many modules. We're using EntityFramework 4.2 with FluentAPI
Background: we have a Grails 1.3.7 app and are using Liquibase to manage our
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
Background: I have encoded a raw h264 file using ffmpeg. I'm trying to create
Background: I have a webpage that lists latest 30 tweets from twitter. But every

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.