collecting data for specific interval of time

Hello All,

I want to run my arduino code for specific amount of time say for eg 1s and collect data from analog pin with specific sampling frequency for eg 100 samples/sec.

So far this is the code I wrote:

int pin1 = A0;
int val1 = 0;
long lastMillis = 0;
long loops = 0;

void setup(){
Serial.begin(9600);
}
void loop()
{
long currentMillis = millis();
loops++;
if(currentMillis - lastMillis <= 1000){
Serial.print("Loops last second:");
Serial.println(loops);
lastMillis = currentMillis;
loops = 0;
for ( int i=0; i <100; i++)
{
val1 = analogRead(pin1);
Serial.println(val1);
}}}

This code will run and prints 100 values once and stops. I want the code to be running continuously and it has to print 100 values every 1s. Please help me out to correct this program.

i found your code seems working fine. it wont stops.

First, lastMillis and currentMillis should be type, unsigned long
Second, use CTRL T to format your code.
Third, Attach your code using the </> icon on the left side of the posting menu.

sketch_dec16a.ino (572 Bytes)