How arudino execute commands

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..

 unsigned long msec = 0;
 float time = 0.0;
int sample = 0;
float totalCharge = 0.0;
float averageAmps = 0.0;
float ampSeconds = 0.0;
float ampHours = 0.0;
float wattHours = 0.0;
float amps = 0.0;
int i,k;
float current1, current2, current;
float wh;
unsigned long previousMillis = 0;  
unsigned long currentMillis;
long interval=1;

void setup() {
  Serial.begin(9600);
}

void loop() {

  int resistance = analogRead(A0);
  float Uresistance = resistance* (5.0 / 1023.0);
    
  int resistancediode = analogRead(A2);
  float Uresistancediode = resistancediode * (5.0 / 1023.0);
  
  float current1=Uresistance/0.47;
   for (long i=0; i <= 1000000; i++){
  current2=current2+current1;
  if (i=1000000) current=current2/1000000;
   }
   


   
  float voltage=Uresistancediode - Uresistance;
  float wattage=current*voltage;
  
   sample = sample + 1;
  msec = millis();
 time = (float) msec / 1000;
totalCharge = totalCharge + current;
averageAmps = totalCharge / sample;
ampSeconds = averageAmps*time;
ampHours = ampSeconds/3600;
float wattSeconds = voltage * ampSeconds;
wattHours = voltage * ampHours;
  
  
 wh=wh + ((wattage*time)/1);
 


 currentMillis = millis();
if(currentMillis - previousMillis > 1000) {
previousMillis = currentMillis;

  Serial.print("Current: ");
  Serial.print(current);
  Serial.println(" A");
  Serial.print("Voltage: ");
    Serial.print(voltage);
    Serial.println(" V");
      Serial.print("Wattage: ");
        Serial.print(wattage);
        Serial.println(" W");
        Serial.print("Consumption: ");
        Serial.print(wh);
        Serial.println(" Ws");
        Serial.print("          ");
        Serial.print(wh/3600);
        Serial.println(" Wh"); 
        Serial.print("Time of run: ");
         Serial.print(time);
        Serial.println(" s");
          Serial.println();
}
}
for (long i=0; i <= 1000000; i++){
  current2=current2+current1;

Or you could use the multiplication operator.

Im measuring consumption and wattage

So two figures?
What is the differance?

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.

...R

  if (i=1000000) current=current2/1000000;

Basic errors there - = is not the equality operator, == is.

1000000 is too large for int, so you should write 1000000L (a long
constant).