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

 

Leave a Reply

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