I am trying to create an unassigned array of size 512 and then assigning a reading to each indices. I want to get the start time before and after the loop and only run it once. Then I want to know how to retrieve the readings and serialprint the values after the loop. So far this is what I have but I don't think it is running as I want it to. I was able to get the start time but I'm not sure if it is running afterwards. Any help is greatly appreciated. Thanks!

void setup() { 
int Vs[512] = {}; 
int analogPin = A0; // turn pin A0 on 
pinMode(9, INPUT); 
Serial.begin(9600); // setup serial 
analogWrite(9, pow(2, 7)); //LED power 0 
int time = micros(); 

void loop() { 
int i; 
for (int i = 0; i <= 511; i++); { //loop setup for 512 
int c = analogRead(A0) * (5.000/1024.000); 
int Vs[i] = {c}; 
int stoptime = micros(); 

void stop() { 
  • if you want to run the code only once, then put the code in the setup() block ... leave the loop() block empty
    – jsotola
    Commented Oct 28, 2019 at 5:13
  • why do you have a stop() function? ... you never call it
    – jsotola
    Commented Oct 28, 2019 at 5:16
  • make the array global. and it is too large for Uno. and Vs[i] = analogRead(A0) * (5.000/1024.000);
    – Juraj
    Commented Oct 28, 2019 at 6:27

1 Answer 1


There are multiple issues with your program. The most obvious is that, if you define a variable multiple times, you end up with mutliple different variables that share the same name. For example:

void setup() { 
    int Vs[512] = {};  // this Vs is local to setup()

void loop() {
    int i;  // this i is local to loop()
    for (int i = 0;  // this other i is visible within the for loop only
            i <= 511;
        ;  // this is the body of the for loop
        int Vs[i] = {c};  // this Vs is local to loop()

Another issue I pointed out in the snippet above is that a semicolon by itself is an empty (i.e. do-nothing) statement. Thus you for loop is empty.

There is no point in defining a constant such as analogPin = A0 if you don't use it.

The value returned by micros() is not an int, it's an unsigned log.

If you store a voltage in volts in an int, you get a very poor resolution of one volt. You could store in a float, but that would fill up the memory of your Arduino. I would rather store the raw readings, and convert to volts only for printing.

Serial.println() takes time, and you are doing this during the timed part of your code, which is probably unintended.

Here is a version of your program that may better reflect your intentions. It takes 512 analog readings, stores them in an array, then prints them and reports the time taken to do the readings. Then it waits for one second and repeats the whole thing again.

const uint8_t analogPin = A0;
const int readings_count = 512;
int Vs[readings_count];

void setup() {

void loop() {
    // Take a run of analog readings.
    unsigned long start_time = micros();
    for (int i = 0; i < readings_count; i++) {
        Vs[i] = analogRead(analogPin);
    unsigned long stop_time = micros();

    // Print the readings.
    Serial.println("Voltage readings:");
    for (int i = 0; i < readings_count; i++) {
        Serial.println(5.0/1024 * Vs[i]);
    Serial.print("Data recorded in ");
    Serial.print(stop_time - start_time);
    Serial.println(" µs");

    // Wait before the next run.

If you really want to run only one iteration of the process, then you can just replace delay(1000); by exit(0);. Alternatively, move the body of loop() to the end of setup(), and keep an empty loop().

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.