Variable Redefinition Speed

The question: How long does it take an Arduino, without modifications to it or the IDE's presets, to redefine a variable?

Other Question: How long does it take an Arduino to compare two values, one from an array, the other being 0. Specifically, seeing if they are the same value.

Alternatively: How can I find out more on how to calculate how long each process takes?

Simply measuring it to a tolerance of 4 microseconds is not ideal, as the error would inevitably stack and eventually make things unusable.


For the folk that might have a better idea and are more than happy to share:

I'm programming the Arduino Uno to be a USB hub, it will forward information back and forth through its digital pins.

I give this explanation because it's dealing with the nitty gritty of USB communications, which is commonly dealt with for us, with resources on it scarce, it's not implausible for someone to know the exact details of Arduino and its operation without knowing how USB works. I'm just trying to save you the time and not trying to insult your intelligence.

If you didn't know, it's not as simple as saying "If this wire is high, then make the output high," I tried, in a naive hope. Problem being that the Arduino doesn't know who's currently talking and when its important to read and when its a good time to listen, when the message is over etc. It's simply not in the mix and when Windows sees this, often resulting in a SE1 (both data lines pulled high), it shuts down the serial bus.

So I need the Arduino to read accurately, and to do this, it needs to know when to read.

I'm working at a 9600 baud rate connection, which gives a fair amount of time, it results in a bit time being around 104.16 microseconds, which gives the processor time for its simple tasks it needs to do.

I want the Arduino to confirm that a line has been pulled high for two bit times.

Here's the code snippet (with explanation)

void startUp() {
 startVar = digitalRead(SDm); // Time elapsed = Variable Redefinition time + ~4.9us
 startVar += digitalRead(SDp); // Time elapsed = (Variable Redefinition time * 2) + ~9.8us
 delayMicroseconds(x) // x being a placeholder for 104.16us - the time elapsed
}

I would have to guess the time all depends on which Arduino you are using.

And to be a bit more specific, if you are referring to C code, then you have to compile and generate a file of the assembly code that is created. Then you can follow the assembly instructions to see how many there are to accomplish the compare or what ever else you want to time.

Paul

TechNotLogy:
The question: How long does it take an Arduino, without modifications to it or the IDE's presets, to redefine a variable?

Other Question: How long does it take an Arduino to compare two values, one from an array, the other being 0. Specifically, seeing if they are the same value.

Alternatively: How can I find out more on how to calculate how long each process takes?

Simply measuring it to a tolerance of 4 microseconds is not ideal, as the error would inevitably stack and eventually make things unusable.

To compare 2 8-bit values it takes 1 cycle, 62.5 nanoseconds on an Uno, when both are in registers which thanks to optimizing compilers with 32 general purpose CPU registers to use make the case most often.

The reason that Arduino micros() has 4 usec granularity has a lot to do with the 32-bit value, AVR word length of 8-bits and the clock needing to not be the only thing running. Are you troubled with the micros() function?

According to the ATMEL docs and my highlighting:

Introduction
® ®
The Atmel picoPower ATmega328/P is a low-power CMOS 8-bit microcontroller based on the AVR® enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATmega328/P achieves throughputs close to 1MIPS per MHz. This empowers system designer to optimize the device for power consumption versus processing speed.

TechNotLogy:
The question: How long does it take an Arduino, without modifications to it or the IDE's presets, to redefine a variable?

You probably mean assign a new value. Defining a variable is when you specify a type and give it a name. e.g. unsigned int myVariable.

This depends on the processor architecture, where the values comes from and where it is written too. Additionally modern C compilers can keep variables in registers and not even write them to memory if the variable is no longer needed.

The fastest is usually copying the value from one register to another or fast RAM to another RAM memory. When the value comes from a peripheral this can take longer.

TechNotLogy:
Other Question: How long does it take an Arduino to compare two values, one from an array, the other being 0. Specifically, seeing if they are the same value.

This depends on the data type and the processor architecture. Usually processors can compare value of processors "natural" data types faster. e.g a 8 bit processor can compare 8 bit values faster than 16 or 32 bit. Also integer values are faster to compare then floating types.

TechNotLogy:
Alternatively: How can I find out more on how to calculate how long each process takes?

You can have a look at the device datasheet. This will tell you what processor architecture you have and how the internal buses and memories are working together. Then you need to look for a family or architecture datasheet or reference manual. You could use Google and use the architecture name plus "instruction set". These information are available for all common architectures. There are some really detailed manuals available that will tell you how many cycles each instruction takes.

But this is not often done. Older simpler microcontroller processors where often programmed counting cycles to create jitter free I/O operations. This has been replaced by more complex peripherals that will work together to create synchronous timings and allow more freedom in the software development process.

I am not entirely sure what you want to achieve. Maybe you could have a look at hardware timers. They can be used to measure signals without the need to have the software involved. There are many different types and you would need to have a look into your devices datasheet to find out what you can do with the ones you have. Search for Capture and Compare in your datasheet for instance.

TechNotLogy:
I'm programming the Arduino Uno to be a USB hub, it will forward information back and forth through its digital pins.

I give this explanation because it's dealing with the nitty gritty of USB communications, which is commonly dealt with for us, with resources on it scarce, it's not implausible for someone to know the exact details of Arduino and its operation without knowing how USB works. I'm just trying to save you the time and not trying to insult your intelligence.

It's been done years ago with 328P chips. One member here made USB business cards that stored and typed data into a text editor.

V-USB is a software-only implementation of a low-speed USB device for Atmel’s AVR® microcontrollers, making it possible to build USB hardware with almost any AVR® microcontroller, not requiring any additional chip.
....
Fully USB 1.1 compliant low-speed device, except handling of communication errors and electrical specifications.
Example projects demonstrate device and host driver implementations on Linux, Mac OS X and Windows.
Supports multiple endpoints: one control endpoint, two interrupt/bulk-in endpoints and up to 7 interrupt/bulk-out endpoints. (Note that the USB specification forbids bulk endpoints for low speed devices, but V-USB supports them to some degree.)
Transfer sizes up to 254 bytes by default, more as configuration option.
Comes with freely usable USB identifiers (Vendor-ID and Product-ID pairs).
Runs on any AVR microcontroller with at least 2 kB of Flash memory, 128 bytes RAM and a clock rate of at least 12 MHz.
No UART, timer, input capture unit or other special hardware is required (except one edge triggered interrupt).
Can be clocked with 12 MHz, 15 MHz, 16 MHz 18 MHz or 20 MHz crystal or from a 12.8 MHz or 16.5 MHz internal RC oscillator.
High level functionality is written in C and is well commented.
Only about 1150 to 1400 bytes code size.
You can choose the License: Open Source or commercial.

But WHY BOTHER when you can get a PJRC Teensy 2.0 or Arduino Micro or Leonardo with Atmega32U4 with built-in USB and HID commands and 2536 bytes RAM and 12 ADC read pins. And this is without going into the ARM chip boards, all USB capable at more than twice the speed, Teensy 4.0 runs 600MHz if you want fast.

If you want to host USB, look for an add-on module is your best bet.

TechNotLogy:
Alternatively: How can I find out more on how to calculate how long each process takes?

Measure the time for 1,000 or 10,000 repeats of the process and then the granularity of micros() won't be an issue.

Another way is to set a spare I/O pin HIGH at the start of the process and set it LOW at the end of the process and measure the HIGH time with an oscilloscope.

...R

TechNotLogy:

void startUp() {

startVar = digitalRead(SDm); // Time elapsed = Variable Redefinition time + ~4.9us
startVar += digitalRead(SDp); // Time elapsed = (Variable Redefinition time * 2) + ~9.8us
delayMicroseconds(x) // x being a placeholder for 104.16us - the time elapsed
}

If you want to read pins fast you don't use the beginner-safe Arduino commands.

Use direct manipulation of the 3 registers of the port your pin is in.
You read the PINx register (x is the port letter, like A) to get 8 pin states, mask to get your pin. 2 or 3 cycles.
0 is false, not-0 is true, there is no need to shift the pin bit down as there's only 1 if any, 0 is LOW, not-0 is HIGH.