Assignment 1

This is my assignment 1 for Making Things Interactive. The goal was to return statistics from two different analog sensors. My method was creating two separate arrays and averaging the statistics.


// -*-c++-*-
/*
The MIT License (MIT)

Copyright (c) 2014 J. Eric Townsend

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
static const int SENSORPIN = A0;
static const int SENSORPIN2 = A3;
static const int sampleSize = 16;
unsigned int readings[sampleSize];
unsigned int readings2[sampleSize];
unsigned int sampleIndex = 0;

static const int statusLed = 13;

void setup() {
    Serial.begin(115200);

    pinMode(SENSORPIN, INPUT);
   
    pinMode(SENSORPIN2, INPUT);
   
    pinMode(statusLed, OUTPUT);
}

void loop() {

    int photo = analogRead(SENSORPIN);
    int photo2 = analogRead(SENSORPIN2);
    readings[sampleIndex] = photo;
    readings2[sampleIndex] = photo2;

    if (sampleIndex < (sampleSize -1)) {
        sampleIndex++;
    }
    else {
      sampleIndex = 0;
    }

    int median = 0;
    int low = 1024;
    int high = 0;
    int mean = 0;
    for (int i = 0; i < sampleSize; i++) {
        mean += (readings[i] + readings2[i]);
        if (readings[i] < low) {
            low = readings[i];
        }
        if (readings2[i] < low) { low = readings2[i]; } if (readings[i] > high) {
            high = readings[i];
        }
        if (readings2[i] > high) {
            high = readings2[i];
        }
    }
    mean = mean / (sampleSize * 2);
    bubbleSort();
    median = (readings[sampleSize / 2] + readings2[sampleSize / 2] ) / 2;

// Range, mean, and median
    Serial.print("(");
    Serial.print(low);
    Serial.print(",");
    Serial.print(high);
    Serial.print(")");
    Serial.print(",");
    Serial.print(mean);
    Serial.print(",");
    Serial.println(median);

}

// tigoe's sort for an array
void bubbleSort() {
  int out, in, swapper;
  for(out=0 ; out < sampleSize; out++) {  // outer loop
    for(in=out; in<(sampleSize-1); in++) { // inner loop if( readings[in] > readings[in+1] ) {   // out of order?
        // swap them:
        swapper = readings[in];
        readings [in] = readings[in+1];
        readings[in+1] = swapper;
      }
    }
  }
}

 

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.