Creating an array, assigning a value to each indices, and assessing the array

Arduino Asked by Anthony Chan on December 28, 2021

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() { 

One Answer

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

Answered by Edgar Bonet on December 28, 2021

Add your own answers!

Related Questions

How to Set RTC in ES32 using NTP server?

1  Asked on December 7, 2021 by abdul-wasey


Holding buttons (keyboard)

1  Asked on December 7, 2021 by amin-abbasi


Is it required to delete variables before going to sleep?

1  Asked on November 30, 2021 by tono-nam


Running Arduino with 2 outputs and 2 inputs

2  Asked on November 25, 2021


How to read a specific amount of data via serial?

1  Asked on November 20, 2021 by noxus-nexus


Can you access the EEPROM of an ATtiny with Arduino Code?

4  Asked on November 17, 2021 by isabel-alphonse


GPS detecting stationary?

1  Asked on November 15, 2021 by linuxferlife


What exactly does the return type do in a function?

2  Asked on November 13, 2021 by grant-scott


MPU6050 Problem with NodeMCU

1  Asked on November 5, 2021


AccelStepper library advanced codes!

1  Asked on September 25, 2021 by 2012user


Arduino I2C external 32Kb fram data organization

1  Asked on September 25, 2021 by steven-gangaram


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir