So, I have a very basic project where I cycle through 8 IR individual transmitters. After setting the individual transmitter high, I cycle through all 8 IR receivers and print their values, RX_CYCLES many times. I need a huge amount, quality data from these loops to train a neural net, so I want to optimize my code to where I can analogRead each receiver the fastest, but keep the maximum resolution possible.

I'm having trouble understanding Teensy's documentation and provided libraries and finding any relevant resources that are clear enough for me to weave through the jargon.

My questions are:

  1. What would be or how do I go about calculating the optimization or balance of the fastest baudrate I can match with the Teensy 3.6, with the highest quality resolution possible?
  2. How to optimize analogRead? I've read that the fastest way is to use continuous analogRead instead of single analogRead? I'm having trouble finding clear examples of what this actually does and how to use it effectively. I definitely could be wrong about it this.
  3. Am I outputting data the best way possible? I was wondering if there would be any reason in my case to use print() or println() or if I should be using flush() after every cycle

These are a lot of questions, and I don't expect anyone to optimize my code for me, so if you can help point me in the direction of clear material I can use to direct me in this problem.

I'd also really appreciate any other suggestions or insight anyone would like to offer that I may be missing.

This is my very basic code:

const uint8_t RX_CYCLES = 1;
const uint8_t RX_START = 14, RX_END = 21;
const uint8_t TX_START = 0, TX_END = 7;

void setup() {
    while (!Serial);
    for (uint8_t reciever = RX_START; reciever <= RX_END; reciever++) pinMode(reciever, INPUT);
    for (uint8_t transmitter = TX_START; transmitter <= TX_END; transmitter++) pinMode(transmitter, OUTPUT);

void loop() {
    for (uint8_t transmitter = TX_START; transmitter <= TX_END; transmitter++) {
        digitalWriteFast(transmitter, HIGH);
        for (uint8_t i = 0; i < RX_CYCLES; i++) {
            for (uint8_t reciever = RX_START; reciever <= RX_END; reciever++) Serial.println(analogRead(reciever));
        digitalWriteFast(transmitter, LOW);
  • I was thinking of using DMA. It also seems that analogRead is pretty slow on the teensy because it calibrates itself every times it's called. This thread seems to contain a lot of useful info: forum.pjrc.com/threads/…
    – Gerben
    Mar 7, 2019 at 19:27
  • @Gerben if the optimization would be turning off calibration before every call, wouldn't this produce uncalibrated readings which inherently agains't anyones interest?
    – dandev
    Mar 7, 2019 at 19:33
  • please correct me if i'm wrong, but it seems to me that you have not explained what you are measuring and what parameter you are referring to when you say "maximum resolution" ..... just because you are using analogRead, it cannot be assumed that you are only measuring analog levels of some physical property
    – jsotola
    Mar 8, 2019 at 6:24
  • @jsotola I meant to imply that the IR receivers where measuring the output of the current IR transmitter in the loop. I realize now that's not so obvious. From what I understand resolution refers to the specificity of data, so having the highest resolution possible is the difference between 1024 and 1023.67. I'm going to break this question up, I think this may be too much and broad to ask.
    – dandev
    Mar 8, 2019 at 13:03


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.