Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Timing the main loop in c on Teensy++ 2.0

  1. #1

    Timing the main loop in c on Teensy++ 2.0

    Hey there! Iím working on a program that allows the user to choose from multiple scripts and the simulates Nintendo Switch controller commands to be able to grind in a video game.

    Because youíre able to choose from multiple scripts, and they can be very long, Iíd like for the teensy to be able to report back how long it took to execute the main loop while still connected to my computer so I can hardcore that disclaimer into my code. (I tried timing a script of a known length and extrapolating the time vs duration, but hit some snags)

    All the code I found for timing and delays were for teensyduino, and Iím working with code that does not use the arduino library, just straight c code. Am I still able to use this feature?

  2. #2
    Member
    Join Date
    May 2019
    Location
    Colorado, USA
    Posts
    36
    Do you have access to the number of milliseconds since startup? If so, I do this kind of thing:

    TimeIt = millis(); // millis function returns number of ms since device start or reset
    while (Ethernet.begin(mac) == 0) // 1 = success
    {
    if(millis() - TimeIt > 2000) // if a couple of seconds pass, try something else
    {
    Serial.println("Failed to configure Ethernet using DHCP");
    getFixedIpAddress();
    break;
    }
    }

    Hope that helps,
    Len

  3. #3
    Quote Originally Posted by KD0RC View Post
    Do you have access to the number of milliseconds since startup?

    I don't think so, I get the following error when trying to implement that:

    Code:
    Joystick.c:14:15: warning: implicit declaration of function 'millis' [-Wimplicit-function-declaration]
       14 |  int timeit = millis();
    The other problem is that since my code is meant to interface with the switch, I'm not sure that it's totally ready to provide any information to my computer at the moment...

    Also, is your code in c++? Unfortunately I forked a repo that relied heavily on C, so I'm kind of stuck with a bare-bones approach.

  4. #4
    Member
    Join Date
    May 2019
    Location
    Colorado, USA
    Posts
    36
    I was really just providing an approach. millis() may not be available in your library, but check around for something similar. At the very least, you may find a register for the chip you are programming that contains something similar. Yes, it is a variant of C++, but the concept should still work.

    Len

  5. #5
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,593
    Arduino and Teensy AVR cores implement millis() with TIMER0, study https://github.com/PaulStoffregen/Ar...duino/wiring.c

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •