Halloween Candy Meter

For this lab, I decided to make a Halloween Candy meter that I will be needing very soon as Halloween is around the corner. My community is a very enthusiastic about trick or treating and as a result of that kids are able to amass an amazing amount of candy. This ends up being a nightmare for the parents. Many a times they accompany their kids and start nagging them to stop when candy levels get too high. The kids obviously do not enjoy this experience of being shadowed around when they are having fun trick-o-treating with their friends.

I decided to use my Arduino kit to solve this problem. I used the FSR to sense the weight of candy collected and calibrated it such that –

  • it would start a blinking led when it is getting closer to a pound of candy
  • it would start beeping (using the piezo) when the weight of the collected candy got to the one-pound mark (signaling that it is time to go home)

After uploading my code into the Arduino, I encased the breadboard and the Arduino in a takeout deli box with the FSR sensor facing outward towards the candy. I then put the deli box in my daughter’s Halloween Jack o Lantern candy collector. The video below illustrates that –

https://drive.google.com/a/berkeley.edu/file/d/0ByHw8c_nutT2R2hCUzNVMEo3Sjg/view?usp=sharing

All I need to think about now is to put together a costume which is way more fun than nagging a toddler around on a fun trick-o-treating night!

Components – FSR, LED and Piezo

Code:

/*
* ——–
*
*
* Created 24 October 2006
* copyleft 2006 Tod E. Kurt <tod@nulltodbot.com
* http://todbot.com/
*/

int fsrPin = 0; // select the input pin for the potentiometer
int speakerPin = 7;
int ledPin = 13;

int fsrReading = 0;
int val = 0;

void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println(“ready”);
}

void loop() {

digitalWrite(speakerPin, LOW);

fsrReading = analogRead(fsrPin); // read value from the sensor
Serial.println(fsrReading);
val = fsrReading*2; // process the value a little
//val = val/2; // process the value a little

if (fsrReading < 10) {
Serial.println(” – No pressure”);
} else if (fsrReading < 100) {
Serial.println(” – Light squeeze”);
digitalWrite(ledPin, HIGH); // turn the LED on pin 13 on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED on pin 13 off by making the voltage LOW
delay(100);
} else if (fsrReading < 400) {
Serial.println(” – Medium squeeze”);
} else {
Serial.println(” – Big squeeze”);
for( int i=0; i<500; i++ ) { // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(val);
digitalWrite(speakerPin, LOW);
delayMicroseconds(val);
}

}

}

 

Paper flower singing Super Mario theme song

Description

I created a singing paper flower which can change colors following the rhythm. I can use a potentiometer to adjust the brightness of the colors.

Components

  • 1 Arduino
  • 1 Pot
  • 1 Piezo Buzzer
  • 3 LEDs
  • 4 Resistors
  • 1 Breadboard
  • several Papers

Code

#include "pitches.h"

const int piezoPin = 7; //piezo
int rPin = 9;  //red LED
int gPin = 11;  //green LED
int bPin = 10;  //blue LED
int potPin = A0;  //pot 

int potValue = 0;
// notes

int melody[] = {NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0};

// durations: 2 = half note, and 8/3,4,6,8,12. It appears that 8/2.9 is more accurate than 8/3.

float noteDurations[] = {
 12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};

// calculates the number of elements in the melody array.
int musicLength=sizeof(melody)/sizeof('NOTE_F5');

void setup() {  
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT);
}

void loop() {
    Serial.println(potValue);
       
    for (int thisNote = 0; thisNote &lt; musicLength; thisNote++) {
         potValue = analogRead(potPin);
      
      // calculate the note duration. change tempo by changing 2000 to other values
      int noteDuration = 2000/noteDurations[thisNote];
      tone(piezoPin, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well
      float pauseBetweenNotes = noteDuration * 1.1;
      delay(pauseBetweenNotes);

      // blink the three LEDs in sequence
      if (thisNote%3==0){    
          analogWrite(rPin, potValue/4);
          analogWrite(gPin, 0);
          analogWrite(bPin, 0);
          delay(100);
      }
      else if (thisNote%3==1){    
          analogWrite(rPin, 0);
          analogWrite(gPin, potValue/4);
          analogWrite(bPin, 0);
          delay(100);
      }
      else if (thisNote%3==2){    
          analogWrite(rPin, 0);
          analogWrite(gPin, 0);
          analogWrite(bPin, potValue/4);
          delay(100);
      }
   }
      
}


Best birthday card ever!

Description:

For this lab, I decided to make the best birthday card ever. For those who just can’t get enough of having people sing them Happy Birthday (or those who like to celebrate for an entire month—long after their friends have grown tired of singing Happy Birthday to them), this birthday card is for them. Every time the special birthday lady or gentleman opens the card, it plays them Happy Birthday. It uses a photocell to determine when the card is opened, and I used a modified notebook to stand in for the card because I don’t actually have any birthday cards lying around, sadly. Thankfully for those in the same room, the card will stop playing the melody, but will start again from the beginning once opened again.

Components:

  • Arduino Uno
  • Breadboard
  • 1 10kΩ resistor
  • 1 photocell
  • 1 piezo speaker
  • jumper cables
  • birthday card

img_1228

img_1229

Code:


// TUI Lab 05 - Molly
// Happy birthday melody sourced from http://forum.arduino.cc/index.php?topic=178460.0

int speakerPin = 9;
int photoPin = A1;
int length = 28; // the number of notes
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8,8, 16, 1, 2,2,8,8,8,8,16, 1,2,2,8,8,8,16 };
int tempo = 150;
int val = 0;

void playTone(int tone, int duration) {
for (long i = 0; i &lt; duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
'c', 'd', 'e', 'f', 'g', 'a', 'b',
'x', 'y' };

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
956, 834, 765, 593, 468, 346, 224,
655 , 715 };
int SPEE = 5;

// play the tone corresponding to the note name
for (int i = 0; i &lt; 17; i++) { 
  if (names[i] == note) { 
    int newduration = duration/SPEE; 
    playTone(tones[i], newduration); 
    } 
  } 
} 

void setup() { 
  pinMode(speakerPin, OUTPUT); 
  Serial.begin(9600); 
} 

void loop() { 
  Serial.print("Value:"); 
  Serial.print(analogRead(photoPin)); 
  while (analogRead(photoPin) &gt; 200) {
    for (int i = 0; i &lt; length; i++) { 
      if (notes[i] == ' ' and analogRead(photoPin) &lt; 200) {
        delay(beats[i] * tempo); // rest
      }
      else if (analogRead(photoPin) &lt; 200) {
        playNote(notes[i], beats[i] * tempo);
      }
      else break;
    // pause between notes
    delay(tempo);
   }
  }
}

Goodnight Bowl

Description

This bowl acts as a night-light and glows red when picked up while it is dark. Upon being set down, it plays “Twinkle, Twinkle Little Star” and the light slowly fades. During the day when there is light, it appears to be just a bowl of cotton balls. In this way, it acts as an object that will put the user to sleep at the end of their day.

I used a force-sensitive resistor on the bottom of the bowl to measure when it is set down, and I used a light sensor on the bowl to measure whether it is dark around the bowl. A piezo speaker plays music.

Materials

  • 1 ceramic bowl
  • 1 force-sensitive resistor
  • masking tape
  • 1 Arduino
  • cotton balls
  • 1 red LED
  • 1 light sensor
  • 1 piezo speaker
  • 1 22 KΩ resistor
  • 3 10 KΩ resistors
  • wires

Images

Goodnight Bowl

Goodnight Bowl

Code

int ledPin = 9;     // select the output pin for LED
int speakerPin = 7; // select the output pin for speaker
int lightSensorPin = A0;   // select input for light sensor
int forceSensorPin = A2;   // select input for force sensor

int forceValue = 0;  // variable to store force value
int lightValue = 0;  // variable to store light value
int wasHeld = 0; // boolean for if force was very low in last loop

byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1351, 1275, 1136, 1014, 956}; // f was changed to f sharp (370 Hz)
byte melody[] = "7d1p7d1p7a1p7a1p7b1p7b1p8a6a2p7g1p7g1p7f1p7f1p7e1p7e1p8d6d2p";
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int LIGHT_THRESHOLD = 200;
int FORCE_THRESHOLD = 100;

void setup() {
  // put your setup code here, to run once:
  pinMode(speakerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int max_subloops;
  // put your main code here, to run repeatedly:
  lightValue = analogRead(lightSensorPin);
  // turn the ledPin on, convert from 0-1023 scale to 0-255
  //analogWrite(ledPin, (lightValue - 300) / 4); //forceValue / 4 + 

  forceValue = analogRead(forceSensorPin);
  if (forceValue >= FORCE_THRESHOLD && wasHeld && lightValue < LIGHT_THRESHOLD) {
    wasHeld = 0;
    digitalWrite(speakerPin, LOW);
    max_subloops = sizeof(melody) / 2;
    for (count = 0; count < max_subloops; count++) {
      analogWrite(ledPin, 255 * (max_subloops - count - 1) / max_subloops);
      // 48 converts from byte to int
      // 30 is length of time
      for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {  
        for (count2=0;count2<8;count2++) {        // 8 is the size of names
          if (names[count2] == melody[count*2 + 1]) {       
            digitalWrite(speakerPin,HIGH);
            delayMicroseconds(tones[count2]);
            digitalWrite(speakerPin, LOW);
            delayMicroseconds(tones[count2]);
          } 
          if (melody[count*2 + 1] == 'p') {
            // make a pause of a certain size
            digitalWrite(speakerPin, 0);
            delayMicroseconds(500);
          }
        }
      }
    }
   } else if (forceValue < FORCE_THRESHOLD) {
    if (lightValue < LIGHT_THRESHOLD) {
     digitalWrite(ledPin, HIGH); 
    }
    wasHeld = 1;
  } else {
    wasHeld = 0;
  }

  Serial.print(lightValue);
  Serial.println();
}

Quickening and slowing childhood

Summary

When I first heard the piezo speaker output, I instantly thought of Super Mario Bros. The theme song has a similar tone, and is a game which led me to think of some kind of joystick like the ones used on the old controllers.I cut an old toilet paper tube and attached the speaker and part of a pot to one half, which attaching the turning part of the pot to the other half. By turning the two halves in opposite directions, you can control the tempo of the song.

You can watch the video here!


Components

  • Arduino
  • Breadboard
  • 1 potentiometer
  • 1 piezo speaker
  • hookup wires
  • toilet paper roll, scissors, tape

Code

/*
 Arduino Mario Bros Tunes
 With Piezo Buzzer and PWM

 Connect the positive side of the Buzzer to pin 3,
 then the negative side to a 1k ohm resistor. Connect
 the other side of the 1 k ohm resistor to
 ground(GND) pin on the Arduino.

 by: Dipto Pratyaksa
 last updated: 31/3/13
*/

/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

#define melodyPin 3
//Mario main theme melody
int melody[] = {
 NOTE_E7, NOTE_E7, 0, NOTE_E7,
 0, NOTE_C7, NOTE_E7, 0,
 NOTE_G7, 0, 0, 0,
 NOTE_G6, 0, 0, 0,

 NOTE_C7, 0, 0, NOTE_G6,
 0, 0, NOTE_E6, 0,
 0, NOTE_A6, 0, NOTE_B6,
 0, NOTE_AS6, NOTE_A6, 0,

 NOTE_G6, NOTE_E7, NOTE_G7,
 NOTE_A7, 0, NOTE_F7, NOTE_G7,
 0, NOTE_E7, 0, NOTE_C7,
 NOTE_D7, NOTE_B6, 0, 0,

 NOTE_C7, 0, 0, NOTE_G6,
 0, 0, NOTE_E6, 0,
 0, NOTE_A6, 0, NOTE_B6,
 0, NOTE_AS6, NOTE_A6, 0,

 NOTE_G6, NOTE_E7, NOTE_G7,
 NOTE_A7, 0, NOTE_F7, NOTE_G7,
 0, NOTE_E7, 0, NOTE_C7,
 NOTE_D7, NOTE_B6, 0, 0
};
//Mario main them tempo
int tempoFast[] = {
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 9, 9, 9,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,

 9, 9, 9,
 12, 12, 12, 12,
 12, 12, 12, 12,
 12, 12, 12, 12,
};

int tempoSlow[] = {
 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,

 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,

 4, 4, 4,
 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,

 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,

 4, 4, 4,
 6, 6, 6, 6,
 6, 6, 6, 6,
 6, 6, 6, 6,
};

int speaker = 3;
int sensorValue = 0; // variable to store value coming from sensor
int pot = A0;

void setup(void)
{
 Serial.begin(9600);
 pinMode(speaker, OUTPUT);//declare speaker as output
}
void loop()
{
 sensorValue = analogRead(pot);
 Serial.println("ready");
 Serial.println(sensorValue);

 // iterate over the notes of the melody:
 if (sensorValue &lt; 500) {
 Serial.println("slow");
 Serial.println(" 'Mario Theme1'");
 int size = sizeof(melody) / sizeof(int);
 for (int thisNote = 0; thisNote &lt; size; thisNote++) {

 // to calculate the note duration, take one second
 // divided by the note type.
 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
 int noteDuration = 1000 / tempoFast[thisNote];

 buzz(melodyPin, melody[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);

 // stop the tone playing:
 buzz(melodyPin, 0, noteDuration);

 }

 } else {

 Serial.println(" 'Mario Theme2'");
 int size = sizeof(melody) / sizeof(int);
 for (int thisNote = 0; thisNote &lt; size; thisNote++) {

 // to calculate the note duration, take one second
 // divided by the note type.
 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
 int noteDuration = 1000 / tempoSlow[thisNote];

 buzz(melodyPin, melody[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);

 // stop the tone playing:
 buzz(melodyPin, 0, noteDuration);

 }
 }
}

void buzz(int targetPin, long frequency, long length) {
 digitalWrite(13, HIGH);
 long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
 //// there are two phases to each cycle
 long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
 //// multiply frequency, which is really cycles per second, by the number of seconds to
 //// get the total number of cycles to produce
 for (long i = 0; i &lt; numCycles; i++) { // for the calculated length of time...
 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
 delayMicroseconds(delayValue); // wait for the calculated delay value
 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
 delayMicroseconds(delayValue); // wait again or the calculated delay value
 }
 digitalWrite(13, LOW);

}

Lab 5: Reactive Flower

Description

This rose responds to specific interactions. When a bug lands near the bloom, it turns on, pulsing white light. When a face comes near to the leaf (giving CO2), it calmly pulses blue light. And if you try to pick the flower, squeezing the base of the stem with moderate pressure, it flashes red light. At higher pressure, this flashing is accompanied by a blaring warning sound.

Components

  • Arduino
  • Breadboard
  • 2 photosensors
  • 1 force sensitive resistor (FSR)
  • 1 red, 1 blue, and 1 white LED
  • 1 piezo speaker
  • hookup wires
  • paper, straws, tape

Code

/*
controls various inputs and output to an origami flower
By Leah Rosenbaum
*/

int pullSensor = A0; // force sensor at the base of the stem
int bugSensor = A2; // photo sensor for presense of bug on the calyx
int proximitySensor = A1; // photo sensor for proximity of face to leaf

// select the pins for LED output:
int redPin = 11;
int whitePin = 9;
int bluePin = 10;
int piezoPin = 7;

int pullValue = 0; // stores value coming from the pull/force sensor
int bugValue = 0; // stores value coming from the photo/bug sensor
int proximityValue = 0; // stores value coming from the photo/proximity sensor

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(whitePin, OUTPUT);
pinMode(piezoPin, OUTPUT);

Serial.begin(9600);
}

void loop() {
digitalWrite(piezoPin, LOW);

pullValue = analogRead(pullSensor); // read the value from the force sensor
bugValue = analogRead(bugSensor); // read the value from the photo/bug sensor
proximityValue = analogRead(proximitySensor); // read in the value from the photo/proximity sensor

Serial.print("pull value:");
Serial.println(pullValue);
Serial.print("bug value:");
Serial.println(bugValue);
Serial.print("proximity value:");
Serial.println(proximityValue);

if ( pullValue &gt; 700) { //if pulling too hard on the stem
allOff();

//flash the red LED and buzz piezo:
digitalWrite(redPin, HIGH); //turn on red LED
buzzPiezo(1000);
delay(25);

digitalWrite(redPin, LOW); //turn off red LED
buzzPiezo(1000);
delay(25);
}
else if (pullValue &gt; 500) { //touching the stem
allOff();

//flash the red LED:
digitalWrite(redPin, HIGH);
delay(150);
digitalWrite(redPin, LOW);
delay(150);
}
else { //minimal force input
if (proximityValue &lt; 800){ //and proximity to leaf
allOff();

//pulse blue:
fadePin(bluePin, 15, 30);
}
else { //minimal proximity input
if (bugValue &lt; 900){
allOff();

//pulse the white LED:
fadePin(whitePin, 5, 30);
}
}
}
delay(500);
}

void buzzPiezo(int delayTime){
for( int i=0; i&lt;250; i++ ) { // play it for 50 cycles
digitalWrite(piezoPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(piezoPin, LOW);
delayMicroseconds(delayTime);
}
}

void fadePin(char pinName, int fadeStep, int waitTime){
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue &lt;= 255; fadeValue += fadeStep) {
analogWrite(pinName, fadeValue);
delay(waitTime); // wait to see the dimming effect
}
for (int fadeValue = 255 ; fadeValue &gt;= 0; fadeValue -= fadeStep) {
analogWrite(pinName, fadeValue);
delay(waitTime); // wait to see the dimming effect
}
}

void allOff(){
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(whitePin, LOW);
digitalWrite(piezoPin, LOW);
}

Images

responsive-flower

[Lab05] Music Box ♪೭੧(❛▿❛✿)੭೨♪

Description

When I was little, I loved music boxes, because I thought they were a toy that offered some interaction. Twist the top, and then let the melody play. There was both a visual and an audio output. The ballerina would twist in pirouettes to the tune of the Nutcracker. In this lab, I decided to create my own using a Little Mermaid Theme.

The dolphin can bob back and forth a bit, but when pressed, the melody will speed up faster. If the melody is too distracting, someone can twist the potentiometer to turn down the sound. There is a mix of red and blue light in the bottom layer of the box that blinks to the melody.

Materials

  • Arduino, Breadboard, Jumper Wires
  • Potentiometer, Force Sensitive Resistor, LEDs
  • Daiso Dolphin (Token), Faux shards (Reflecting diffused light)
  • Sponge (Diffuser)
  • Silk Tape
  • Code

    
    
    int potPin = A0;
    int ledPin = 13;
    int fsrPin = A1;
    int ledPin2 = 12;
    int speakerOut = 7;               
    byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
    int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
    byte melody[] = "2d1p2e1p2f1p5f5p2e1p2f1p2g1p5g5p1p2d1p2e1p2f1p5f8p2e1p2f1p2e1p2f1p2g1p2g1p5g5p2e1p2f1p5g1p2f1p2g1p5f2d1p2f1p2g1p2a1p2a1p4g8p";
    // count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
    //                                10                  20                  30
    int count = 0;
    int count2 = 0;
    int count3 = 0;
    int MAX_COUNT =62;
    int statePin = LOW;
    
    void setup() {
     pinMode(ledPin, OUTPUT); 
     pinMode(potPin, INPUT);
     pinMode(ledPin2, OUTPUT);
     pinMode(speakerOut, OUTPUT);
    }
    
    void loop() {
      digitalWrite(speakerOut, LOW);     
      for (count = 0; count < MAX_COUNT; count++) {
        statePin = !statePin;
        digitalWrite(ledPin, statePin);
        for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
          for (count2=0;count2<8;count2++) {
            if (names[count2] == melody[count*2 + 1]) {       
              digitalWrite(speakerOut,HIGH);
              delayMicroseconds(tones[count2]);
              digitalWrite(ledPin,HIGH);
              digitalWrite(ledPin2, HIGH);
    
              
              //delayMicroseconds(analogRead(potPin));
              digitalWrite(speakerOut, LOW);
              delayMicroseconds(tones[count2]);
              digitalWrite(ledPin,LOW);
              digitalWrite(ledPin2, LOW);
     
              //delayMicroseconds(analogRead(potPin));
            } 
            if (melody[count*2 + 1] == 'p') {
              // make a pause of a certain size
              digitalWrite(speakerOut, 0);
              Serial.println(analogRead(potPin));
              delayMicroseconds(analogRead(fsrPin));
            }
          }
        }
      }
    }

    Harry Potter & the Photocell

    For this lab, I chose to incorporate Harry Potter into my project once again. This time, I chose to trigger the Harry Potter theme song when you wave your magic wand over the Sorting Hat. When you do, the main song from the movie will play from the hat.

    Components:

    1. 1 Arduino Board
    2. 1 Bread board
    3. 1 Piezo speaker
    4. 4 wires
    5. 1 photocell
    6. 1 USB cord
    7. 1 Sorting Hat

     

    Code:

    /*
     Melody
    
     Plays a melody
    
     circuit:
     * 8-ohm speaker on digital pin 8
    
     created 21 Jan 2010
     modified 30 Aug 2011
     by Tom Igoe
    
    This example code is in the public domain.
    
     http://www.arduino.cc/en/Tutorial/Tone
    
     */
    
    /*************************************************
     * Public Constants
     *************************************************/
    
    #define NOTE_B0 31
    #define NOTE_C1 33
    #define NOTE_CS1 35
    #define NOTE_D1 37
    #define NOTE_DS1 39
    #define NOTE_E1 41
    #define NOTE_F1 44
    #define NOTE_FS1 46
    #define NOTE_G1 49
    #define NOTE_GS1 52
    #define NOTE_A1 55
    #define NOTE_AS1 58
    #define NOTE_B1 62
    #define NOTE_C2 65
    #define NOTE_CS2 69
    #define NOTE_D2 73
    #define NOTE_DS2 78
    #define NOTE_E2 82
    #define NOTE_F2 87
    #define NOTE_FS2 93
    #define NOTE_G2 98
    #define NOTE_GS2 104
    #define NOTE_A2 110
    #define NOTE_AS2 117
    #define NOTE_B2 123
    #define NOTE_C3 131
    #define NOTE_CS3 139
    #define NOTE_D3 147
    #define NOTE_DS3 156
    #define NOTE_E3 165
    #define NOTE_F3 175
    #define NOTE_FS3 185
    #define NOTE_G3 196
    #define NOTE_GS3 208
    #define NOTE_A3 220
    #define NOTE_AS3 233
    #define NOTE_B3 247
    #define NOTE_C4 262
    #define NOTE_CS4 277
    #define NOTE_D4 294
    #define NOTE_DS4 311
    #define NOTE_E4 330
    #define NOTE_F4 349
    #define NOTE_FS4 370
    #define NOTE_G4 392
    #define NOTE_GS4 415
    #define NOTE_A4 440
    #define NOTE_AS4 466
    #define NOTE_B4 494
    #define NOTE_C5 523
    #define NOTE_CS5 554
    #define NOTE_D5 587
    #define NOTE_DS5 622
    #define NOTE_E5 659
    #define NOTE_F5 698
    #define NOTE_FS5 740
    #define NOTE_G5 784
    #define NOTE_GS5 831
    #define NOTE_A5 880
    #define NOTE_AS5 932
    #define NOTE_B5 988
    #define NOTE_C6 1047
    #define NOTE_CS6 1109
    #define NOTE_D6 1175
    #define NOTE_DS6 1245
    #define NOTE_E6 1319
    #define NOTE_F6 1397
    #define NOTE_FS6 1480
    #define NOTE_G6 1568
    #define NOTE_GS6 1661
    #define NOTE_A6 1760
    #define NOTE_AS6 1865
    #define NOTE_B6 1976
    #define NOTE_C7 2093
    #define NOTE_CS7 2217
    #define NOTE_D7 2349
    #define NOTE_DS7 2489
    #define NOTE_E7 2637
    #define NOTE_F7 2794
    #define NOTE_FS7 2960
    #define NOTE_G7 3136
    #define NOTE_GS7 3322
    #define NOTE_A7 3520
    #define NOTE_AS7 3729
    #define NOTE_B7 3951
    #define NOTE_C8 4186
    #define NOTE_CS8 4435
    #define NOTE_D8 4699
    #define NOTE_DS8 4978
    
    int potPin = 0;
    int val = 0;
    int speakerPin = 7;
    
    void setup() {
     pinMode(speakerPin, OUTPUT); 
     Serial.begin(9600);
     Serial.println("ready");
    
    }
    
    void loop() {
     digitalWrite(speakerPin, LOW); 
     val = analogRead(potPin); // read value from the sensor
     Serial.println(val);
    
     if (val &gt; 750) {
     tone(speakerPin, 493.88, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 659.25, 1500/3);
     delay(1500/3);
     tone(speakerPin, 783.99, 500/3);
     delay(500/3);
     tone(speakerPin, 739.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 659.25, 2000/3);
     delay(2000/3);
     tone(speakerPin, 987.77, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 880.00, 3000/3);
     delay(3000/3);
     
     tone(speakerPin, 739.99, 3000/3);
     delay(3000/3);
     
     tone(speakerPin, 659.25, 1500/3);
     delay(1500/3);
     tone(speakerPin, 783.99, 500/3);
     delay(500/3);
     tone(speakerPin, 739.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 622.25, 2000/3);
     delay(2000/3);
     tone(speakerPin, 698.46, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 493.88, 5000/3);
     delay(5000/3);
     tone(speakerPin, 493.88, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 659.25, 1500/3);
     delay(1500/3);
     tone(speakerPin, 783.99, 500/3);
     delay(500/3);
     tone(speakerPin, 739.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 659.25, 2000/3);
     delay(2000/3);
     tone(speakerPin, 987.77, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1174.66, 2000/3);
     delay(2000/3);
     tone(speakerPin, 1108.73, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1046.50, 2000/3);
     delay(2000/3);
     tone(speakerPin, 830.61, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1046.50, 1500/3);
     delay(1500/3);
     tone(speakerPin, 987.77, 500/3);
     delay(500/3);
     tone(speakerPin, 932.33, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 466.16, 2000/3);
     delay(2000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 659.25, 5000/3);
     delay(5000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 987.77, 2000/3);
     delay(2000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 987.77, 2000/3);
     delay(2000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1046.50, 2000/3);
     delay(2000/3);
     tone(speakerPin, 987.77, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 932.33, 2000/3);
     delay(2000/3);
     tone(speakerPin, 739.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 783.99, 1500/3);
     delay(1500/3);
     tone(speakerPin, 987.77, 500/3);
     delay(500/3);
     tone(speakerPin, 932.33, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 466.16, 2000/3);
     delay(2000/3);
     tone(speakerPin, 493.88, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 987.77, 5000/3);
     delay(5000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 987.77, 2000/3);
     delay(2000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 987.77, 2000/3);
     delay(2000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1174.66, 2000/3);
     delay(2000/3);
     tone(speakerPin, 1108.73, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1046.50, 2000/3);
     delay(2000/3);
     tone(speakerPin, 830.61, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 1046.50, 1500/3);
     delay(1500/3);
     tone(speakerPin, 987.77, 500/3);
     delay(500/3);
     tone(speakerPin, 932.33, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 466.16, 2000/3);
     delay(2000/3);
     tone(speakerPin, 783.99, 1000/3);
     delay(1000/3);
     
     tone(speakerPin, 659.25, 5000/3);
     delay(5000/3);
    
     } 
    
     
    }

     

     

    Sorting Hat

    Arduino Lamp Photocell Serial Control with Bottle Cork

    Description

    I’m interested in how to push certain part of a system to create changing outputs over the time. With this in mind, for this project, I tried to mimic a serial lighting input environment for the photocell sensor, by making a paper bottle cork which is long enough to cover the photocell entirely inside. In this way, if I push and pull the hollow bottle cork, it can cover the photocell from the outside lighting from 100% to 0%. However, since white paper is actually semi-translucent to light, so I decide to use black paper to make the bottle cork, which is designed to be an excellent shading barrier for the photocell.

    Components

    • 1 paper diffuser
    • 1 black bottle cork
    • Arduino uno
    • wires
    • 1 LED
    • 1 photocell
    • 2 resistors
    • 1 laptop

     

    Code

    int sensorPin = A0; 
    int ledPin = 13; 
    int sensorValue = 0; 
    
    void setup() {
     // declare the ledPin as an OUTPUT:
     pinMode(ledPin, OUTPUT);
    }
    
    void loop() {
     // read the value from the sensor:
     sensorValue = analogRead(sensorPin);
     // turn the ledPin on
     digitalWrite(ledPin, HIGH);
     // stop the program for &lt;sensorValue&gt; milliseconds:
     delay(sensorValue*5);
     // turn the ledPin off:
     digitalWrite(ledPin, LOW);
     // stop the program for for &lt;sensorValue&gt; milliseconds:
     delay(sensorValue*5);
    }

     

    Video

     

    Photos

    img_0910 img_0911 img_0914 img_0915

     

    Enchanted bottle

    Description

    When the cap of a bottle is screwed on, the bottle is not enchanted. But, when the lid of the bottle is unscrewed, the contents inside the bottle light up and change different colors!

    Materials

    • 3 LEDs (Red, Green, Blue)
    • 3 220k ohm resistors
    • 1 potentiometer
    • 1 diffuser
    • 1 arduino uno
    • 1 breadboard

    Code

    /*
    
     * Code Adapted From:
    
     http://www.arduino.cc/en/Tutorial/AnalogInput
    
     */
    
    int sensorPin = A0; // select the input pin for the potentiometer
    int greenLedPin = 11; // select the pin for the LED
    int blueLedPin = 10;
    int redLedPin = 9;
    int sensorValue = 0; // variable to store the value coming from the sensor
    
    int potPin = A1; // Analog input pin that the potentiometer is attached to
    int potValue = 0; // value read from the pot
    int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
    int i = 1; //to drive the movement through the lights
    int valueGreen = 125;
    int valueRed = 75;
    int valueBlue =125;
    
    void setup() {
     // declare the ledPin as an OUTPUT:
     pinMode(greenLedPin, OUTPUT);
     pinMode(redLedPin, OUTPUT);
     pinMode(blueLedPin, OUTPUT);
    
     // initialize serial communications at 9600 bps:
     Serial.begin(9600);
     // declare the led pin as an output:
    // pinMode(led, OUTPUT);
    }
    
    void loop() {
     // read the value from the sensor:
     sensorValue = analogRead(sensorPin);
     Serial.println(sensorValue);
     // turn the ledPin on
     if (sensorValue &gt; 375) {
     analogWrite(greenLedPin, 0);
     analogWrite(redLedPin, 0);
     analogWrite(blueLedPin, 0);
     }
     else {
     analogWrite(greenLedPin, valueGreen);
     analogWrite(redLedPin, valueRed);
     analogWrite(blueLedPin, valueBlue);
     valueGreen += i;
     if (valueGreen == 255) {
     i = -i;
     }
     if (valueGreen == 125) {
     i = -i;
     }
     delay(20);
     }
    }
    
    

    img_7382