All the files for our music box can be found here: http://lauralippincott.com/musicbox
Here's the Sketchup file I have for us to start from. It has a cutout for each component, and interlocking box joints that require no glue or nails. We'll need to build a linkage to lift the lid, and a dancer to spin on the continuous rotation servo. Basel has composed a melody for us using the Pitches library.
#include "pitches.h"
// notes in the melody:
int melody[] = { NOTE_D3, NOTE_F3, NOTE_A3, NOTE_G3,
NOTE_C3, 0, NOTE_C3, NOTE_D3, NOTE_F3,
NOTE_A3, NOTE_D3, NOTE_F3, NOTE_A3,
NOTE_G3, NOTE_C3, 0, NOTE_C3, NOTE_F3,
NOTE_F3, NOTE_A3 };
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = { 34, 20, 20, 25, 45, 20, 20, 30,
40, 30, 55, 23, 56, 40, 35, 23, 67, 5 };
void setup(){
}
void loop(){
for (int thisNote = 0; thisNote < 18; thisNote++) {
int noteDuration = 1000/noteDurations[thisNote]; // one second divided by note duration
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8); // stop the melody
delay(1000); // pause for a second
}
}
//Code below modified by RBL to be a single function that returns a logic high if someone is within one yard of the sensor, low otherwise.
ReplyDelete/* Ping))) Sensor
This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* +V connection of the PING))) attached to +5V
* GND connection of the PING))) attached to ground
* SIG connection of the PING))) attached to digital pin 7
http://www.arduino.cc/en/Tutorial/Ping
created 3 Nov 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
// this constant won't change. It's the pin number
// of the sensor's output:
//const int pingPin = 7;
//const int outPin = 8;
//void setup(){
// initialize serial communication:
// Serial.begin(9600);
//}
long readPing()
{
const int pingPin = 7;
const int outPin = ??; //choose an output pin
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;//, cm;
//long microseconds;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(outPin, OUTPUT);
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
//inches = microsecondsToInches(duration);
inches = duration / 74 / 2;
//cm = microsecondsToCentimeters(duration);
//Serial.print(inches);
//Serial.print("in, ");
//Serial.print(cm);
// Serial.print("cm");
//Serial.println();
if (inches>36)//if no one is within a yard
{
digitalWrite(outPin, LOW);
}
else
{
digitalWrite(outPin, HIGH);
}
delay(100);
return(duration);
}
//long microsecondsToInches(long microseconds)
//{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
// return microseconds / 74 / 2;
//}
//long microsecondsToCentimeters(long microseconds)
//{
// // The speed of sound is 340 m/s or 29 microseconds per centimeter.
// // The ping travels out and back, so to find the distance of the
// // object we take half of the distance travelled.
// return microseconds / 29 / 2;
//}
This comment has been removed by the author.
ReplyDeletethe codes of my part: (Light--Photoresistor)
ReplyDeleteint PRpin = A0;
void setup(){
}
void loop(){
Serial.println(readPR());
}
long readPR(){
long PRvalue=analogRead(PRpin);
return PRvalue;
}
// Init the Pins used for PWM
ReplyDeleteconst int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
int incomingByte; // a variable to read incoming serial data into
// Init the Pins used for 10K pots
const int PR1pin = A0;
const int PR2pin = A1;
const int PR3pin = A2;
// Init our Vars
int PR1 = 0;
int PR2 = 0;
int PR3 = 0;
int PR1val, PR2val, PR3val;
int currentColorValueRed;
int currentColorValueGreen;
int currentColorValueBlue;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(PR1,INPUT);
pinMode(PR2,INPUT);
pinMode(PR3,INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
changeColor();
Serial.print("PR1 = ");
Serial.print(readPR1());
Serial.print(", PR2 = ");
Serial.print(readPR2());
Serial.print(", PR3 = ");
Serial.println(readPR3());
}
void changeColor() {
// Read the voltage on each analog pin then scale down to 0-255 and inverting the value for common anode
currentColorValueRed = (255 - map( analogRead(PR1pin), 0, 300, 0, 155 ) );
currentColorValueBlue = (255 - map( analogRead(PR2pin), 300, 600, 0, 255 ) );
currentColorValueGreen = (255 - map( analogRead(PR3pin), 600, 1024, 0, 255 ) );
// Write the color to each pin using PWM and the value gathered above
analogWrite(redPin, currentColorValueRed);
analogWrite(bluePin, currentColorValueBlue);
analogWrite(greenPin, currentColorValueGreen);
}
long readPR1(){
long PR1value = analogRead(PR1pin);
return PR1value;
}
long readPR2(){
long PR2value = analogRead(PR2pin);
return PR2value;
}
long readPR3(){
long PR3value = analogRead(PR3pin);
return PR3value;
}