Hi, Im interested in how arudino works. Arduino runing in cycles, but if I write for loop, does it finish in one cycle or it do just one loop in cyle?
I tried count number of cycles by i=i+1, small program have thousand cycles per second, my bigger program have 4 cycles per second (wattmeter, just read two voltages and some math), it looks strange.
The Arduino will execute all of the instructions inside a for loop each time through the loop so it is no surprise that putting more instructions in the loop causes it to perform less loops per second. Having said that, the difference that you are seeing is dramatic which must mean that the extra instructions are taking a long time to execute.
Please post your program with the extra instructions in the loop.
The arduino runs at sixteen million cycles a second.
Each instruction of machine code takes one or two cycles.
However each line of C code is translated into many instructions. Some times it is a small number like less than ten, sometimes a medium number like sixty for a digital write. Other likes like a floating point multiply can take hundreds or thousands of cycles.
This conversion between C code and machine code is done by the compiler before the machine code is down loaded into your arduino.
Main reason why Im interested in this. Im measuring consumption and wattage of blinking led light. So I need average values for current and voltage (like from 5 seconds). So I thought if I make for loop, which will repeat like for milion times it will make average value..
This sounds like homework, certainly it is nothing practical as you are better off just measuring the duty cycle of the LED and calculating the power consumption from that.
Serial.print commands are very slow. If you can get them outside your loop you will get many more loops per second.
If you want to print variables that are collected with each iteration of loop save them to an array within loop and then print them later. You could probably have an array to save 50 or 100 values but you won't have enough memory to save 1000.