How to start a "for loop" without using a void loop ()

I am learning Arduino IDE, hence I need to upload my code often , however, the void loop () in the beginning keeps the loaded code running continuously, which occupies the serial port and prevents from uploading new code. I have to change USB cable from one port to another for uploading a new code, some times, I had to do it a few times to upload a new code successful. it is really boring for a new user. I fully understand that eventually, I need the void loop () to run finalized code.

When I put a for loop inside the void loop (), no error, however, when the void loop is removed, I got an error,

expected unqualified ID before 'for'

I added a void before the "for", it did not help.

Could some one let me know how to get it around?
Thanks

Code without void loop()

#include <Wire.h>
void setup() {
  Serial.begin(9600);        
  Wire.begin();           
  Wire.setClock(100000);
}
  byte addr = 2;
  byte high;
  byte low;
  byte reg_addr;
  unsigned int temp1;
  
  for (byte reg_addr = 0; reg_addr <= 20; reg_addr ++) { 
  Wire.beginTransmission(addr);  
  Wire.write(reg_addr);  
  Wire.endTransmission();
  
  Wire.requestFrom(addr, 2);   
  if (2 <= Wire.available()) { 
  high = Wire.read();  
  low = Wire.read(); 
  temp1 = high*256 + low;  
  Serial.print("data from address  "); 
  Serial.print(reg_addr, HEX);
  Serial.print("  ");
  Serial.println(temp1, HEX);   // print the data in hexdecimal value
  Serial.println(" ");   // print a blank line
     }
  }

Code with the void loop ()

#include <Wire.h>
void setup() {
  Serial.begin(9600);        
  Wire.begin();           
  Wire.setClock(100000);
}
  byte addr = 2;
  byte high;
  byte low;
  byte reg_addr;
  unsigned int temp1;
  
  void loop () {
  for (byte reg_addr = 0; reg_addr <= 20; reg_addr ++) { 
  Wire.beginTransmission(addr);  
  Wire.write(reg_addr);  
  Wire.endTransmission();
  
  Wire.requestFrom(addr, 2);   
  if (2 <= Wire.available()) { 
  high = Wire.read();  
  low = Wire.read(); 
  temp1 = high*256 + low;  
  Serial.print("data from address  "); 
  Serial.print(reg_addr, HEX);
  Serial.print("  ");
  Serial.println(temp1, HEX);   // print the data in hexdecimal value
  Serial.println(" ");   // print a blank line
     }
   }
  }

Wire.endTransmission() returns 0 upon success, may be you want to double check that before continuing with your Wire.requestFrom()?

#include <Wire.h>

const byte addr = 2;

void setup() {
  Serial.begin(115200); // change serial monitor to 115200 bauds
  Wire.begin();
  Wire.setClock(100000);
}

void loop () {
  byte high;
  byte low;
  unsigned int temp1;

  for (byte reg_addr = 0; reg_addr <= 20; reg_addr ++) {
    Wire.beginTransmission(addr);
    Wire.write(reg_addr);
    if (Wire.endTransmission() == 0) {
      if (Wire.requestFrom(addr, (uint8_t) 2) == 2) {
        high = Wire.read();
        low = Wire.read();
        temp1 = high * 256 + low;
        Serial.print(F("data from address  0x"));
        Serial.print(reg_addr, HEX);
        Serial.write('\t');
        Serial.println(temp1, HEX);   // print the data in hexdecimal value
        Serial.println();   // print a blank line
      }
    }
  }
  delay(10); // run this @100Hz give or take
}

and do you need to run that full speed ? a delay(10) at the end of the loop would help ?
also, change serial monitor to 115200 bauds (at minimum - may be go to 500,000 or 1,000,000)

and if you want to only Read once, don't put that in the loop, have your for loop in the setup()

I'm not sure about the part where you claimed you had to change usb cable etc to be able to upload code but you can always add while(1); to the end of your loop(), behind your code in loop() to stop it from repeating, or add it to the end of setup() behind your code to stop it from running loop()

Wire.requestFrom() is a complete transaction. You don't need this line. Just read the data and move on.

  if (2 <= Wire.available()) {
void setup() {  
}

void loop() {
}

is the real bare minimum every sketch needs to be compilable.

void setup() {
}
is the function that runs one time after power is connected

void loop() {

}
is the function that loops infinitely.
if you want to stop loop from looping again and again

void loop() {

while(1); // keep the Arduino running inside this while-loop for-ever
}

Take a look into this tutorial:
Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Hi Stefan,

Thank you!
I have gone through the tutorial quickly and it helps me a lot.
Gu

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.