Category Archives: Week 5

homework for week 5: Music Box

The two dancers dance elegantly with the music Canon, and the colorful light blinking inside the box as well.

Code:

/*
Canon for summer night (with LED) by aNdY 2013
*/

#include “pitches.h”
#include <Servo.h>

Servo myservo1; //180
Servo myservo2; //360
int value1 = 360;
int value2 = 0;

// notes in the melody:
int CanonTone[] = {
NOTE_E3, NOTE_G4, NOTE_C5, NOTE_E5, NOTE_B2, NOTE_G4, NOTE_B4, NOTE_D5,
NOTE_C3, NOTE_E4, NOTE_A4, NOTE_C5, NOTE_G2, NOTE_E4, NOTE_G4, NOTE_B4,
NOTE_A2, NOTE_C4, NOTE_F4, NOTE_A4, NOTE_E1, NOTE_C4, NOTE_E4, NOTE_G4,
NOTE_A2, NOTE_D4, NOTE_F4, NOTE_A4, NOTE_B2, NOTE_D4, NOTE_G4, NOTE_B4,
NOTE_E3, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5,
NOTE_E5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_G4,
NOTE_F4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4,
NOTE_F4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5,
NOTE_E5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4,
NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_C5, NOTE_B4, NOTE_C5,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_B4,
NOTE_C5,0
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
8,16,16,8,16,16,16,16,16,16,16,16,16, 16,
1
};

int toneSpeed = 1800; // set music speed
int ledPin;
int speakerPin = A0;
int thisNote;

void setup() {
myservo1.attach(13);
myservo2.attach(0);

thisNote = 0;
for(int pin = 1; pin <= 12; pin++) {
pinMode(pin, OUTPUT);
}
}
void loop() {
while(CanonTone[thisNote] > 0) {
// int flag = 0;
// if (flag == 0) {
// value1 += 10;
// if (value1 == 180) {
// flag = 1;
// }
// }
// else if (flag == 1){
// value1 -= 10;
// if (value1 == 0) {
// flag = 0;
// }
// }
if(thisNote < 32)
value1 -= 45;
else
value1 -= 30;
value2 += 1;
if(value1 <= 0) {
value1 += 360;
}
if(value2 >= 360) {
value1 -= 360;
}
myservo1.write(value1);
myservo2.write(value2);

int toneLevel = CanonTone[thisNote]; // get tone pitch
switch(toneLevel) {
case 262: // if C4, pin = 1
ledPin = 1;
break;
case 294: // if D4, pin = 2
ledPin = 2;
break;
case 330: // if E4, pin = 3
ledPin = 3;
break;
case 349:
ledPin = 4;
break;
case 392:
ledPin = 5;
break;
case 440:
ledPin = 6;
break;
case 494:
ledPin = 7;
break;
case 523:
ledPin = 8;
break;
case 587:
ledPin = 9;
break;
case 659:
ledPin = 10;
break;
case 698:
ledPin = 11;
break;
case 784:
ledPin = 12;
break;
default:
ledPin = 1;
break;
}
digitalWrite(ledPin, HIGH);

int noteDuration = toneSpeed / noteDurations[thisNote];
tone(speakerPin, CanonTone[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
digitalWrite(ledPin, LOW);
delay(2);
thisNote++;
if(thisNote >= 144)
thisNote -= 144;
}
// noTone(speakerPin); // turn speaker off
}

Weeks 4-6: Piezo Buzzer and Servo Motor w/ Sensors

Octi
Reactive Octi
Based on the pressure or flex applied to the embedded sensors in Octi’s arms, his eyes rotate to make him look either sad or mad.
#include <Servo.h>
Servo myservo;
int servo = 7;
int piezo = 8;
int pressure = A0;
int servo_pos = 0;
int current_pressure = 0;
int current_note = 200;
int current_spin = 1;
void setup() {
  // put your setup code here, to run once:
  pinMode(servo, OUTPUT);
  pinMode(piezo, OUTPUT);
  pinMode(pressure, INPUT);
  myservo.attach(servo);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  current_pressure = analogRead(pressure);
  Serial.print(current_pressure);
  Serial.print(” “);
  current_note = map(current_pressure, 0, 1024, 200, 2000);
  Serial.print(current_note);
  Serial.print(” “);
  current_spin = map(current_pressure, 0, 1024, 1, 20);
  Serial.println(current_spin);
  tone(piezo, current_note);
  myservo.write(servo_pos);
  servo_pos += 1*current_spin;
}

The Talking Tissue Box

So far it’s not working quite the way I want it to yet. Had problems getting Processing to read my Serial messages properly when I coded in Arduino, so in frustration, I used Firmata and controlled Arduino through Processing.

Except my talking box still isn’t working the way I want to yet. I think I bent the sensor too many times…. Seemed to work a bit better after I switched sensor.

Firmata is included with Arduino’s examples, so I’m not going to include it here. Here’s the Processing code that is still buggy:

Continue reading

hw week 5

I created a led lit cigarette box that is controlled by a tilt sensor which I attached to the top of the box which turns the led light on when the top of the box is tilted open. The sensor was a bit buggy and led flickered on and off quite a bit.

int SensorPin = 2;
int LEDPin = 3;

int LEDstate = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 50;

void setup()
{
pinMode(SensorPin, INPUT);
digitalWrite(SensorPin, HIGH);
pinMode(LEDPin, OUTPUT);
}

void loop()
{
int switchstate;

reading = digitalRead(SensorPin);

if (reading != previous) {

time = millis();
}

if ((millis() – time) > debounce) {

switchstate = reading;

if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(LEDPin, LEDstate);

previous = reading;
}

hw5.2 hw5

 

 

LUOBIN_HOMEWORK

 

This device is using Arduino, a photocell, an accelerometer, and a LCD. The goal is to help people to take more stable shots of photo/video by notification. In a darker environment, the helper will tell user to hold steady until it’s stable enough to take a clear photo. The helper will automatically turns off when there’s enough light.

The photocell senses the lightness of the environment. A line code is checking the value, if the value is low enough, it will check the realtime reading from accelerometer and give out suggestions.

Test without enclosure…………………………………

breadboard…………………………………..

wire_bbschematic…………………………………………….

 

 

code………………………………………………….

/******************************************************************************
*
* peterobbin
*
* Courtesy To:
* SFE_MMA8452Q Library Basic Example Sketch
* Jim Lindblom @ SparkFun Electronics
* Original Creation Date: June 3, 2014
* https://github.com/sparkfun/MMA8452_Accelerometer
*
* This sketch uses the SFE_MMA8452Q library to initialize the
* accelerometer, and stream values from it.
******************************************************************************/
#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library

// Begin using the library by creating an instance of the MMA8452Q
MMA8452Q accel;

float lastasValue;
float asValue;
float asThreshold = 0.015;
float lightThreshold = 220;
int photocellPin = A0;
int pcValue;
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// The setup function simply starts serial and initializes the
// accelerometer.
void setup()
{
Serial.begin(9600);
Serial.println(“MMA8452Q Test Code!”);

accel.init();

// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print(“HOLD STEADY”);
}
void loop()
{

pcValue = analogRead(photocellPin);
Serial.println(pcValue);

// Use the accel.available() function to wait for new data
// from the accelerometer.
if (accel.available())
{
// First, use accel.read() to read the new variables:
accel.read();

// accel.read() will update two sets of variables.
// * int’s x, y, and z will store the signed 12-bit values
// read out of the accelerometer.
// * floats cx, cy, and cz will store the calculated
// acceleration from those 12-bit values. These variables
// are in units of g’s.
if (pcValue < lightThreshold){
printCalculatedAccels();
lcd.setCursor(13, 1);
lcd.print(” ON”);
}
else{
lcd.setCursor(0, 0);
lcd.print(“STABILIZE HELPER”);
lcd.setCursor(13, 1);
lcd.print(“OFF”);
}

printOrientation();
Serial.println();
}
}

void printCalculatedAccels()
{
lastasValue = asValue;
asValue = accel.cz;
float a = asValue – lastasValue;
float compareValue = abs(a);
Serial.print(compareValue, 3);
Serial.print(“\t”);
Serial.print(accel.cx, 3);
Serial.print(“\t”);
Serial.print(accel.cy, 3);
Serial.print(“\t”);
Serial.print(accel.cz, 3);
Serial.print(“\t”);
if(compareValue > asThreshold){
lcd.setCursor(0, 0);
lcd.print(“HOLD STEADY “);
}
else{
lcd.setCursor(0, 0);
lcd.print(“OKAY TO SHOOT “);
}

lcd.setCursor(0, 1);
lcd.print(compareValue);
}

 

 

Homework Week5_Qianjing_Liu

IMG_0944 IMG_0949 IMG_0951 IMG_0956

 

This is a clock. When the sun light came into the small house from the break on the roof, the clock inside will make sound and weak up users. There is also a switch on the house, which can control the sound value and turn off the clock.

 

#include “pitches.h”

int speaker = 11;
int sensor1 = A0;
int sensor2 = A1;
int sensorValue = 0;
int getsensor2 = 0;
int getsensor1 = 0;

void setup() {
Serial.begin(9600);
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(speaker, OUTPUT);
}

void loop() {
getsensor1 = analogRead(sensor1);
getsensor2 = analogRead(sensor2);
sensorValue = map(getsensor2, 0, 1023, 0, 4000);
if(getsensor1<=0 || getsensor2<=0){
digitalWrite(speaker, LOW);
}else{
tone(speaker, sensorValue);
delay(1000/16);
noTone(speaker);
}
Serial.println(getsensor2);
}

Haotian_Pressure Measurement

Press the face then the LCD screen can show the pressure.

When you are angry, you may want to hit somebody. This help you to express your angry and show you angry level.

7F43438E-85F7-4CFD-9ABB-4E18FB38540C

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
int sensorPin = A2;
int val=0;

void setup()
{
lcd.begin(16,2);
lcd.print(“press”);
delay(2000);JN
lcd.clear();
}

void loop()
{
val= analogRead(sensorPin);
lcd.print(“Pressure”);
lcd.setCursor(0,1);
lcd.print(val);
delay(1000);
lcd.clear();
Serial.print(val);

}

HW4_BREATHLAMP_YU_ZHANG

 

Arduino2_bb

Arduino2_pcb

Arduino2_schem

 

 

 

BREATH LAMP

There are six pink leds on the arduino board, fading in and out, just like they’re breathing.  It also has the sounds as well.

The fading lighting will affect the photoresistor, which will lead to the buzzer to make sound. When the photoresistor detected this fading lights, the buzzer will create the ramp sound.

Also, it will be affected by something above it, when I put my hands above it, the sound appears.

————————————————

CODE:

int prPin = 0;
int prReading;
int buzzerPin = 4;
long buzzerFreq;
int led = 9;
int brightness = 0;
int fadeAmount = 5;long BUZZ_FREQ_MAX = 10000;
long PR_MAX = 1023;

void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(led, OUTPUT);
}

void loop() {
prReading = analogRead(prPin);
buzzerFreq = (prReading * BUZZ_FREQ_MAX) / PR_MAX;

buzz(buzzerPin, buzzerFreq, 10);
analogWrite(led, brightness);

brightness = brightness + fadeAmount;

if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
}

void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2;
long numCycles = frequency * length/ 1000;
for (long i=0; i < numCycles; i++){
digitalWrite(targetPin,HIGH);
delayMicroseconds(delayValue);
digitalWrite(targetPin,LOW);
delayMicroseconds(delayValue);
}
}

Demonic Baby Toy

IMG_20140925_171419782 IMG_20140925_171426322

IMG_20140925_171449560 IMG_20140925_171458413 IMG_20140925_171517436_HDR IMG_20140925_171525590 IMG_20140925_173039229 IMG_20140925_173524906 IMG_20140925_194624990 IMG_20140925_194642550

 

 

For this assignment I used a digital accelerometer with LEDs and a piezo buzzer to make a shaking baby toy.

Based on the accelerometer data, I had the LEDs light up at a certain speed and remapped the speed data to the piezo tone so that at higher speeds it squealed at a higher pitch.

I also learned to solder, and had some fun with the enclosure.

I feel a little bit bad about making this, just a little bit.