DL1.pde

/*
    Language: Wiring/Arduino
 
    Datalogger test 2
    Accelerometer on an Arduino, button for control

    The Idea:
    Set up a 'large' array in memory
    On button press, start filling the array with data
    On second button press, dump the array to serial.

    If this works, it's because Murphy wasn't paying attention.

    Eric Ayars
*/

#define DataPoints 100

byte button = 10;       // pushbutton
byte GreenLight = 13;   // indicator LED
byte RedLight = 11;     // indicator LED

// Data storage
int Data[DataPoints];       // I hope I don't need to initialize this!
int j=0;                    // for loop counting
boolean Collected = false;  // whether data has been collected

// Data collection parameters
unsigned int Interval = 10; // data collection interval, in ms
byte AccelX = 0;    // accelerometer X axis
//byte AccelY = 1;  // accelerometer Y axis
//byte AccelZ = 2;  // accelerometer Z axis

void setup() {
    // start serial port at 9600 bps:
    Serial.begin(9600);

    // Set up basic buttons
    pinMode(button, INPUT);
    pinMode(GreenLight, OUTPUT);
    pinMode(RedLight, OUTPUT);
}

void loop() {

    if (Collected) {
        // Data is collected, so do something with it.
        if (digitalRead(button)==LOW) {
            // Dump data to serial port
            digitalWrite(RedLight, HIGH);   // Turn red on
            // tell the receiver the data-collection interval
            Serial.println(Interval, DEC);
            // tell the receiver the number of points
            Serial.println(DataPoints, DEC);
            // dump the rest of the data array.
            for (j=0;j<DataPoints;j++) {
                Serial.println(Data[j], DEC);
            }
            digitalWrite(RedLight, LOW);    // Turn red off
        }
    } else {
        // Data has not been collected. Collect it!
        if (digitalRead(button)==LOW) {
            // start collection
            digitalWrite(GreenLight, HIGH); // Turn green on
            for (j=0;j<DataPoints;j++) {
                Data[j] = analogRead(AccelX);
                delay(Interval);
            }
            Collected = true;   // indicate that we've taken data
            digitalWrite(GreenLight, LOW);  // Turn green off
        }
    }
}

Generated by GNU enscript 1.6.4.