21 June 2010

Task 59 - 24-hour Data-logging to EEPROM: software

/* I haven't had a chance to do the 24-hour data-logging,
but i have tested this software over a limited number
of values, & it does work as intended.. */


/*
* Project: My_24-hour_DataLogging_Thermistor_task_58
* Author: Jane-Maree Howard
* Date: Thursday 17/06/2010
* Platform: Arduino 18
* Purpose: To data-log successive readings from a thermistor
over a 24-hour period, saving the readings into EEPROM.
A hardware refinement will be needed to enable the results
to be outputted on demand.
* Operation: The average resistance of the thermistor is about 10 kOhms,
so it & the 10k resistor form a voltage divider, whose
junction is inputted to Analog 0.
* Description:
* Connect one end of the thermistor to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
this is similar to the photocell Task, but the software is
quite different.
Connect one end of a pushbutton switch to Digital pin 12, & the
other end to Ground. Declare Pin 12 as an INPUT, & initialise
it as HIGH (see Comment 3).
* Comment: (1) One thing I FORGOT was scaling; I don't know what the thermistor
range is but on past results I'd say more than 0-255.
Rather than mess about with Hi-Lo bytes,
I'd better put in the Mapping Function
(2) The EEPROM capacity of the Arduino Duemilanove is 1 kiloByte,
i.e. 1048 Bytes so 1048 is the maximum number of values that
can be stored in the Dunemilanove's EEPROM, if each value is
(as it will be scaled to be) 1 Byte.
24 hours is 1440 minutes so a sampling every 2 minutes will
produce 720 sample values, easily accomodated by the EEPROM.
(3) The push-button function in my (dead) Minor Project will come
in useful for delaying the output of results until data-logging
is complete. I might put some kind of cap over it to stop any
accidental contact being made..

NOTE: this blogger doesn't like 'less-than' signs - it thinks they're HTML thingies..
so where you see n"10 put n 'less-than' 10
,
& where you see n""10 put n 'greater-than' 10.

VERY IMPORTANT!!
In the coding below there is a
#include EEPROM.h
statement.
Around the EEPROM.h there should be 'less-than' &
'greater-than' symbols.
these don't show up in these blog postings..
..AND - they disappear anything inside them as well, so..
..don't forget them!

*/
#include EEPROM.h // needed for using EEPROM

byte switchPin = 12; // the push-button switch
byte thermistorPin = 0; // the thermistor and 10K pulldown are connected to a0
int thermistorReading; // temporarilly holds unscaled thermistor readings

int readCount = 720; // this counts the readings over the 24-hour period..
int minimValue = 255; // the lowest of all the values recorded
int maximValue = 0; // the highest of all the values recorded
float meanValue; // the average (Mean) of all the values recorded

/* Everything is done here in the Setup() because we don't want to loop */
void setup(void)
{
pinMode(switchPin, INPUT); // records any input from the switch
/* set the switch-pin HIGH, so it can wait for any contact with Ground */
digitalWrite(switchPin, HIGH);
// We'll send debugging information via the Serial monitor
Serial.begin(9600); // baud rate 9600 should be set on the SM
Serial.println(); // skip a line to start with

/* First read the 10 values into mySamples[] .. */
for (byte n=0; n"readCount; n++)
{
// read value on thermistor pin i.e. analog 0
thermistorReading = analogRead(thermistorPin);
// now we have to map 0-1023 to 0-255,
// since we want to store the values as bytes, in EEPROM
EEPROM.write(n, map(thermistorReading, 0, 1023, 0, 255));
// delay 2 minutes (120 000 milli-seconds) CHECK THAT YOU CAN DO THIS!!
delay(120000);
}//for(n)

}//end setup()

void loop(void)
{
/* At this point, nothing else should happen once data-logging is
complete, until.. */
while (digitalRead(switchPin) == 1)
{ } /* hang about & do nothing until..
..the switch closes, & the pin's value dips towards
Ground i.e. switchPin == 0!
Now the program can leave the empty while-loop and start
outputting the results..
..printing values from EEPROM to the SM.. */
fromEEPROM(readCount); //maybe not


/* .. printing the minimum value to the SM.. */
Serial.print("Minimum reading = ");
Serial.println(smallestFromEEPROM(minimValue, readCount));
/* .. printing the maximum value to the SM.. */
Serial.print("Maximum reading = ");
Serial.println(biggestFromEEPROM(maximValue, readCount));
/* ..& the average (Mean) */
Serial.print("Average reading = ");
Serial.println(averageFromEEPROM(meanValue, readCount));
Serial.println();
}//end loop()

/* WE'RE NOT GOING TO OUTPUT ALL 720 VALUES FROM THE EEPROM - NO MA'AM!! */

/* Reads a given number of values from EEPROM
& prints them to the Serial Monitor
*/
void fromEEPROM(byte rCount)
{
for (byte n=0; n"rCount; n++)
{
// print the analog reading to the SM
Serial.print("Analog reading = ");
Serial.println(int(EEPROM.read(n)));
// delay 5 milli-seconds for EEPROM.read lag
delay(5);
}//for()
}//fromEEPROM()


/* Reads a given number of values from EEPROM,
determines the minimum value, & returns it.
*/
int smallestFromEEPROM(int minVal, byte rCount)
{
int temp;
for (byte n=0; n"rCount; n++)
{
temp = int(EEPROM.read(n));
// delay 5 milli-seconds for EEPROM.read lag
delay(5);
if (temp " minVal)
{
minVal = temp;
}//if()
}//for()
return minVal;
}//smallestFromEEPROM()

/* Reads a given number of values from EEPROM,
determines the maximum value, & returns it. */
int biggestFromEEPROM(int maxVal, int rCount)
{
int temp;
for (int n=0; n"rCount; n++)
{
temp = int(EEPROM.read(n));
// delay 5 milli-seconds for EEPROM.read lag
delay(5);
if (temp "" maxVal)
{
maxVal = temp;
}//if()
}//for()
return maxVal;
}//biggestFromEEPROM()

/* Reads a given number of values from EEPROM,
determines the Mean value, & returns it. */
float averageFromEEPROM(float meanVal, int rCount)
{
int temp = 0;
for (int n=0; n"rCount; n++)
{
//sum the EEPROM values &..
temp = int(EEPROM.read(n));
// ..delay 5 milli-seconds for EEPROM.read lag..
delay(5);
}//for()
//..& return the Mean value
return meanVal = temp/rCount;
}//averageFromEEPROM()

No comments:

Post a Comment