0

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(); 
Serial.println(time); 
}

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}; 
Serial.println(Vs[i]); 
} 
int stoptime = micros(); 
Serial.println(stoptime); 
}

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

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;
            i++)
        ;  // 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() {
    Serial.begin(9600);
}

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");
    Serial.println();

    // Wait before the next run.
    delay(1000);
}

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, privacy policy and cookie policy

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