Stupid Pet Trick – Creepy Box

Date: Friday, March 10th, 2017

Instructor: Dan

Author: Andrew Huang

Materials: Arduino, Cardboard Box, Wires, Motion sensor, Light sensor, resistors, Leds.

Inspiration:

Originally, I wanted to do a door / security type project where the motor is opened by some kind of interface I made through the led’s or wires. However, my friend Kevin and I agreed that it wasn’t truly in  the spirit of IMA. After all, there was already several safe’s and boxes made which made it somewhat of an uncreative idea. I needed to think outside of the box (not really). From the box idea, I just messed with the LED’s in order to configure different light configurations. I started with a strobe mode, a On mode, and an Off mode. I figured I would have the opening of the box trigger some sort of an event. I thought of it like a treasure chest, or an idol, which it ended up becoming. Having no further insight come to me, I decided to proceed on with that idea.

685544895 688689224

The Coding for my project was not too difficult… Perhaps because my idea lacked creativity and therefore my code reflected it’s limited function.

The Video shows the kind of strobing pattern that appears.

Problems:  

The biggest challenges I had was the fact that the Serial.write for the Arduino simply did not interface well with Arduino. Given the fact that I needed the threshold voltage to be above 0 when the box was open did not help. The sensor would not even go off unless I shined my light on it.  In the end, this phone light shining really ruined the sort of presentation I was going for. Additionally, the processing code doesn’t accurately read the code I had wrote to the Serial port. Odd negative numbers would appear, even after I had mapped it from ascii.

Conclusion:

Next Project, I will think of an idea which is more tangible and easier to imagine. I found that a lot of people didn’t know what my project even was until I explained it to them. Additionally, I could have placed the box in a well hidden manner inside the box so that I could see what was going on outside. Perhaps a better use of sensors was warranted too. I could have used the ultrasonic sensor to detect if someone was opening the box from the outside. Overall, the project turned out somewhat well, but it didn’t get the full amount of creativity I wanted.

/* Author: Andrew Huang
 * IMA: Stupid Pet Project
 */

#include <Wire.h>
#include <Digital_Light_TSL2561.h>

const int LIGHT_PORT[] = {8, 9, 10, 11, 12};
    
void setup()
    {
      Wire.begin();
      Serial.begin(9600);
      TSL2561.init();
      pinMode(LIGHT_PORT[0], OUTPUT);
      pinMode(LIGHT_PORT[1], OUTPUT);
      pinMode(LIGHT_PORT[2], OUTPUT);
      pinMode(LIGHT_PORT[3], OUTPUT);
      pinMode(LIGHT_PORT[4], OUTPUT);
    }
void loop()
    {
      Serial.print("The Light value is: ");
      int lightLevel = TSL2561.readVisibleLux();
      Serial.println(lightLevel);
      if (lightLevel < 20){
          if (lightLevel > 0){
            Serial.write(lightLevel);
            delay(10);
          }
        strobe(lightLevel);
      } else {
        for (int i = 0; i < 10; i++){
          allOn();
          delay(100);
          allOff();
          delay(1000);
        }
      }
    }
void strobe(int rate){
  for (int i=0; i < 5; i++){
        digitalWrite(LIGHT_PORT[i], HIGH);
        Serial.print(-1 * rate + 800);
        int x = 500 - rate;
        if (x > 0){
          delay(x);
        } else{
          delay(0);
        }
        digitalWrite(LIGHT_PORT[i], LOW);
    }
}
void allOn(){
    for (int i = 0; i < 5; i ++){
    digitalWrite(LIGHT_PORT[i], HIGH);
    }
}
void allOff(){
    for (int i = 0; i < 5; i ++){
    digitalWrite(LIGHT_PORT[i], LOW);
    }
}

/////////////////////////////////////////////////


//Music Playback


import processing.sound.*;
SoundFile file;

import processing.serial.*;


Serial myPort;
int valueFromArduino;

void setup() {
  size(640, 360);
  background(255);

  printArray(Serial.list());
  // this prints out the list of all available serial ports on your computer.
  
  myPort = new Serial(this, Serial.list()[ 5 ], 9600);
    
  // Load a soundfile from the /data folder of the sketch and play it back
  
}      
void play(){
  file = new SoundFile(this, "stranger.mp3");
  file.play();
  delay(10000);
  file.stop();
}


void draw() {
  while ( myPort.available() > 0) {
    valueFromArduino = myPort.read();
  }
  println(valueFromArduino-48);
  if (valueFromArduino-48 == -38){
    play();
    
  }
}

Lab 4: Sensors

Author: Andrew Huang

Instructor: Daniel

Date: March 3rd, 2017

Goal: To create a response on the servo motor, given an input from a sensor.

Materials: Arduino, breadboard, Jumper cables, Micro Servo, Ultrasonic sensor

WechatIMG2 WechatIMG1

After installing the Ultrasonic Library, I was ready to test that the Ultrasonic sensor worked. Initially I had some trouble hooking up the sensors because I didn’t know how to connect the power lines to all the parts, but after seeing someone else attach their parts on the breadboard, I followed suit. Then I attached the corresponding wires to the breadboard to their appropriate sensor ports: 7 for the ultrasonic and 9 for the servo motor. After quick inspection of the serial monitor, I saw that the numbers appearing on the screen were realistic. After some modification of the example code that came with the ultrasonic, and inspection of the servo sweep code, I then made the servo monitor spin in the degrees relative to the height of the roof of the building I was in (in this case the library), which was 99 inches. After that I multiplied by an offset as to encompass the entire 180 degree range (180 / 99). The results worked! My code for the lab is attached below.

 

#include "Ultrasonic.h"
#include <Servo.h>

Ultrasonic ultrasonic(7);
Servo myservo;

void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
}
void loop()
{
  long RangeInInches;
  long RangeInCentimeters;
  
  Serial.println("The distance to obstacles in front is: ");
  RangeInInches = ultrasonic.MeasureInInches();
  Serial.print(RangeInInches);//0~157 inches
  long offset = RangeInInches * 1.8181818; // height of roof offset
  myservo.write(offset);  // turns to the corrosponding degrees in inches.
  
  delay(15);
  Serial.println(" inch");
  delay(250);

  Serial.print(offset);
  Serial.println(" degrees");
  
  RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
  Serial.print(RangeInCentimeters);//0~400cm
  Serial.println(" cm");
  delay(250);
}

Lab 3: Animation in Processing

Author: Andrew Huang

Date: 2/24/17

Instructor: Daniel

NetID: amh877

For this lab, I tried to incorporate as multiple elements of processing as I can. I couldn’t think of anything to make in the beginning, but after remembering how I used React for a recent project, I decided to make an animated version of the React logo. Initially I tried to make a sphere and then overlay a smaller white one inside in order to create the logo. This did not have the desired effect, as the fill color just covered up all of my other fills underneath. After this, I looked more into Processing’s documentation, and remembered about the stroke() function and the strokeWeight() function. After that, creating the rings were trivial, and I abandoned using fills and alpha values. After I made 3 of the rings offset by 45 degrees and rotating, the logo was made. Perhaps now I’ll try to make a 3d version.

Screen+Shot+2017-02-24+at+2.25.48+PM

My code for the lab is attached below. It is not really refactored, so the code is somewhat messy.

void setup(){
  size(500, 500);
  rectMode(CENTER);
}

  int x = 0;
  int y = 0;
  int z = 50;
  float c = 10;

void outer(){
  strokeWeight(10);
  stroke(0, 216, 255);
  fill(255, 255, 255, 0);
  ellipse(x, y, z*3*3, z*2);
}
void react(){
  for (int i =0; i < 3; i++){
      outer();
      rotate(45);
  }
}
  
void draw(){
  background(255);
  translate(width/2, height/2);
  ellipse(x, y, z, z);
  text("React", -250, -230); 
  rotate(radians(c));
  react();
  fill(#00D8FF);
  c++;
}

Week 2: Arduino Basics

Author: Andrew Huang
Date: 17th Feb. 2017

Partner: Esther Liu
Instructor: Daniel
Lab section: 1:15~2:30 pm

Part 1: Traffic Sensor

Attaching all of the wires and resistors during this lab was a bit easier from familiarity from the previous lab, but the real fun was seeing the result of the lab, after the code had been flashed onto the Arduino. The cleverness of the code was revealed and it goes to show quite some thought goes behind modern stop lights. The light for walk turns on with some delay after the red light, and turns to green as the passengers can now cross the street safely.  I did not have much time after the lab to review the code, but after reviewing the code for myself afterwards, I can see the subtle features that traffic lights have.

lab2_traffic2lab2_traffic

Part 2: Temperature Sensor

The temperature sensor was a quick build, and while looking over the source code, I once again learned something new. Particularly, it was interesting how the temperature sensor, which got it’s data in volts, was able to do unit conversion to a corresponding temperature. The code provided me with enough insight to how this would work.

 

lab2_temperature lab2_temperature2

Conclusion:

The Arduino basics lab provided me with a good understanding of how the code and Arduino board brings ideas to live through sensors. However, I still don’t have a completely clear picture of how wiring the components together leads to a changes that the Arduino board itself can detect. Perhaps later on I will see how individual components can talk to sensors through a voltage change or some other mechanism.

// traffic light
void changeLights() {
	digitalWrite(carGreen, LOW); //green off
	digitalWrite(carYellow, HIGH); //yellow on
	delay(2000); //wait 2 seconds
		
	digitalWrite(carYellow, LOW); //yellow off
	digitalWrite(carRed, HIGH); //red on
        delay(1000); //wait 1 second till its safe
		
	digitalWrite(pedRed, LOW); //ped red off
	digitalWrite(pedGreen, HIGH); //ped green on
}

//temperature sensor

data = (double) val * (5/10.24);  // convert the voltage to temperture
     
    if(data>27){        // If the temperture is over 27 degree, buzzer will alarm.  
          for(int x=0; x<180; x++){
            sinVal = (sin(x*(3.1412/180)));
            toneVal = 2000+(int(sinVal*1000));
            tone(8, toneVal);
            delay(2); 
   }

Week 1: Lab 1

Author: Andrew Huang
Date: 10th Feb. 2017

Partner: Sky Yu
Instructor: Daniel
Lab section: 1:15~2:30 pm

WechatIMG3 (1)

Part 1: Speaker connection

Initially, reading the schematics was somewhat difficult, but shortly after we set it up. Following the positive and negative leads helps to make sure that we follow the intended path. However, we also setup the speaker and the 7805 chip backwards, so we found checking the labels before hooking it up helped.

WechatIMG7 (2)WechatIMG6 (1)

Part 2: LED Connection

Setting up the second circuit proved to be easy, just by simply adding a resistor and LED in the middle of the circuit where the speaker used to be. From an engineering perspective, this represents a type of “do not repeat yourself” methodology. We also found that adding the components in individually before adding the wire helps to wire the circuit much more efficiently.

WechatIMG5 (1)

Part 3: Variable Resistor

The final circuit proved to be sort of tricky to setup, but after following the schematic, intuition helped me to figure out to use the middle lead to set up the last part of the circuit, which remains from the pattern from the first circuit. It is very interesting how the variable resistor can change the brightness of the bulb through altering its resistance.

WechatIMG5

Conclusion:

The first lab provided a diverse set of circuits for us to familiarize ourselves with electronic components and their uses. I think this lab provides a fine segue into how sensors and lights will be very functional in the next Lab: Arduino basics.