Darkness Loving Owl
Owl
A piezo speaking owl that communicates when it is in distress from the brightness in the room. It is a darkness loving prototype that I worked on with Minho
</code>
#define v 18000
#define x 10000
#define z 8000
#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz
#define D 1680
#define E 1500
#define F 1350
#define G 1200
#define A 1050
#define B 912
// Define a special note, 'R', to represent a rest
#define R 0
// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 9;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness;
int growspeed;
int bN = 0;
int bNcount = 0;
void setup() {
pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
}
// MELODY and TIMING =======================================
// melody[] is an array of notes, accompanied by beats[],
// which sets each note's relative length (higher #, longer note)
int hellom[] = {C, D, C};
int beats[] = {16, 64, 24};
int dyingm[] = { g, R,R, z, x };
int beats2[] = { 200, 8, 16, 8, 32 };
int happym[] = { c, C, c, C, g, G, g, G, c, C, c, C, g, G, g, G};
int beats3[] = { 16, 8, 16, 8, 16, 16, 24, 16, 16, 8, 16, 8, 16, 16, 24, 16};
int sadm[] = {c, x, c, z, c, x, z};
int beats4[] = {36, 24, 36, 36, 36, 24, 64 };
int yesm[] = {d, E, A, G};
int beats5[] = {8, 16, 16, 24,};
int nom[] = {f, x, f};
int beats6[] = {8, 16, 8};
int MAX_COUNT = sizeof(hellom) / 2;
int MAX_COUNT2 = sizeof(dyingm) / 2;
int MAX_COUNT3 = sizeof(happym) / 2;
int MAX_COUNT4 = sizeof(sadm) / 2;
int MAX_COUNT5 = sizeof(yesm) / 2;
int MAX_COUNT6 = sizeof(nom) / 2;// Melody length, for looping.
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
// Initialize core variables
int tone1 = 0;
int beat = 0;
long duration = 0;
// PLAY tone1 ==============================================
// Pulse the speaker to play a tone1 for a particular duration
void playtone1() {
long elapsed_time = 0;
if (tone1 > 0) { // if this isn't a Rest beat, while the tone1 has
// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone1 / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone1 / 2);
// Keep track of how long we pulsed
elapsed_time += (tone1);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}
// LET THE WILD RUMPUS BEGIN =============================
void loop() {
// play each sound in order, with a pause between each one.
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading);
Serial.println( growspeed);
Serial.println(bNcount);
LEDbrightness = 255;
//LEDbrightness = map(photocellReading, 200, 600, 0, 255);
// analogWrite(LEDpin, LEDbrightness);
growspeed = map(photocellReading,0, 1023, 10000, 2000);
//if(photocellReading>900){
//
// delay(3000);
// yes();
// delay(3000);
// hello();
// delay(3000);
// happy();
//
//}
//
// if(photocellReading<200){
// //delay(3000);
// sad();
// //delay(2000);
//}
if(photocellReading>300){
bN = 0;
bNcount = bNcount +1;
if(bNcount == 5){
bN= 1 ;
bNcount = 0;
}
}
if(photocellReading<300){
bN = 4;
bNcount = bNcount +1;
if(bNcount == 5){
bN= 5 ;
bNcount = 0;
}
}
switch (bN) {
case 0: // your hand is on the sensor
hello();
break;
case 1: // your hand is close to the sensor
analogWrite(LEDpin, LEDbrightness);
happy();
break;
case 2: // your hand is a few inches from the sensor
// game3();
break;
case 3: // your hand is nowhere near the sensor
// game4();
break;
case 4: // your hand is nowhere near the sensor
no();
break;
case 5: // your hand is nowhere near the sensor
dying();
break;
case 6: // your hand is nowhere near the sensor
sad();
break;
}
}
void hello() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
tone1 = hellom[i];
beat = beats[i];
duration = beat * tempo; // Set up timing
playtone1();
// A pause between notes...
delayMicroseconds(pause);
//noTone(speakerOut);
}
delay(growspeed);
}
void dying() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT2; i++) {
tone1 = dyingm[i];
beat = beats2[i];
duration = beat * tempo; // Set up timing
playtone1();
// A pause between notes...
delayMicroseconds(pause);
//noTone(speakerOut);
}
delay(10000);
}
void happy() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT3; i++) {
tone1 = happym[i];
beat = beats3[i];
duration = beat * tempo; // Set up timing
playtone1();
// A pause between notes...
delayMicroseconds(pause);
//noTone(speakerOut);
}
delay(8000);
}
void sad() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT4; i++) {
tone1 = sadm[i];
beat = beats4[i];
duration = beat * tempo; // Set up timing
playtone1();
// A pause between notes...
delayMicroseconds(pause);
//noTone(speakerOut);
}
delay(2000);
}
void yes() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT5; i++) {
tone1 = yesm[i];
beat = beats5[i];
duration = beat * tempo; // Set up timing
playtone1();
// A pause between notes...
delayMicroseconds(pause);
//noTone(speakerOut);
}
delay(growspeed);
}
void no() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT6; i++) {
tone1 = nom[i];
beat = beats6[i];
duration = beat * tempo; // Set up timing
playtone1();
// A pause between notes...
delayMicroseconds(pause);
//noTone(speakerOut);
}
delay(2000);
}<code>


makingtoys 8:42 pm on February 17, 2011 Permalink |
Please just your code in “read more”. thnks!