Stopping Sampling after 2 seconds

Hello,

I am wondering about the best way to stop a sample on an analog pin after 2 seconds.

I am playing about with an accelerometer and the Uno, and I am currently saving the output to the EEPROM, but rather than gather a constant stream of data which will get over written, I would like to gather a few seconds of information at a time.

Thanks, Seán

MoonBeam: Hello,

I am wondering about the best way to stop a sample on an analog pin after 2 seconds.

I have no idea what you actually mean by this.

I am playing about with an accelerometer and the Uno, and I am currently saving the output to the EEPROM, but rather than gather a constant stream of data which will get over written, I would like to gather a few seconds of information at a time.

Information is measured in bytes, not seconds. How many bytes?

Thanks, Seán

Hi,

Thanks for the reply.

I will try and word my question better.

If you are reading in information on an analog pin, am I correct in saying you are carrying out an analog to digital conversion?

I would like to know how to stop the conversion process after a set time.

For example, if I am sampling at 200Hz, am I able to stop the conversion process after 400 samples?

I am afraid, as yet, I don't know how many bytes would be stored.

Thanks, Seán

If you are reading in information on an analog pin, am I correct in saying you are carrying out an analog to digital conversion?

Yes.

I would like to know how to stop the conversion process after a set time.

Each conversion is one call to analogRead()

For example, if I am sampling at 200Hz, am I able to stop the conversion process after 400 samples?

You can do anything you like, you just have to program it. Sounds like you have no code to post yet? Have you looked at the examples using analogRead()?

My code is below:

#include<EEPROM.h>

int addr = 0;           // The current address in the EEPROM (i.e. which byte we will write to next)
int analogPin = 3;      
int val = 0;            // Variable to store the value read

void setup(){
  Serial.begin(9600);          // Setup serial
}

void loop(){
  val = analogRead(analogPin);    // Read the input pin. Div by 4 because ADC range is 0 to 1023, EEPROM is 0 to 255
  // Serial.print(val);
  // Serial.println();
  // Serial.println(val);         
  EEPROM.write(addr,val);           // Writes values. Information will remain when power is off.
  addr = addr + 1;                  // Moves to the next address location

    if(addr == EEPROM.length()){
      addr = 0;
    }
  delay(100);
}

So, from what you are saying, since I have the analogRead inside an infinite look, I need to put in it a look of it’s own for the amount of samples I would like?

I will give that a go.

Thanks,
Seán

An Uno has 1024 bytes of EEPROM so you can not store more than 20 bits (2 1/2 bytes) of data per sample at 400 samples. It is not what the EEPROM is meant for: it’s for storing settings and so that you need between restarts. Data is best sent out to another device that can store it all (such as an external computer, or a directly connected SD storage).

Stopping after taking 400 samples is easy, just count them:

sampleCount = 0;

void loop() {
  if (sampleCount < 400) {
    sample = readSample();
    writeSample(sample);
    sampleCount++;
  }
}
[/count]

Hi,

Thanks for the reply. I hadn't got as far as wondering about the limit, so that's good to know.

I have an external microSD card breakout board ordered, so I will use that.

I will do some research and see if I can figure out how to save data to a folder on the hard drive.

Thanks again, Seán

MoonBeam: Thanks for the reply. I hadn't got as far as wondering about the limit, so that's good to know.

Make it a habit of wondering about limits first when working with highly limited devices like Arduinos. Very soon you'll know the limits, and that's a great help in designing your projects.

Suggest you also make it a habit to check the Arduino reference page https://www.arduino.cc/en/Reference/HomePage when trying to solve problems like the one in your first post. Look especially hard at the control structures section. Very soon, you'll learn a lot about programming, and that's a great help in having fun with the Arduino. :)

PS: you don't have to know everything on the Arduino reference page, but you should take a quick peek at most of the items, so you have a basic understanding of what's available.