Книга: Arduino: Master The Arduino Basics
Назад: Chapter 5: How to Store Data on Arduino
Дальше: Chapter 6: USB and Serial Communication on Arduino

There may be some times when you would like to get the Arduino to save and store your data. The project you have may require the ability to recognize voice patterns later on or some names and passwords depending on the type of project that you want to work with. While there are a few options available to help you store the data, the sketches that seem to be the most useful will be the VemierThermistorStore or the VemierAnalogStore. Both of these are going to work pretty much the same, and they will take your readings from the analog sensor before storing this information inside the EEPROM memory of your Arduino board.

One of the nice things about using the EEPROM memory is that it is considered non-volatile. This means that even when you take the power away from your Arduino board, the data will continue to be saved on it. The limit of points that you can use with this option though will be 511.

Now, you have to choose which option you would like to work with. Unless you want to keep track of the temperatures with your board, the VemierThermistorStore will probably not be the option that you want to go with. This means that we will focus our energy on using the VemierAnalogStore. This option will be able to handle the analog sensor that has a linear calibration. As it is written, it will be able to collect data from the dual-range force sensor each second. You will find that with some experience, it can be easier to modify this sketch for a different sensor if you would like or even for a different rate of collection.

There are going to be a few things that you will need in order to make these sketches work. These things include:

 

If you would like to be able to change the rate that the data is being collected, you will just need to change the variable that you are using under TimeBetweenReadings. Note that you are only able to store up to 511 points in your EEPROM memory while using this sketch. This means that it may be a good idea to increase the amount of time that happens in between your readings. This makes it easier to collect the data over a longer period of time rather than doing it as much as possible.

Now that we have taken a look at some of the steps that you need to take in order to start storing and collecting the data on your board, it is time to bring out the code that will help you to get this done. The code is a bit lengthy, but it is going to tell the board exactly what you want to be done. You just need to type it into your IDE, and the board will respond. The code that you need to use includes:

 

int buttonPin= 12; // analog input pin to use as a digital input

int ledPin1= 13; // digital output pin for LED 1 indicator

int debounce= 20; // ms debounce period to prevent flickering when pressing or releasing the button

int holdTime= 2000; // ms hold period: how long to wait for press+hold event

#include <EEPROM.h>

int base = 0; // the base address in EEPROM for data storage

// note first two bytes store the number of points collected

// Button variables

int buttonVal = 0; // value read from button

int buttonLast = 1; // buffered value of the button's previous state

long btnDnTime; // time the button was pressed down

long btnUpTime; // time the button was released

long ReadingTime; //time at which a reading was taken

long ElapsedTime; //time since last reading

boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered

boolean ledVal1 = false; // state of LED 1

int AnalogDataPin = A0; // this may be changed depending on circuit wiring

int Count;

float Voltage;

float SensorReading;

unsigned long Time;

long unsigned int TimeBetweenReadings = 1000; //in ms

int NumberOfPoints= 511;

 

String SensorName = "Dual-Range Force Sensor, 10 N";

String Measurement = "Force";

String ShortMeasurement = "F"; // this is a shortened version of the label

String Units = "Newtons";

float Intercept = 12.25;

float Slope = -4.9;

 

void setup()

{

// Set button input pin

pinMode(buttonPin, INPUT);

digitalWrite(buttonPin, HIGH);

pinMode(AnalogDataPin, INPUT);

pinMode(ledPin1, OUTPUT);// Set LED output pin

Serial.begin(9600);

Serial.println("VernierAnalogStore sketch");

Serial.println("Press and hold to collect new data");

Serial.println("Press button briefly to read data stored in EEPROM");

Serial.println("and send it to the Serial Monitor");

Serial.println(" ");

}

 

void loop()

{

digitalWrite(ledPin1, false);

// Read the state of the button

buttonVal = digitalRead(buttonPin);// button down is low

// Test for button pressed and store the down time

if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounce))

{

//button pressed

btnDnTime = millis();// note time of press

}

// Test for button release and store the up time

if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounce))

{

if (ignoreUp == false) ReadEEPROMData();// read data from EEPROM and send to Serial Monitor

else ignoreUp = false;

btnUpTime = millis();// note time of button release

}

// Test for button held down for longer than the hold time

if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime))

{

CollectData(); //collect new data

ignoreUp = true;

btnDnTime = millis(); // get new button down time

}

buttonLast = buttonVal;

}

Назад: Chapter 5: How to Store Data on Arduino
Дальше: Chapter 6: USB and Serial Communication on Arduino