Tuesday, September 25, 2012

Erin | Ch4 Prj 11, 12, 13

Prj 11 Piezo Sounder Alarm


This version sounds like a creaky door.


// Project 11 - Piezo Sounder Alarm
float sinVal; //holds sin value to cause the tone to rise and fall
int toneVal;  //takes the sin val and converts to frequency

void setup() {
  pinMode(8, OUTPUT);
}

void loop() {
        for (int x=0; x<180; x++) { //set up so sine value doesn't go into negative

        // convert degrees to radians then obtain sin value
        sinVal = (sin(x*(3.1412/180)));
        // generate a frequency from the sin value
        toneVal = 50+(int(sinVal*100));
        tone(8, toneVal);
        delay(25); //defined here to allow for sufficent delay
}
}

Prj 12 Piezo Sounder Melody Player

with commentst

// Project 12 - Piezo Sounder Melody Player
#define NOTE_C3  131 //these define directives define notes with specific tones C3 through B4
#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 WHOLE 1 //these define note lengths
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.125
#define SIXTEENTH 0.0625

// make integer array for Puff the magic dragon

int tune[] = { NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G3, NOTE_A3,
NOTE_C4, NOTE_C4, NOTE_G3, NOTE_G3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3, NOTE_E3, NOTE_G3,
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4};

// integer array to assign the length of the note as it is played
float duration[] = { EIGHTH, QUARTER+EIGHTH, SIXTEENTH, QUARTER, QUARTER, HALF, HALF,
HALF, QUARTER, QUARTER, HALF+QUARTER, QUARTER, QUARTER, QUARTER, QUARTER+EIGHTH, EIGHTH,
QUARTER, QUARTER, QUARTER, EIGHTH, EIGHTH, QUARTER, QUARTER, QUARTER, QUARTER,
HALF+QUARTER};

int length; // used to calculate and store the lenth of the array (number of notes in the tune)

void setup() {
        pinMode(8, OUTPUT);  // digtial pin 8 to output to piezo
length = sizeof(tune) / sizeof(tune[0]);  // initialize the integer with the number of notes using the sizeof() function
//which returns the number of bytes in the parameter (two bytes on arduino)  tunes[] has 26 notes 
// they are divided number of bytes in this case 26/2 = 13, if the tune is replaced this is recalculated
}

void loop() {
        for (int x=0; x<length; x++) { //counts the nnumber of timest there are notes inthe melody
        tone(8, tune[x]); //play the tune
        delay(1500 * duration[x]);  //then wait for the time ot let it play -can speed up or slow down here
        noTone(8); //turn off the tone
}
delay(5000); // stop for 5 seconds before beginning again
}

Ch4 Project 13 Piezo Knock Sensor

This is not quite working as it should, though admittedly alligator clips would be ideal. I've bent my connectors. The sensitivity had to be adjusted for it to even work, I can knock the table and the LED turns on, but sometimes the led turns on even when my hand hovers above it.

// Project 13 - Piezo Knock Sensor 
//is this what touch lamps use?
int ledPin = 9; // LED on Digital Pin 9
int piezoPin = 5; // Piezo on Analog Pin 5
int threshold = 60; // The sensor value to reach before activation
int sensorValue = 0;  // A variable to store the value read from the sensor
float ledValue = 0; // The brightness of the LED


void setup() {
        pinMode(ledPin, OUTPUT); // Set the ledPin to an OUTPUT
        // Flash the LED twice to show the program has started
        digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150);
        digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150);
}

void loop() {
        sensorValue = analogRead(piezoPin);  // Read the value from the sensor
        if (sensorValue >= threshold) { // If knock detected set brightness to max
        ledValue = 255;
}
analogWrite(ledPin, int(ledValue) ); // Write brightness value to LED
ledValue = ledValue - 0.05; // Dim the LED slowly
if (ledValue <= 0) { ledValue = 0;}  // Make sure value does not go below zero
}

project 14 Light Sensor done in class

No comments:

Post a Comment