Collect analogRead data 100 times a second Problem

Hi all,

I have a vibration sensor and I read the data by using analogRead. I want to sample the value 100 times per second and put them into a set. Then use a serial monitor to display it. The code I used is here

#define VIBRATION_SENSOR  A0  
int vibdat[100];        // vibration 100 samples in one second 
int count = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(VIBRATION_SENSOR, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
   for (count=0;count<100;count++){
       vibdat[count] = analogRead(VIBRATION_SENSOR);
     }

   Serial.println(vibdat[count]);

}

The output is a large number 134536520 all the time. My question is how can I sample 100 date in a minute and put them into a set, then using serial print to display it?

Thanks guys

Serial.println(vibdat[count]);

Because the final value of count++ is 100, and you are printing from memory you do not own.

To print out the array values, you need to loop through the values with a for loop

for (count = 0; count < 100; count++) {
    Serial.print(count);
    Serial.print('\t');
    Serial.println(vibdat[count]);

Hi cattledog,

Thanks for you help ! I got the right output. And I have another question, can I convert this set of vibration data into a string like String = 123,333,456,123,999,..... I am doing a Iot project, and I would like to send the string to the cloud.

can I convert this set of vibration data into a string like String = 123,333,456,123,999,

You could, but you most likely just be wasting memory - what's wrong with the sequential printing of the values you have at the moment?

(Also note that there's a difference between a string and a String)