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

Leave a Reply

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