Assignment 2

Goal: The goal for my state machine was to build a basic alert system that tells you when there is someone/something in the proximity. The switch is the state change that turns the entire alert system on or off. This is essential for the edge case when you are expecting many people and do not need to be alerted. There were three LED colors: yellow for idle (nothing in the proximity), red for something physically there and it is light out, and green for something physically there and it is dark out.

Reflection: If I were to update/re-do this, I would change the code so the interrupt is the proximity sensor rather than the switch. I would also change the light so the green represents light and proximity and red represents dark and proximity. This would align with my overarching ideal goal to make a front door security system. I would also change the light/dark reading to a boolean.

(My unstable hand is to blame for the flickering green light)


 // Define constant values.

// The wiring assignment.
#include

static const int SWITCH_PIN = 3;
static const int LED1Pin = 4;
static const int LED2Pin = 5;
static const int LED3Pin = 13;
static const int lightSensor = A0;
bool LEDState = false;
static const int sampleSize = 16;
unsigned int readings[sampleSize];
unsigned int sampleIndex = 0;
static const int nighttime = 50;
const int TRIG_PIN = 9;
const int ECHO_PIN = 8;
const int MAX_DISTANCE = 700;

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

void SwitchPressed()
{
if (digitalRead(SWITCH_PIN) == 1) {
LEDState = true;
}
else{
LEDState = false;
}
}

// ================================================================================
// Configure the hardware once after booting up. This runs once after pressing
// reset or powering up the board.

void setup()
{
// Initialize the serial UART at 9600 bits per second.

Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);

pinMode(SWITCH_PIN, INPUT);

pinMode(LED1Pin, OUTPUT);

pinMode(LED2Pin, OUTPUT);

pinMode(LED3Pin, OUTPUT);

pinMode(lightSensor, INPUT);

pinMode(TRIG_PIN, OUTPUT);

digitalWrite(TRIG_PIN, LOW);

// Initialize the echo pin for input.
pinMode(ECHO_PIN, INPUT);

attachInterrupt (digitalPinToInterrupt (SWITCH_PIN), SwitchPressed, CHANGE);

}

// ================================================================================
// Run one iteration of the main event loop. The Arduino system will call this
// function over and over forever.

void loop()
{

int photo = analogRead(lightSensor);

readings[sampleIndex] = photo;

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];
if (readings[i] < low) { low = readings[i]; } if (readings[i] > high) {
high = readings[i];
}
}
mean = mean / sampleSize;
bubbleSort();
median = readings[sampleSize / 2];

//TURNED ON, IDLE STATE

if (LEDState && sonar.ping_median() < 700 && mean > nighttime ) {
digitalWrite(LED1Pin, 255);
digitalWrite(LED2Pin, 0);
digitalWrite(LED3Pin, 0);
}

else if (LEDState && sonar.ping_median() < 700 && mean < nighttime ) {
digitalWrite(LED1Pin, 0);
digitalWrite(LED2Pin, 0);
digitalWrite(LED3Pin, 255);
}

//TURNED OFF

else if (!LEDState) {
digitalWrite(LED2Pin, 0);
digitalWrite(LED3Pin, 0);
digitalWrite(LED1Pin, 0);
}

else {
digitalWrite(LED2Pin, 255);
digitalWrite(LED3Pin, 0);
digitalWrite(LED1Pin, 0);
}

delay(500);
Serial.println("mean:");
Serial.println(mean);
Serial.println("median:");
Serial.println(sonar.ping_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;
}
}
}
}

 

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;
      }
    }
  }
}