Assignment Two

Hot/Cold

I wanted to build upon and improve the previous assignment. The goal is to balance at a certain distance in order to turn on the LED, but done through a classic game of hot or cold. While it’s rather trivial, it has helped me to better understand the ultrasonic sensor and its possibilities. refining the sensor data and perfecting the timing is something that I’d like to explore further down the road.

The states are used in order to guide you through the game. State one lets you now that the game is ready, state two sets a random number for you to find using the ultrasonic sensor, and in state three…you find the number. Prompted in the serial monitor if you are getting warmer or colder.

Code:
//--------NEOPIXEL SETUP --------
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
static const int PIN = 40;
static const int NUMPIXELS = 1;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

//--------ULTRASONIC PINS--------
static const int trigPin = A0;
static const int echoPin = A1;

//--------LIGHTS--------
static const int ledPin = 4;

//--------SWITCH PIN--------
static const int switchPin = 3;

//------------------------------------------------------------------------------------

//--------Global variables--------

const bool isInterrupt = true;

// button state
int buttonState = 0; // current state of the button

// Target
int target;

// Time
unsigned long lastSampleTime = 0;
unsigned long sampleInterval = 5000; // in ms

unsigned long lastSampleTime2 = 0;
unsigned long sampleInterval2 = 250; // in ms

unsigned long lastSampleTime3 = 0;
unsigned long sampleInterval3 = 500; // in ms

// Ultrasonic
long duration;
int distance;

//------------------------------------------------------------------------------------

void SwitchPressed()
{
 if (digitalRead(switchPin) == HIGH) {
 // this is why people hate C
 buttonState == 3 ? buttonState = 0 : buttonState++;
 }
 // Serial.println(buttonState);
}

//------------------------------------------------------------------------------------

void setup() {

Serial.begin(9600);

pinMode(ledPin, OUTPUT);
 pinMode(switchPin, INPUT);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);

// attach the interrupt pin to a method
 if (isInterrupt) {
 attachInterrupt (digitalPinToInterrupt (switchPin), SwitchPressed, CHANGE);
 }
 randomSeed(analogRead(2));
 pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {
 unsigned long now = millis();

switch (buttonState) {
 case 0:
 pixelBlinkGreen();
 Serial.println ("Ready To Start Game");
 break;
 case 1:
 if (lastSampleTime + sampleInterval < now) {
 lastSampleTime = now;
 target = random(1, 150);
 Serial.print("Target Set");
 }
 pixels.setPixelColor(0, pixels.Color(0, 255, 0));
 break;
 case 2:
 pixels.setPixelColor(0, pixels.Color(255, 200, 0));
 // Serial.print("Find ");
 // Serial.println(target);
 hotCold(target);
 break;
 default:
 pixelBlinkYellow();
 Serial.println("Push Button For New Game");
 // statements
 }
}
/*
 if (buttonState == 0) {
 pixelBlinkGreen();
 Serial.println ("GREEN BLINK");
 }
 else if (buttonState == 1) {
 if (lastSampleTime + sampleInterval < now) {
 lastSampleTime = now;
 target = random(1, 150);
 Serial.print("Target = ");
 Serial.println(target);
 }
 pixelBlinkYellow();
 }
 else if (buttonState == 2) {
 pixels.setPixelColor(0, pixels.Color(255, 200, 0));
 // Serial.print("Find ");
 // Serial.println(target);
 hotCold(target);
 }
 }
*/


//------------------------------------------------------------------------------------

void hotCold(int x) {
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
 distance = duration / 10 / 2 ;

unsigned long now2 = millis();
 if (lastSampleTime2 + sampleInterval2 < now2) {
 lastSampleTime2 = now2;
 if ((distance <= (target + 5)) && (distance >= (target - 5))) {
 Serial.println("ON FIRE!");
 ledOn();
 pixelBlinkPink();
 } else if ((distance <= (target + 10)) && (distance >= (target - 10))) {
 Serial.println("Things are heating up");
 ledOff();
 } else if ((distance <= (target + 20)) && (distance >= (target - 20))) {
 Serial.println("Warmer...");
 ledOff();
 } else if ((distance <= (target + 30)) && (distance >= (target - 30))) {
 Serial.println("Cooler...");
 ledOff();
 } else if ((distance <= (target + 40)) && (distance >= (target - 40))) {
 Serial.println("Pretty Cold");
 ledOff();
 } else if ((distance <= (target + 50)) && (distance >= (target - 50))) {
 Serial.println("Ice Cold, Elsa");
 ledOff();
 } else {
 ledOff();
 //Serial.println("Something is wrong");
 }
 }
}

void pixelBlinkGreen() {
 pixels.setPixelColor(0, pixels.Color(0, 255, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(500);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(500);
}

void pixelBlinkYellow() {
 pixels.setPixelColor(0, pixels.Color(255, 200, 0, 75));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
}

void pixelBlinkWhite() {
 pixels.setPixelColor(0, pixels.Color(255, 255, 255));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(300);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
}

void pixelBlinkPink() {
 pixels.setPixelColor(0, pixels.Color(254, 6, 141));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(300);
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 delay(250);
}

void pixelOff() {
 pixels.setPixelColor(0, pixels.Color(0, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
}

void ledOn() {
 digitalWrite(ledPin, HIGH);
}

void ledOff() {
 digitalWrite(ledPin, LOW);
}
Files:

Fritzing & Arduino Files

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

 

Assignment2 – Knock Interface

Imagine your desks, doors, and floors turning into an interface that responds to knocks. What if you could control room lighting with the silent knocks? What if you could customize the knock interaction for each object? In this assignment, I built an interactive system that is able to detect and record knock patterns. Used a piezo sensor to detect the knock vibrations and a button to trigger interrupt to switch 2 different modes – detect and record. Knock detection result is displayed on a console.

The system consists of 6 states in total – wait / read / output for detect mode and wait / read / record for record mode.

Demo:

Code and fritzing here

Assignment 2: State Machines and Interrupts

Assignment 2: State Machines and Interrupts

Due 23:59, 29 January 2018.  We will discuss in class on the 30th and suggest improvements or analyze different ways to resolve your problem.

Requirements

State a question or process you would like to solve. (Ex: “control the lights in a room based on a door being opened and curtains being opened or closed.”)  Create a physical interaction using a state machine driven by inputs and interrupts that collects data, interprets the data, then displays a result via console, LEDs, or other output devices. Speakers are acceptable but must be executed in a useful manner, kinematic solutions are also welcome.

The state machine should have at least three states not including on and off states.  Declare whether your states have an order or if any state can transition to another state.

Post a short video showing the various states, I suggest vimeo or youtube.   For your Arduino and Fritzing sketches, make a zip file or gzip/tar bundled and post that to the blog.

Assignment 1 – Distance gamer

This is ment to be a toy for cats and dogs, where the closer they are to the toy, the brighter the LED is going to be. And when they put their paws on th button (it’s a photoresistor), the LED with stop for however long they leave their paws in place.

 

Material:

  • 1x  IR distance sensor
  • 1x  photoresistor
  • 1x  3.3v LED
  • 1x  10k potentiometer
  • 1x  220 resistor
  • Adafruit Metro M0 Express

Setup:

Initially prototyped with 2 photoresistor instead of ir sensor to test the data (which haven’t really happen yet)

Code:


/*
  Jean Zhang


  This is a sketch about a toy for pets (hopefully).
  This is Ver 1.0 which is a easy prototype, a proof of
  concept that the code/board/sensors works properly.

  This code is modified from the class 2 note from
  Prof. J. Eric Townsend.The lisence is as the following:



  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.

  This code is modified from the original code by Jean Zhang.
  Lots of thanks to Robert Zacharias and Professor Garth Zeglin for helping out with debugging and making the code to properly compile.




*/
const int irPin = 0;
const int photoPin = 1;
const int ledPin = 8;

const int sampleSize = 16;

int irIndex = 0;
int irReadings[sampleSize];

int photoIndex = 0;
int photoReadings[sampleSize];

int distVal, photoVal, ledVal;

const int ambientPhoto = 600;
const int tolerance = 200;



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

  pinMode(irPin, INPUT);
  pinMode(photoPin, INPUT);
  pinMode(ledPin, OUTPUT);


  // put your setup code here, to run once:

}

void loop() {
  //Reading the values from ir and photoResistor
  int photo = analogRead(photoPin);
  photoReadings[photoIndex] = photo;
  photoIndex = (photoIndex + 1) % sampleSize;

  int irDist = analogRead(irPin);
  irReadings[irIndex] = irDist;
  irIndex = (irIndex + 1) % sampleSize;

  //Get readings for irSensors
  int *pirReadings = irReadings;
  distVal = getVal(pirReadings); // feed the pointer of the array (array location)
//  photoVal = getVal(*photoReadings);



  // console
#if 0
  Serial.print("low ");
  Serial.print(low);
  Serial.print(" high ");
  Serial.print(high);
  Serial.print(" mean ");
  Serial.println(mean);
  Serial.print(" median ");
  Serial.println(median);
  delay(500);
  digitalWrite(photoPin, LOW);
  delay(500);
#endif


  int ledVal = map(distVal, 0, 1023, 0, 255);
  //0 (min) is based on the ambient setting

  if (abs(ambientPhoto - photoVal) > tolerance) {
    ledVal = 0;
  }

  analogWrite(ledPin, ledVal);



}

//Modified from class note:
int getVal(int *readings) { //return mean for now
  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(readings);
  median = readings[sampleSize / 2];

  return mean;
  //return median
}
//In C, a function cannot return an array




// tigoe's sort for an array
// since bubbleSort changes the array locally, the function doesn't
// need to return any value
void bubbleSort(int *readings) {
  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;
      }
    }
  }

}

 

Reflection:

I haven’t been able to get my hardware to work at the moment (just started testing like 10min ago). I spent a lot of time trying to debug the program. I had lots of issue with C++ since I didn’t really learn how to program in C besides reading codes for reference in my Python Projects (so much less work…).

My biggest issue is with the syntax of functions and the “pointer”  in C. I wanted to make the return median/mean part into a function and use bubbleSort multiple times on different sensor values, where instead of changing the global, it takes in an array and sort that array. The problem being when defining function,  it is defining certain type of data/value. And the take-in value need to be defined for their data type . However, when the take in value is an array, the pointer concept comes into play, and I keep getting the error like this

invalid types 'int[int]' for array subscript

After lots of Google-ing and reading comments I start to understand the concept of pointers and modify my code accordingly. (It is actually a really simple solution, kinda like the x.^x – the dot syntax in MATLAB). Instead of using :

void bubbleSort(int readings) { ........

Simply change the expression into:

void bubbleSort(int *readings) { ........ 

Note the different between “int readings” and “int *readings“.  Where *readings is a pointer and readings is not really a valid input.

I also tried to return an array for bubbleSort, which I don’t have to do that (bad habit writing python programs, it does so much for you).  In C, a function cannot return an array, instead, the way to return arraies is to use pointers, which also gave me a lot of problems when compiling.

 

Lots of thanks to Robert Zacharias and Professor Garth Zeglin for helping with the bug in the code (mainly for the pointer spaghetti) and making the code compile properly.

 

Assignment 1

My goal with this code was to make something that would measure temperature and light using a thermistor sensor and a photo sensor. I then coded in a servo motor whose rotational speed would vary based on light/temperature. I was thinking of this as a sort of electric fan that varies in speed based on the conditions of the room it is placed in. I used the code we went over in class, with adjustments, to “accomplish” this. My issue is that it’s not actually accomplished.

The code compiles, and it “uploads” to my board via my computer, but the sensors don’t respond and I keep getting error messages from my computer ex.  (every time I upload)

and  which I’ve never seen before,

and apparently the internet is as confused as I am.

I then tried taking the servo code out, and just running for the two sensors using the on-board neopixel as an output, with the same results. And apparently my computer has the same problem with the code we went over in class. Seeing as the deadline is about 15 min. away, and I’m not sure if my code or my wiring is wrong or if my computer is just being weird, I figured I’d upload the code and the wiring and re-upload past the deadline when I figure out what went wrong.

 

The servo one (with diagram):     photo_servo

The neo pixel one: photo_therm

 

Assignment 1

Circuit: The circuit in two modes i.e. photoresistor and thermistor mode. The wires can be interchanged or the pins can be changed in the code to alter the input sensor. After calibration, I use a library(https://playground.arduino.cc/Main/QuickStats) to find the median which assigns the value to the output pin via analogWrite. The library also gives access to other stats.

CODE: https://drive.google.com/open?id=1tP_MSYRrJdPCSbaLjyjgEVYBWCxyLu0I

/* basic stats with analogRead()
*
* intputs
* A0 photoresitor / A3 thermistor
*
* output
* D13 LED (Debugging)
* A9 LED (Output)
* Serial stats output
*
* Auto caliberates in the first 5 seconds (press/cover the sensor)
*
*/
// using library from https://playground.arduino.cc/Main/QuickStats
#include "QuickStats.h"
int NUMSAMPLES=10; // number of samples to take for data smoothing
float measurements[10];
const int analogInPin = A0; // Analog input A0 for photoresistor and A1 for thermistor
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

QuickStats stats; //initialize an instance of this class

// Min and Max sensor values

int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
float sensorMinF = 0.1;
float sensorMaxF = 0.0;
float smoothed = 0.0;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
caliberate();

}

// concept borrowed from https://www.arduino.cc/en/Tutorial/Calibration
void caliberate()
{
Serial.println("Caliberating ");
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

while (millis() < 5000) { sensorValue = analogRead(analogInPin); // record the maximum sensor value if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

sensorMin-= 10;
sensorMax+=10;

// volts * 10 for the thermistor to work

sensorMinF = (50.0*(float)sensorMin/1023.0);
sensorMaxF = (50.0*(float)sensorMax/1023.0);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

}

void printStats()
{
Serial.println("Descriptive Statistics");
Serial.print("Mean : ");
Serial.println(stats.average(measurements,NUMSAMPLES));
Serial.print("Minimum: ");
Serial.println(stats.minimum(measurements,NUMSAMPLES));
Serial.print("Maximum: ");
Serial.println(stats.maximum(measurements,NUMSAMPLES));
Serial.print("Standard Deviation: ");
Serial.println(stats.stdev(measurements,NUMSAMPLES));
Serial.print("Standard Error: ");
Serial.println(stats.stderror(measurements,NUMSAMPLES));
Serial.print("Coefficient of Variation (%): ");
Serial.println(stats.CV(measurements,NUMSAMPLES));
Serial.print("Median: ");
Serial.println(stats.median(measurements,NUMSAMPLES));
Serial.print("Mode: ");
Serial.println(stats.mode(measurements,NUMSAMPLES,0.00001));
}

void loop() {

for(int i=0;i<NUMSAMPLES;i++){
sensorValue = analogRead(analogInPin);
measurements[i]=(5.0*(float)sensorValue/1023.0); // convert to volts
delay(5); // Change (or remove) this delay value to alter the sampling time span.
}
smoothed=stats.median(measurements,NUMSAMPLES); // Median filter (choose which filter to use)

// caliberating for thermistor (low values)
outputValue = map(smoothed*10, sensorMinF, sensorMaxF, 0, 255);
outputValue = constrain(outputValue, 0, 255);

Serial.println(smoothed,3); // Print smoothed value to serial monitor
Serial.print("min: ");
Serial.print(sensorMinF); // Print min value to serial monitor
Serial.print(", max: ");
Serial.print(sensorMaxF); // Print max value to serial monitor
Serial.print(", ");
Serial.println(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

analogWrite(analogOutPin, outputValue);
printStats();
delay(250); // Change (or remove) this delay to alter the time between readings.
}

Link to picture: https://docs.google.com/document/d/1W2unaggIEvyaNUqzIg3yzXc4pTtWhLolyvhvZdAdMLc/edit

Circuit diagram borrowed from: https://learn.adafruit.com/experimenters-guide-for-metro/circ09-wiring 

Assignment One

Serial “Hot or Cold”

The first code combines the stat code we learned in class with an ultrasonic sensor to play an alternate version of the game “Hot or Cold.” The closer to the sensor, the hotter. The further away, the colder. It prints the results to serial.

VIDEO LINK password: MakingThingsInteractive

//--------NEOPIXEL SETUP --------
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
static const int PIN = 40;
static const int NUMPIXELS = 1;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

//--------ULTRASONIC PINS--------
static const int trigPin = A0;
static const int echoPin = A1;

//--------SAMPLE ARRAY--------
static const int sampleSize = 30;
int readings[sampleSize];
unsigned int sampleIndex = 0;

//--------TIME--------
unsigned long lastSampleTime = 0;
unsigned long sampleInterval = 100; // in ms
// we need to move these out of the loop() as well

//--------STATS--------
unsigned int median = 0;
unsigned int low = 0;
unsigned int high = 0;
unsigned int mean = 0;

//--------LIGHTS--------
static const int statusLed = 13;
unsigned int lastRedVal = 0;

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

pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(statusLed, OUTPUT);

pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

//--------SENDING OUT THE SIGNAL--------
 long duration, distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = duration / 29 / 2 ; //CENTIMETERS

// TODO what happens if you try to set a negative color?
 unsigned int redVal = 0;

// TODO
 // don't read the input on every loop to help stabilize the data
 // sampling over time is also a UI change, you have to actively
 // block light for
 // sampleSize * sampleInterval milliseconds to create a change

unsigned long now = millis();
 if (lastSampleTime + sampleInterval < now) {
 lastSampleTime = now;

// good for debugging:
 // Serial.print(now);
 // Serial.print(" ");
 //
 // Serial.println(distance);

readings[sampleIndex] = distance;
 sampleIndex < (sampleSize - 1) ? sampleIndex++ : sampleIndex = 0;

//digitalWrite(photoPin, HIGH);
 for (int i = 0; i < sampleSize; i++) {
 mean += readings[i];
 if (readings[i] < low) {
 low = readings[i];
 }
 if (readings[i] > high) {
 high = readings[i];
 }
 }

if (distance > 120) {
 Serial.println ("Frozen");
 Serial.println(" ");
 } else if (distance < 120 && distance > 70) {
 Serial.println ("Ice Cold");
 Serial.println(" ");
 } else if (distance < 70 && distance > 40) {
 Serial.println ("Pretty Cold");
 Serial.println(" ");
 } else if (distance < 40 && distance > 20) {
 Serial.println ("Cool...");
 Serial.println(" ");
 } else if (distance < 20 && distance > 15) {
 Serial.println ("Warmer...");
 Serial.println(" ");
 } else if (distance < 15 && distance > 11) {
 Serial.println ("Things are heating up!");
 Serial.println(" ");
 } else if (distance < 11 && distance > 6) {
 Serial.println ("HOT");
 Serial.println(" ");
 } else if (distance < 6 && distance > 2) {
 Serial.println ("On. Fire.");
 Serial.println(" ");
 } else {
 Serial.println (distance);
 Serial.println(" ");
 }

mean = mean / sampleSize;
 bubbleSort();
 median = readings[sampleSize / 2];
 }

redVal = map(median, 2, 150, 0, 200);

int tolerance = 3;

// This looks like it should work, right?
 // what happens if lastRedVal is 0?
 if (
 (redVal > (lastRedVal + tolerance)) || (redVal < (lastRedVal - tolerance))
 ) {

pixels.setPixelColor(0, pixels.Color(redVal, 0, 0));
 pixels.show(); // This sends the updated pixel color to the hardware.
 lastRedVal = redVal;
 }
}


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

Finding the balance

I wanted to use the values to inform color–both red and blue values. Using mapping to reverse the color values of 0-255, the more light you let in, the LED becomes more red, the less light…blue. However, if you hover in the middle and find the balance between the two values, within 10. This causes the LED to blink.

VIDEO LINK password: MakingThingsInteractive

// -*-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.
*/

/* basic stats with analogRead()

intputs
 A0 photoresitro

output
 D13 LED
 40 NEOPIXEL

*/

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 40
#define NUMPIXELS 1
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

static const int photoPin = 0;
static const int sampleSize = 16;
int readings[sampleSize];
int sampleIndex = 0;

static const int statusLed = 13;
int lastRedVal = 0;

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

pinMode(photoPin, INPUT);

pinMode(statusLed, OUTPUT);

pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {


 int photo = analogRead(photoPin);
 readings[sampleIndex] = photo;
 sampleIndex < (sampleSize - 1) ? sampleIndex++ : sampleIndex = 0;

digitalWrite(photoPin, HIGH);
 // Serial.println(photo);
 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];

int redVal = map(median, 40, 1023, 0, 100);
 int blueVal = map(median, 40, 1023, 100, 0);

Serial.print ("redVal = ");
 Serial.println (redVal);
 Serial.print ("blueVal = ");
 Serial.println (blueVal);

// use a tolerance to limit to a range
 // by making this not a const we can change it in the code
 // as needed

int tolerance = 3;

// This looks like it should work, right?
 // what happens if lastRedVal is 0?
 if (
 (redVal > (lastRedVal + tolerance))
 || (redVal < (lastRedVal - tolerance))
 ) {

pixels.setPixelColor(0, pixels.Color(redVal, 0, blueVal));
 pixels.show(); // This sends the updated pixel color to the hardware.
 lastRedVal = redVal;
 }

//----Difference code http://forum.arduino.cc/index.php?topic=105383.0 
 int difference = redVal - blueVal;

difference = abs(difference); // 'abs' is the absolute value, the result is always positive.
 Serial.println (difference);
 if (difference < 10) { // it's always higher or equal than zero
 pixels.setPixelColor(0, pixels.Color(redVal, 0, blueVal));
 pixels.show();
 delay(200);
 pixels.setPixelColor(0, 0, 0, 0);
 pixels.show();
 delay(200);
 }
 else {
 pixels.setPixelColor(0, pixels.Color(redVal, 0, blueVal));
 }

}


// 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 Two Zip Files

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 &lt; (sampleSize -1)) {
        sampleIndex++;
    }
    else {
      sampleIndex = 0;
    }

    int median = 0;
    int low = 1024;
    int high = 0;
    int mean = 0;
    for (int i = 0; i &lt; sampleSize; i++) {
        mean += (readings[i] + readings2[i]);
        if (readings[i] &lt; low) {
            low = readings[i];
        }
        if (readings2[i] &lt; low) { low = readings2[i]; } if (readings[i] &gt; high) {
            high = readings[i];
        }
        if (readings2[i] &gt; 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 &lt; sampleSize; out++) {  // outer loop
    for(in=out; in&lt;(sampleSize-1); in++) { // inner loop if( readings[in] &gt; readings[in+1] ) {   // out of order?
        // swap them:
        swapper = readings[in];
        readings [in] = readings[in+1];
        readings[in+1] = swapper;
      }
    }
  }
}